⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emwin.c

📁 本代码包为本人的一篇文章<一个占用内存极少的菜单系统的实现>在在PC上的测试移植代码。 ------------------------------ Menu_Src目录为Menu的源
💻 C
📖 第 1 页 / 共 3 页
字号:
// Date		:	2005-05-28
//
// Author	:	HOUHH
//
// File		:	emWin.c,the main file for the emWin Simulation...
//
// Version	:	ver1.0.0

#include "stdafx.h"
#include "resource.h"
#include "Branding.h"

#include "Common.h"

typedef struct tagkeystruc{
	RECT keyarea;
	int keystate;
	SIM_HARDKEY_CB* keyrpoc;
	int keymode;
}KeyStruc, lpKeyStruc;

typedef struct tagkeydata{
	int VirCode;	//键盘虚拟码
	int key;		//对应在UCGUI当中的键值...
}KeyData, lpKey;

typedef struct tagLCD{
	HWND	lcd_hwnd;
	int		TimerID;
}LCDScreen;


#define DELAYEVERYTIME		10		//每次延时的时间(sleep())...
#define MAX_LOADSTRING		100
#define TOTAL_TASK			4		//支持多任务时的多任务数...
#define MAXKEY				80		//按键的个数...
#define MAXLCDNUM			16		//最大可打开LCD的个数...
#define VKEYNUM				11		//常用功能键数目,如VK_SHIFT等...
#define MAXLOGLEN			1024*12	//常用功能键数目,如VK_SHIFT等...


//以下为用到到部分全局变量...
int		LCDWidth = 0, LCDHeight = 0;
int		LCDWidth2 = 0, LCDHeight2 = 0;
int		wndWidth = 400, wndHeight = 300;
int		LOG_y0 = 0;								//LOG显示窗口纵坐标...
int		xPosLCD = -1, yPosLCD = -1, Mag = 1;
int		MessageBoxOnError = 1;
int		LOG_xsize = 0x1F4, LOG_ysize = 0x3C;
DWORD	timeStartup = 0;
char	LCDCaption[200];
char	LCDCaption2[200];

enum	SimStausEnum {terminated=0, started, suspended, paused, notrunning, continued, notstopped};
char*	SimStatusString[] = {"Application suspended",
					"Application started",
					"Application terminated.",
					"Application paused already...",
					"Application is not running...",
					"Application continued",
					"Application is not stopped..."};

enum	SimClassNameEnum {SimulationMainframe=0, LCDInfo, LCD, LCD1, emWinDevice, emWinHardkey, Log};
char*	SimClassName[]	= {"emWin Simulation Mainframe",
					"emWin LCD Info",
					"emWin LCD",
					"emWin LCD1",
					"emWinDevice",
					"emWinHardkey",
					"Log"};

char*	SimStatus[] = {"(Suspended)", "(Executing)", "(Terminated)"};
char	SimErrorFile[] = {"SimError.log"};
HANDLE	hLogo = 0, ahBmpDevice = 0, ahBmpDevice2 = 0;
HMENU	hMenuPopup = 0;									//右键单出控制开启与暂停菜单...
HWND	hWndMain = 0, hWndLCD1 = 0, LOG_hWnd = 0;
//motion log
HWND	hWnd_MotionLog = 0;
//main task log
HWND	hWnd_MainTaskLog = 0;
//Motion Data Disp
HWND    HWnd_MotionDisp = 0;

DWORD	rgbTransparent = 0;
//HWND	LCD_aWinData
int		CmdKill = 0;
HANDLE	ghThread = 0;
int		SuspendCount = 0;
int		NumTask = 0;
HANDLE	ahThread[TOTAL_TASK];
DWORD	aThreadID[TOTAL_TASK];
int		KeyBuffer = 0;
HANDLE	SimFileMap = 0;

char	LOG_acBuffer[MAXLOGLEN];

//Motion Log Buffer   缓冲
char	MotionLog_Buffer[MAXLOGLEN];

char	MainTaskLog_Buffer[MAXLOGLEN];

KeyStruc	aHardkey[MAXKEY];
int			NumHardkeys = 0;
LCDScreen	LCD_aWinData[MAXLCDNUM];
KeyData		aVKey2Key[] = {{VK_UP, GUI_KEY_UP}, {VK_DOWN, GUI_KEY_DOWN},{VK_LEFT, GUI_KEY_LEFT}, \
							{VK_RIGHT, GUI_KEY_RIGHT}, {VK_HOME, GUI_KEY_HOME}, {VK_END, GUI_KEY_END}, {VK_SHIFT, GUI_KEY_SHIFT},\
							{VK_CONTROL, GUI_KEY_CONTROL},{VK_BACK, GUI_KEY_BACKSPACE},{VK_INSERT, GUI_KEY_INSERT},{VK_DELETE, GUI_KEY_DELETE}};
