#pragma warning(disable:4786)
#include <windows.h>
#include <map>
using namespace std;
#define WINDOW_CLASS "SDK"
#define WINDOW_NAME "SDK class"
// 定义消息处理函数的类型
typedef LRESULT (*FUN_TYPE)(HWND, WPARAM, LPARAM);
// 实现消息到函数的映射
static std::map <DWORD, FUN_TYPE> test;
// 定义一组消息响应函数
template <UINT msg>
inline LRESULT Fun(HWND hWnd, WPARAM wParam, LPARAM lParam);
template <>
inline LRESULT Fun <WM_DESTROY> (HWND hWnd, WPARAM wParam, LPARAM lParam)
{
PostQuitMessage(0);
return DefWindowProc(hWnd, WM_DESTROY, wParam, lParam);
};
template <>
inline LRESULT Fun <WM_LBUTTONUP> (HWND hWnd, WPARAM wParam, LPARAM lParam)
{
MessageBox(NULL,NULL,"WM_LBUTTONUP",MB_OK);
return DefWindowProc(hWnd, WM_LBUTTONUP, wParam, lParam);
};
// 对需要处理的消息进行初始化
void init()
{
test[WM_DESTROY] = Fun <WM_DESTROY> ;
test[WM_LBUTTONUP] = Fun <WM_LBUTTONUP> ;
}
// 窗口过程
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
/*std::*/
map<DWORD,FUN_TYPE>::iterator pos;
pos = test.find(msg); // 察看消息及其处理函数是否在映射中存在
if(pos != test.end())
return test[msg](hWnd, wParam, lParam); // 如果存在则调用消息处理函数
return DefWindowProc(hWnd, msg, wParam, lParam); // 不存在则调用默认窗口过程
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
init(); // 初始化,消息到处理函数的映射
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WindowProc,
0, 0, GetModuleHandle(NULL), NULL, NULL,
NULL, NULL, WINDOW_CLASS, NULL };
RegisterClassEx(&wc);
// Create the application's window
HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME,
WS_OVERLAPPEDWINDOW|WS_VISIBLE, 100, 100,
640, 480, GetDesktopWindow(), NULL,
wc.hInstance, NULL);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message != WM_QUIT)
{
if(GetMessage(&msg, NULL, 0U, 0U))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
}
}
return 0;
}