如何编写界面程序C语言
用C语言编写界面程序需要以下几件事
1. C语言编辑器,如Microsoft visual C++ 6.0
2. UCGUI源代码,包括各种窗体和控件,直接在主函数中调用
编写下面的代码
1. 引用系统图形库函数
#include<windows.h>
#include "io.h"
#include "wingdi.h"
将以下代码复制到您的主函数中
#define LCD_XSIZE 800
#define LCD_YSIZE 480
#define LENGTHOF(A) (sizeof(A)/sizeof(A[0]))#define FRAME_SIZE LCD_XSIZE*LCD_YSIZE
uint16_t LCD_Frame[FRAME_SIZE];
HDC hDC;// 获取掌握台窗口句柄
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound;// This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE];
// Contains fabricated
char pszOldWindowTitle[MY_BUFSIZE];
// Contains original GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE); wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId()); SetConsoleTitle(pszNewWindowTitle); Sleep(40); hwndFound=FindWindow(NULL, pszNewWindowTitle); SetConsoleTitle(pszOldWindowTitle); return(hwndFound);
}
BITMAPINFOHEADER bmiHeader;
void LCD_Init(){
bmiHeader.biSize =sizeof(BITMAPINFOHEADER); bmiHeader.biWidth = LCD_XSIZE; bmiHeader.biHeight = -LCD_YSIZE; bmiHeader.biPlanes = 1; bmiHeader.biBitCount = 16; bmiHeader.biCompression = BI_RGB; bmiHeader.biSizeImage = 0; bmiHeader.biXPelsPerMeter = 0; bmiHeader.biYPelsPerMeter = 0; bmiHeader.biClrUsed = 0; bmiHeader.biClrImportant = 0; SetDIBitsToDevice (hDC, // hDC 0,0,LCD_XSIZE,LCD_YSIZE, 0,0,0,LCD_YSIZE, LCD_Frame,&bmiHeader,DIB_RGB_COLORS );// wUsage*/}
void LCD_Repain(){/*StretchDIBits(bufferHDC, 0,0,0-m_nVideoW,0-m_nVideoH, 0,0,m_nVideoW, m_nVideoH, szFrame,&BmpInfo,DIB_RGB_COLORS,SRCCOPY); */ StretchDIBits(hDC, 0,0,LCD_XSIZE,LCD_YSIZE, 0,0,LCD_XSIZE,LCD_YSIZE, LCD_Frame,&bmiHeader,DIB_RGB_COLORS,SRCCOPY );
}
2. 将UCGUI代码移植到您的项目中,主要是修改画点函数:
#define FRAME_SIZE LCD_XSIZE*LCD_YSIZEextern uint16_t LCD_Frame[FRAME_SIZE];#define GETDATAPTR(xPhys,yPhys) (uint16___t*)(uint32_t)LCD_Frame + 2*(xPhys+yPhys*LCD_XSIZE))#define SETPIXEL _SetPixel#define XORPIXEL _XorPixel //{*(uint16____t)GETDATAPTR(xPhys,yPhys) = 0XFFFF - *(uint16___t*)GETDATAPTR(xPhys,yPhys);} //extern uint16_t *pFrameBuffer; static void _SetPixel(int x, int y, uint16_t Pixel){ *(uint16___t*)GETDATAPTR(x,y) = Pixel;// printf("_SetPixel\r\n");} uint16_t _GetPixel(int x, int y) {uint16___t Pixel; Pixel = *GETDATAPTR(x,y);
return Pixel;
}
3. 初始化在主函数中调用,刷新函数在循环中调用
4. 执行函数后,您定义的窗口图片将展示在掌握台上