//VK_SHIFT

HINSTANCE	hInst;											// current instance
TCHAR	szTitle[MAX_LOADSTRING];							// The title bar text
TCHAR	szWindowClass[MAX_LOADSTRING];						// The title bar text

ATOM	RegisterClasses(HINSTANCE hInstance);
BOOL	InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProcMain(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProcLCD(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

void	MainWnd_Command(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
//void	MainWnd_Paint(HWND hwnd);
void	MainWnd_Paint(HWND hwnd, HDC hdc);
void	MainWnd_OnTimer(HWND hWnd);
void	CreateWndLCD();
int		FindFreeLCDIndex();
void	InitMenu(HMENU hMenu);
void	HandleKeyEvents(UINT message, WPARAM wParam);
//void	LOG_AddPlain(const char* log);
void LOG_AddPlain(const char* log,char * LogBuffer, HWND DispHWin);
void	LogTime();
void	LOG_Add(const char* log);
void	LOG_DelFirstLine(LOG_acBuffer);
void UpDataHALDisp(void);


// 2005-5-31 23:23:02
int		SIM_GetTime();
int		SIM_SetMag(int simmag);
void	SIM_Delay(int delaytime);
void	SIM_ExecIdle();

void	THREAD_Sleep(int delaytime);
void	THREAD_KillAll();
void	THREAD_StartApplication();
void	THREAD_ContinueApplication();
void	THREAD_StopApplication();
void	Thread();
void	CreateTask(void * pFunc);


//初始化之前并做清除...
int APIENTRY	WinMain_NoClean(HINSTANCE hInstance,
									HINSTANCE hPrevInstance,
									LPSTR     lpCmdLine,
									int       nCmdShow);

int APIENTRY	WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	int ret = 0, i = 0;
//	char curPath[255];
//SIM初始化...
	SIM_X_Init();

//初始化汉字库2005-6-12 23:53:15
/* 2005-06-17 9:07:46
	GetCurrentDirectory(255, curPath);
	for(i = strlen(curPath); i > 0; i--){
		if(curPath[i] == '\\'){
			curPath[i+1] = 0;
			break;
		}
	}
	strcat(curPath, "GUI\\GUISource\\Chinese\\hzk16");
	OpenHZ(curPath);*/
/*
	i = OpenHZ("..\\..\\GUI\\GUISource\\Chinese\\hzk16");
	if(i == -1){
		do{
			i = OpenHZ(curPath);
			if(i != -1)	break;
			sprintf(curPath, "%s\n加载汉字库hzk16出错!", curPath);;
			MessageBox(0, curPath, "error", 0);
		}while(0);
	}
*/
	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_EMWIN, szWindowClass, MAX_LOADSTRING);

	ret = WinMain_NoClean(hInstance, hPrevInstance, lpCmdLine, nCmdShow);

	if(hLogo != 0) DeleteObject(hLogo);	//加载所有用的位图与图标...o
	if(ahBmpDevice != 0) DeleteObject(ahBmpDevice);
	if(ahBmpDevice2 != 0)  DeleteObject(ahBmpDevice2);
	if(SimFileMap != 0)  CloseHandle(SimFileMap);
//	if(i != -1)	CloseHZ();
	return ret;
}




///////////////////////////////////////////////////////////////////////
//
// 函数名       : FUNCTION: MyRegisterClass
// 功能描述     :
// 返回值       : ATOM
//
//
///////////////////////////////////////////////////////////////////////

ATOM RegisterClasses(HINSTANCE hInstance)
{
	ATOM ret = 1;
	WNDCLASSEX wcex;

//模拟器主窗口类...
	//IDC_ARROW=7f00;BLACK_BRUSH
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProcMain;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_ICON1);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_APPWORKSPACE+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_EMWIN;
	wcex.lpszClassName	= SimClassName[SimulationMainframe];
//	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_ICON1);
	wcex.hIconSm		= 0;

	ret = ret | RegisterClassEx(&wcex);

//模拟器硬件设备模拟窗口类...
	wcex.style			= 0;
	wcex.lpfnWndProc	= (WNDPROC)WndProcMain;
	wcex.lpszMenuName	= (LPCSTR)0;
	wcex.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	wcex.lpszClassName	= SimClassName[emWinDevice];

	ret = ret | RegisterClassEx(&wcex);

//模拟器硬件按键模拟窗口类...
	wcex.style			= 0;
	wcex.lpfnWndProc	= (WNDPROC)WndProcMain;
	wcex.lpszMenuName	= (LPCSTR)0;
	wcex.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	wcex.lpszClassName	= SimClassName[emWinHardkey];
	ret = ret | RegisterClassEx(&wcex);

//模拟器显示屏模拟窗口类(LCD)...
	wcex.style			= 0;
	wcex.lpfnWndProc	= (WNDPROC)WndProcLCD;
	wcex.lpszMenuName	= (LPCSTR)0;
	wcex.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	ret = LCD;
	wcex.lpszClassName	= SimClassName[LCD];
	ret = ret | RegisterClassEx(&wcex);

//模拟器调色板信息显示窗口类(LCD Info)...
	wcex.style			= 0;
	wcex.lpfnWndProc	= (WNDPROC)WndProcMain;
	wcex.lpszMenuName	= (LPCSTR)0;
	wcex.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	wcex.lpszClassName	= SimClassName[LCDInfo];
	ret = ret | RegisterClassEx(&wcex);

//模拟器LOG信息显示窗口类(LOG)...
	wcex.style			= 0;
	wcex.lpfnWndProc	= (WNDPROC)WndProcMain;
	wcex.lpszMenuName	= (LPCSTR)0;
	wcex.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	wcex.lpszClassName	= SimClassName[Log];
	ret = ret | RegisterClassEx(&wcex);

	return ret;
}


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(SimClassName[SimulationMainframe], szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}


