// TestMessageMap.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include<Windows.h>
HINSTANCE hInst;
HWND hWnd;
MSG msg;
char lpszClassName[]="窗口";
char *ShowText;
//定义宏
#define DECLARE_MESSAGE_MAP()\
struct MSGMAP_ENTRY _messageEntres[];\
#define BEGIN_MESSAGE_MAP()\
struct MSGMAP_ENTRY _messageEntres[]=\
{\
#define ON_WM(messageID,msgFuc)\
messageID,msgFuc,
#define END_MESSAGE_MAP()\
};\
//定义消息映射表项结构******************************************
struct MSGMAP_ENTRY
{
UINT nMessage;
void (*pfn)(HWND,UINT,WPARAM,LPARAM);
};
//声明消息处理函数原型*****************************************
void On_LButtonDown(HWND,UINT,WPARAM,LPARAM);
void On_RButtonDown(HWND,UINT,WPARAM,LPARAM);
void On_Paint(HWND,UINT,WPARAM,LPARAM);
void On_Destroy(HWND,UINT,WPARAM,LPARAM);
/*
//声明消息映射表***********************************************
DECLARE_MESSAGE_MAP()
//实现消息映射表***********************************************
BEGIN_MESSAGE_MAP()
ON_WM(WM_LBUTTONDOWN,On_LButtonDown)
ON_WM(WM_PAINT,On_Paint)
ON_WM(WM_DESTROY,On_Destroy)
END_MESSAGE_MAP()
*/
//如果完全展开,就是如下形式,一目了然。
//声明消息映射表***********************************************
struct MSGMAP_ENTRY _messageEntres[];
//实现消息映射表***********************************************
struct MSGMAP_ENTRY _messageEntres[]=
{
WM_RBUTTONDOWN,On_RButtonDown,
WM_LBUTTONDOWN,On_LButtonDown,
WM_PAINT,On_Paint,
WM_DESTROY,On_Destroy,
};
//窗口函数******************************************************
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
int i;
int n=sizeof(_messageEntres)/sizeof(_messageEntres[0]);
for(i=0;i<n;i++)
{
if(message==_messageEntres[i].nMessage)
(*_messageEntres[i].pfn)(hWnd,message,wParam,lParam);
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
//鼠标左键单击消息处理函数的实现******************************************************************
void On_LButtonDown(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
if(message == WM_LBUTTONDOWN)
{
ShowText="Left Button Down !";
InvalidateRect(hWnd,NULL,1);
}
}
//鼠标左键单击消息处理函数的实现******************************************************************
void On_RButtonDown(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
if(message == WM_RBUTTONDOWN)
{
ShowText="Right Button Down !";
InvalidateRect(hWnd,NULL,1);
}
}
//重绘窗口用户区消息处理函数的实现**************************************************************
void On_Paint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
hdc=BeginPaint(hWnd,&ps);
TextOut(hdc,50,50,ShowText,15);
EndPaint(hWnd,&ps);
}
//销毁窗口消息处理函数的实现************************************************************************
void On_Destroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PostQuitMessage(0);
}
ATOM MyRegisterClass(HINSTANCE hInstance);//注册窗口函数
BOOL Create(HINSTANCE,int);//程序实例初始化函数
int Run();//消息循环函数
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MyRegisterClass(hInstance);
Create(hInstance,nCmdShow);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return Run();
}
//注册窗口函数的实现
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASS wc;
wc.style=0;
wc.lpfnWndProc=WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=lpszClassName;
return RegisterClass(&wc);
}
//创建窗口函数的实现
BOOL Create(HINSTANCE hInstance,int nCmdShow)
{
hWnd=CreateWindow(lpszClassName,
"Windows",
WS_OVERLAPPEDWINDOW,
100,100,640,480,
NULL,
NULL,
hInstance,
NULL);
return TRUE;
}
//消息循环函数的实现
int Run()
{
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}