📄 emwin.c
字号:
// 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;
#define DELAYEVERYTIME 10 //每次延时的时间(sleep())...
#define MAX_LOADSTRING 100
#define TOTAL_TASK 4 //支持多任务时的多任务数...
#define MAXKEY 80 //按键的个数...
#define MAXLCDNUM 16 //最大可打开LCD的个数...
//以下为用到到部分全局变量...
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;
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;
KeyStruc aHardkey[MAXKEY];
int NumHardkeys = 0;
typedef struct tagLCD{
HWND lcd_hwnd;
int TimerID;
}LCDScreen;
LCDScreen LCD_aWinData[MAXLCDNUM];
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);
/* 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
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);
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_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
//
///////////////////////////////////////////////////////////////////////
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:
if(wParam <= 16){
do{
if(LCD_aWinData[wParam].TimerID == LCDSIM_GetModifyCnt()) break;
InvalidateRect(hWnd, 0, 0);
LCD_aWinData[wParam].TimerID = LCDSIM_GetModifyCnt();
break;
}while(0);
}
else{
}
LCDSIM_CheckMouseState();
//if(ghThread == 0) PostQuitMessage(0);
break;
/* 2005-6-8 23:32:58
#define WM_MOUSEFIRST 0x0200
#define WM_MOUSEMOVE 0x0200
#define WM_LBUTTONDOWN 0x0201
#define WM_LBUTTONUP 0x0202
cmp eax, 1FFh //此处处理所有三种MOUSE消息, 在反编译中容易看漏的, 如下所示...
jbe loc_55D4 //小于等于0x1ff跳往DefWindowProc处理...
cmp eax, 202h //msg小于0x202则转往处理MOUSE消息...
jbe short loc_557E */
case WM_MOUSEMOVE:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
xPos = LOWORD(lParam);
yPos = HIWORD(lParam);
if(xPos >= 0 && yPos >=0 && xPos <= LCD_GetDevCap(LCD_DEVCAP_XSIZE) && yPos <= LCD_GetDevCap(LCD_DEVCAP_YSIZE)){
LCDSIM_SetMouseState(xPos, yPos, wParam);
}else{
LCDSIM_SetMouseState(-1, -1, 0);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
if(ps.fErase != 0){
rt.left = 0;
rt.top = 0;
rt.right = 0xfff;
rt.bottom = 0xfff;
FillRect(hdc, &rt, GetStockObject(BLACK_BRUSH));
}
//InvalidateRect(hWnd,NULL, 0); //此句不可丢,否则无法显示出要画的位图, 改在定时器中刷新窗口....
// LCDSIM_CheckInit(hWnd);
LCDSIM_Paint(hdc);
EndPaint(hWnd, &ps);
break;
case WM_RBUTTONDOWN:;
TrackMenuPoint.x = LOWORD(lParam);
TrackMenuPoint.y = HIWORD(lParam);
ClientToScreen(hWnd, &TrackMenuPoint);
TrackPopupMenu(hMenuPopup, TPM_RIGHTBUTTON, TrackMenuPoint.x, TrackMenuPoint.y, 0, hWndMain, 0);
break;
case WM_DESTROY:
for(i = 0; i < MAXLCDNUM; i++){
if(hWnd == LCD_aWinData[i].lcd_hwnd){
LCD_aWinData[i].lcd_hwnd = 0;
LCD_aWinData[i].TimerID = 0;
break;
}
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
char buf[500];
switch (message)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -