📄 tft_api.c
字号:
//=============================================================//语法格式: short TFT_GetChineseFont(STR_WINDOW *Window, STR_FONT *FontInfo);//实现功能: 获取当前使用的中文字库信息//参数: Window - 工作窗口指针// FontInfo - 字库信息结构体地址//返回值: 字库序号//=============================================================FONT TFT_GetChineseFont(WIN_HANDLE Handle, STR_FONT *FontInfo){ STR_WINDOW *Window = g_WinList + Handle; if((Handle < 0) || (Handle >= MAX_WINDOW) || (Window->Flag == 0)) return -1; FontInfo->CharWidth = 0; FontInfo->CharHeight = 0; FontInfo->FontBuf = FONT_UNUSE_FLAG; TFT_GetAsciiFontInfo(Window->ChineseFont, FontInfo); return Window->ChineseFont;}//=============================================================//语法格式: void TFT_SetAsciiFont(STR_WINDOW *Window, short FontType);//实现功能: 设置当前使用的ASCII字库//参数: Window - 工作窗口指针// FontType - 字库序号(0~n)//返回值: 无//=============================================================void TFT_SetAsciiFont(WIN_HANDLE Handle, FONT FontID){ STR_WINDOW *Window = g_WinList + Handle; if((Handle < 0) || (Handle >= MAX_WINDOW) || (Window->Flag == 0)) return; Window->AsciiFont = FontID;}//=============================================================//语法格式: short TFT_GetAsciiFont(STR_WINDOW *Window, STR_FONT *FontInfo);//实现功能: 获取当前使用的ASCII字库信息//参数: Window - 工作窗口指针// FontInfo - 字库信息结构体地址//返回值: 字库序号//=============================================================FONT TFT_GetAsciiFont(WIN_HANDLE Handle, STR_FONT *FontInfo){ STR_WINDOW *Window = g_WinList + Handle; if((Handle < 0) || (Handle >= MAX_WINDOW) || (Window->Flag == 0)) return -1; FontInfo->CharWidth = 0; FontInfo->CharHeight = 0; FontInfo->FontBuf = FONT_UNUSE_FLAG; TFT_GetChineseFontInfo(Window->ChineseFont, FontInfo); return Window->ChineseFont;}//=============================================================//语法格式: void TFT_PutChar(STR_WINDOW *Window, unsigned short CharCode);//实现功能: 显示一个字符或汉字//参数: Window - 工作窗口指针// CharCode - 字库或汉字的编码//返回值: 无//=============================================================void TFT_PutChar(WIN_HANDLE Handle, unsigned short CharCode){ STR_WINDOW *Window = g_WinList + Handle; STR_FONT Font_Char; short i, j, Temp; unsigned short *pDispBuf; unsigned char *pCharBuf; unsigned char Mask; short W, H; if((Handle < 0) || (Handle >= MAX_WINDOW) || (Window->Flag == 0)) return; TFT_GetWorkBufSize(&W, &H); TFT_CallBack_GetCharBuf(Window, CharCode, &Font_Char); // 获得字模数据 for(i=0; i<Font_Char.CharHeight; i++) { Temp = Window->TLy + Window->CurTextY + i; if(Window->CurTextY + i < 0 || Temp < 0 || Temp > Window->BRy) continue; pDispBuf = TFT_SelWorkBuf(-1) + Temp * W + Window->TLx + Window->CurTextX; pCharBuf = Font_Char.FontBuf + i * ((Font_Char.CharWidth+7)>>3); Mask = 0x80; for(j=0; j<Font_Char.CharWidth; j++) { Temp = Window->TLx + Window->CurTextX + j; if(Window->CurTextX+j>=0 && Temp>=0 && Temp<=Window->BRx) { if(*pCharBuf & Mask) { if(Window->Transparency==0) *(pDispBuf+j) = Window->FGColor; else *(pDispBuf+j) = TFT_CalcTransparent(Window, *(pDispBuf+j), Window->FGColor); } else if(Window->BGColor!=COLOR_BLACK) { if(Window->Transparency==0) *(pDispBuf+j) = Window->BGColor; else *(pDispBuf+j) = TFT_CalcTransparent(Window, *(pDispBuf+j), Window->BGColor); } } Mask >>= 1; if(Mask==0x00) { Mask = 0x80; pCharBuf++; } } }}//=============================================================//语法格式: void TFT_PutString(STR_WINDOW *Window, unsigned char *CharBuf);//实现功能: 显示字符串//参数: Window - 工作窗口指针// CharBuf - 字符串的起始地址//返回值: 无//=============================================================void TFT_PutString(WIN_HANDLE Handle, char *CharBuf){ STR_WINDOW *Window = g_WinList + Handle; unsigned char *pCharBuf = (unsigned char *)CharBuf; unsigned short CharCode; STR_FONT AsciiFont_Char; STR_FONT ChineseFont_Char; if((Handle < 0) || (Handle >= MAX_WINDOW) || (Window->Flag == 0)) return; if( (TFT_GetAsciiFontInfo(Window->AsciiFont, &AsciiFont_Char) == 0) || (TFT_GetChineseFontInfo(Window->ChineseFont, &ChineseFont_Char) == 0)) return; while((CharCode = *pCharBuf++)!='\0') { if(CharCode>=0x80) // 汉字 { if(Window->CurTextX+Window->TLx+ChineseFont_Char.CharWidth > (Window->BRx + 1)) { Window->CurTextX = 0; Window->CurTextY += ChineseFont_Char.CharHeight; if(Window->CurTextY>Window->BRy)break; } CharCode |= *(pCharBuf++) << 8; TFT_PutChar(Handle, CharCode); Window->CurTextX += ChineseFont_Char.CharWidth; } else if(CharCode==0x0D) // 回车 { Window->CurTextX = 0; Window->CurTextY += ChineseFont_Char.CharHeight; if(Window->CurTextY>Window->BRy) break; if(*pCharBuf==0x0A) pCharBuf++; } else if(CharCode==0x0A) // 回车 { Window->CurTextX = 0; Window->CurTextY += ChineseFont_Char.CharHeight; if(Window->CurTextY>Window->BRy) break; if(*pCharBuf==0x0D) pCharBuf++; } else // 字符 { if(Window->CurTextX+Window->TLx+AsciiFont_Char.CharWidth > (Window->BRx + 1)) { Window->CurTextX = 0; Window->CurTextY += AsciiFont_Char.CharHeight; if(Window->CurTextY>Window->BRy) break; } TFT_PutChar(Handle, CharCode); Window->CurTextX += AsciiFont_Char.CharWidth; } }}//=============================================================//语法格式: void TFT_Print(STR_WINDOW *Window, unsigned char *format, ...);//实现功能: 根据指定格式打印字符串//参数: Window - 工作窗口指针// format - 格式化字符串//返回值: 无//=============================================================char linebuf[256];void TFT_Print(WIN_HANDLE Handle, const char *format, ...){ va_list ap; va_start(ap, format); vsprintf(linebuf, format, ap); va_end(ap); linebuf[255] = 0; TFT_PutString(Handle, linebuf);}//=============================================================//语法格式: void TFT_PutImage(WIN_HANDLE Handle, short x, short y, STR_IMAGE *pImage);//实现功能: 在工作区中显示位图//参数: Handle - 窗口句柄// x - 位图的左上角x坐标(相对于工作区)// y - 位图的左上角y坐标(相对于工作区)// pImage - 存储位图信息的结构体地址//返回值: 无//=============================================================void TFT_PutImage(WIN_HANDLE Handle, short x, short y, STR_IMAGE *pImage){ TFT_PutImageEx(Handle, x, y, pImage->Width, pImage->Height, pImage->ImageBuf);}//=============================================================//语法格式: void TFT_PutImageEx(WIN_HANDLE Handle, short x, short y, short width, short height, void *pImage)//实现功能: 在工作区中显示位图//参数: Handle - 窗口句柄// x - 位图的左上角x坐标(相对于工作区)// y - 位图的左上角y坐标(相对于工作区)// width - 位图宽度// height - 位图高度// pImage - 位图数据首地址//返回值: 无//=============================================================void TFT_PutImageEx(WIN_HANDLE Handle, short x, short y, short width, short height, void *pImage){ STR_WINDOW *Window = g_WinList + Handle; short W, H; short ScrSx, ScrSy, ScrEx, ScrEy; short ImgSx, ImgSy, ImgEx, ImgEy; short i, j; unsigned short *pImgBuf, *pDispBuf; if((Handle < 0) || (Handle >= MAX_WINDOW) || (Window->Flag == 0)) return; TFT_GetWorkBufSize(&W, &H); ScrSx = (x<0) ? Window->TLx : Window->TLx + x; // 计算实际显示区域 if(ScrSx<0) ScrSx = 0; if(ScrSx>Window->BRx || ScrSx>=W) return; ImgSx = ScrSx - Window->TLx - x; ScrSy = (y<0) ? Window->TLy : Window->TLy + y; if(ScrSy<0) ScrSy = 0; if(ScrSy>Window->BRy || ScrSx>=H) return; ImgSy = ScrSy - Window->TLy - y; ScrEx = Window->TLx + x + width - 1; if(ScrEx > Window->BRx) ScrEx = Window->BRx; if(ScrEx >= W) ScrEx = W - 1; if(ScrEx<0 || ScrEx<Window->TLx) return; ImgEx = ScrEx - Window->TLx - x; ScrEy = Window->TLy + y + height - 1; if(ScrEy > Window->BRy) ScrEy = Window->BRy; if(ScrEy >= H) ScrEy = H - 1; if(ScrEy<0 || ScrEy<Window->TLy) return; ImgEy = ScrEy - Window->TLy - y; if(Window->Transparency==0) // 透明度为0则直接显示 { for(i=ImgSy; i<=ImgEy; i++) { pImgBuf = (unsigned short*)pImage + i * width; pDispBuf = TFT_SelWorkBuf(-1) + ScrSy * W + ScrSx; for(j=ImgSx; j<=ImgEx; j++) { *(pDispBuf++) = *(pImgBuf+j); } ScrSy++; } } else // 透明度不为0则计算叠加颜色后显示 { for(i=ImgSy; i<=ImgEy; i++) { pImgBuf = (unsigned short*)pImage + i * width; pDispBuf = TFT_SelWorkBuf(-1) + ScrSy * W + ScrSx; for(j=ImgSx; j<=ImgEx; j++) { *pDispBuf = TFT_CalcTransparent(Window, *pDispBuf, *(pImgBuf+j)); pDispBuf++; } ScrSy++; } }}//=============================================================//语法格式: void TFT_PutPicture(WIN_HANDLE Handle, short x, short y, unsigned char *pImage, int AutoScale)//实现功能: 在工作区中显示图像//参数: Window - 工作窗口指针// x,y - 图像的显示坐标// pImage - 位图文件首地址// AutoScale - 是否自适应窗口//返回值: 无//备注: 自适应窗口用于当图像大于窗口时,并且图像显示的起始不能在窗口之外//=============================================================void TFT_PutPicture(WIN_HANDLE Handle, short x, short y, unsigned char *pImage, int AutoScale){ STR_WINDOW *Window = g_WinList + Handle; STR_IMAGE Image; int Ret = 1; if((Handle < 0) || (Handle >= MAX_WINDOW) || (Window->Flag == 0)) return; Image.ImageBuf = (unsigned char *)TFT_DEPRESS_ADDR; Ret = TFT_DepressImage(&Image, pImage); if(Ret == 0) { short CurX, CurY; TFT_GetTextPos(Handle, &CurX, &CurY); TFT_SetTextPos(Handle, x, y); TFT_Print(Handle, "不支持的图像格式"); TFT_SetTextPos(Handle, CurX, CurY); return; } if(AutoScale) { if((x >= 0) && (y >= 0)) if((Image.Width > Window->Width - x) || (Image.Height > Window->Height - y)) { float HScale, VScale; int ScaleSize; HScale = (float)Image.Width / (Window->Width - x); VScale = (float)Image.Height / (Window->Height - y); if(HScale >= VScale) { // Scale the image to fit for the width of screen ScaleSize = Image.Height / HScale; TFT_ScaleImage( Image.Width, Image.Height, // Image size before scale Window->Width - x, ScaleSize, // Image size after scale (void*)TFT_DEPRESS_ADDR, (void*)TFT_DEPRESS_ADDR, // Replace the image by scaled one 0, 0); // No Reverse Image.Width = Window->Width - x; Image.Height = ScaleSize; } else { // Scale the image to fit for the height of screen ScaleSize = Image.Width / VScale; TFT_ScaleImage( Image.Width, Image.Height, // Image size before scale ScaleSize, Window->Height - y, // Image size after scale (void*)TFT_DEPRESS_ADDR, (void*)TFT_DEPRESS_ADDR, // Replace the image by scaled one 0, 0); // No Reverse Image.Width = ScaleSize; Image.Height = Window->Height - y; } } } TFT_PutImage(Handle, x, y, &Image);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -