1,Message Map雏形之实现
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LONG OnCreate(HWND,UINT,WPARAM,LPARAM);
LONG OnPaint(HWND,UINT,WPARAM,LPARAM);
LONG OnSize(HWND,UINT,WPARAM,LPARAM);
LONG OnCommand(HWND,UINT,WPARAM,LPARAM);
LONG OnSetFocus(HWND,UINT,WPARAM,LPARAM);
LONG OnClose(HWND,UINT,WPARAM,LPARAM);
LONG OnDestroy(HWND,UINT,WPARAM,LPARAM);
LONG OnAbout(HWND,UINT,WPARAM,LPARAM);
LONG OnExit(HWND,UINT,WPARAM,LPARAM);
struct MSGMAP_ENTRY
{//消息映射表表项
UINT nMessage;
LONG (*pfn)(HWND,UINT,WPARAM,LPARAM);
};
MSGMAP_ENTRY _messageEntries[]=
{
WM_CREATE,OnCreate,
WM_PAINT,OnPaint,
WM_SIZE,OnSize,
WM_COMMAND,OnCommand,
WM_SETFOCUS,OnSetFocus,
WM_CLOSE,OnClose,
WM_DESTROY,OnDestroy
};
MSGMAP_ENTRY _commandEntries[] =
{
IDM_ABOUT,OnAbout,
IDM_EXIT,OnExit
};
LONG OnCreate(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
{
return 0;
}
LONG OnPaint(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd, &ps);
::TextOut(hdc,10,10,_T("hello"),5);
EndPaint(hWnd, &ps);
return 0;
}
LONG OnSize(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
{
return 0;
}
LONG OnCommand(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
{//处理command
int i,wmId,wmEvent;
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
int numEntries = sizeof(_commandEntries)/sizeof(_commandEntries[0]);//命令映射表表项数目
for (i=0;i
{
if (_commandEntries[i].nMessage==wmId)
{//命令匹配
return (*_commandEntries[i].pfn)(hWnd,nMessage,wParam,lParam);//调用处理函数
}
}
if (i==numEntries)
{
return DefWindowProc(hWnd,nMessage,wParam,lParam);
}
return 0;
}
LONG OnSetFocus(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
{
return 0;
}
LONG OnClose(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
{
DestroyWindow(hWnd);
return 0;
}
LONG OnDestroy(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
{
PostQuitMessage(0);
return 0;
}
LONG OnAbout(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
{
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
return 0;
}
LONG OnExit(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
{
DestroyWindow(hWnd);
return 0;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_CH_1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CH_1));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i;
int numEntries = sizeof(_messageEntries)/sizeof(_messageEntries[0]);//消息映射表表项数目
for (i=0;i
{
if (_messageEntries[i].nMessage==message)
{//消息匹配
return (*_messageEntries[i].pfn)(hWnd,message,wParam,lParam);//调用处理函数
}
}
if (i==numEntries)
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
2,空闲时间的处理
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
OnIdle();
}
}
3, PostMessage的使用
LONG OnDestroy(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
{
::PostMessage(hWnd,WM_QUIT,NULL,NULL);
return 0;
}
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/01/18/1044829.html,如需转载请自行联系原作者