void InitMenu(HMENU hMenu)
{
	if(ghThread != 0)
	{
		EnableMenuItem(hMenu, IDM_NEWLCD, MF_ENABLED);
		if(SuspendCount != 0){
			EnableMenuItem(hMenu, IDM_PAUSEAPP, MF_DISABLED | MF_GRAYED);
			EnableMenuItem(hMenu, IDM_RESUMEAPP, MF_ENABLED);
		}else{
			EnableMenuItem(hMenu, IDM_PAUSEAPP, MF_ENABLED);
			EnableMenuItem(hMenu, IDM_RESUMEAPP, MF_DISABLED | MF_GRAYED);
		}
	}else{
		EnableMenuItem(hMenu, IDM_RESUMEAPP, MF_DISABLED | MF_GRAYED);
		EnableMenuItem(hMenu, IDM_PAUSEAPP, MF_DISABLED | MF_GRAYED);
	//	EnableMenuItem(hMenu, IDM_NEWLCD, MF_DISABLED | MF_GRAYED);
	}
}


LRESULT CALLBACK WndProcMain(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	RECT rt;

	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	switch (message)
	{
		case WM_CREATE:
			SetTimer(hWnd, 0, 20, 0);
			break;
		case WM_TIMER:
		//	MainWnd_OnTimer(hWnd);
			break;
		case WM_INITMENU:
			InitMenu((HMENU)wParam);
			break;
		case WM_KEYDOWN:
		case WM_KEYUP:
			HandleKeyEvents(message, wParam);
			break;
		case WM_COMMAND:
			MainWnd_Command(hWnd, message, wParam, lParam);
			break;
			wmId    = LOWORD(wParam);
			wmEvent = HIWORD(wParam);
			// Parse the menu selections:
			switch (wmId)
			{
			case IDM_ABOUT:
				DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				break;
			case IDM_EXIT:
				DestroyWindow(hWnd);
				break;
			case IDM_NEWLCD:
				CreateWndLCD();
				break;

			default:
				return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;

		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			GetClientRect(hWnd, &rt);
			rt.top+= 70;
			rt.bottom+= 100;

			DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
			MainWnd_Paint(hWnd, hdc);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			KillTimer(hWnd, 0);
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}


///////////////////////////////////////////////////////////////////////
//
// 函数名       : WndProcLCD
// 功能描述     : LCD模拟屏幕消息处理函数, 重要一点是处理重画, 以及关于窗口
//				: 画面的更新,即隔多少秒要重画一次, 另外还有MOUSE消息处理...
// 参数         : HWND hWnd
// 参数         : UINT message
// 参数         : WPARAM wParam
// 参数         : LPARAM lParam
// 返回值       : LRESULT CALLBACK
//
///////////////////////////////////////////////////////////////////////
//extern void  timerfunc(void);
LRESULT CALLBACK WndProcLCD(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int xPos = -1, yPos = -1;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	RECT rt;
	POINT TrackMenuPoint = {0,0};
	int i = 0;
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	switch (message)
	{
		case WM_CREATE:
			SetTimer(hWnd, FindFreeLCDIndex(), 20, 0);
			break;
		case WM_TIMER:
//            timerfunc();//利用刷新LCD显示的定时器来完成PIC GUI的计时
			if(wParam <= 16){
				do{
					if(LCD_aWinData[wParam].TimerID == LCDSIM_GetModifyCnt())	break;
					InvalidateRect(hWnd, 0, 0);
					LCD_aWinData[wParam].TimerID = LCDSIM_GetModifyCnt();
					break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -