Windows程序消息机制浅析

时间:2023-03-09 09:58:48
Windows程序消息机制浅析

1、消息

    消息是由MSG结构体来表示的。如下:

 typedef struct tagMSG {
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG

2、WinMain函数的定义

WinMain函数的原型声明如下:

 int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
);

3、窗口的创建:设计一个窗口类、注册窗口类、创建窗口、显示及更新窗口。

设计一个窗口:WNDCLASS结构体的定义如下:

 typedef struct _WNDCLASS {
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
} WNDCLASS;

注册窗口类:注册函数的原型声明如下:

 ATOM RegisterClass(
CONST WNDCLASS *lpWndClass // class data
);

创建窗口:CreateWindow函数的原型声明如下:

 HWND CreateWindow(
LPCTSTR lpClassName, // registered class name
LPCTSTR lpWindowName, // window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // menu handle or child identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam // window-creation data
);

更新及更新窗口:显示窗口ShowWindow函数的原型声明如下:

 BOOL ShowWindow(
HWND hWnd, // handle to window
int nCmdShow // show state
);

更新及更新窗口:更新窗口UpdateWindow函数的原型声明如下:

 BOOL UpdateWindow(HWND hWnd);

4、消息循环:不断从消息队列中取出消息,并进行响应。

 BOOL GetMessage(
LPMSG lpMsg, // message information
HWND hWnd, // handle to window
UINT wMsgFilterMin, // first message
UINT wMsgFilterMax // last message
);

Windows应用程序消息处理机制如下图所示:

  Windows程序消息机制浅析

Windows程序消息机制浅析

  1. 操作系统接收到应用程序的窗口消息,将消息投递到应用程序的消息队列中。

  2. 应用程序在消息循环中调用GetMessage函数从消息队列中取出一条一条的消息。取出消息后,应用程序可以对消息进行一些预处理,例如,放弃对某些消息的响应,或者调用TranslateMessage产生新的消息。

  3. 应用程序调用DispatchMessage,将消息回传给操作系统。消息是由MSG结构体对象来表示的,其中就包含了接收消息的窗口的句柄。因此,DispatchMessage函数总能进行正确的传递。

  4. 系统利用WNDCLASS结构体的lpfnWndProc成员保存的窗口过程函数的指针调用窗口过程,对消息进行处理(即“系统给应用程序发送了消息”)。

5、编写窗口过程函数:窗口过程函数的声明形式如下:

 LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
提示:系统通过窗口过程函数的地址(指针)来调用窗口过程函数,而不是名字。 
例:WinMain.cpp
 #include <windows.h>
#include <stdio.h> LRESULT CALLBACK WinSunProc(
                HWND hwnd, // handle to window
                UINT uMsg, // message identifier
                WPARAM wParam, // first message parameter
                LPARAM lParam // second message parameter
                ); int WINAPI WinMain(
          HINSTANCE hInstance, // handle to current instance
          HINSTANCE hPrevInstance, // handle to previous instance
          LPSTR lpCmdLine, // command line
          int nCmdShow // show state
           )
{
WNDCLASS wndcls;
wndcls.cbClsExtra=;
wndcls.cbWndExtra=;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="bedrock32";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls); HWND hwnd;
hwnd=CreateWindow("bedrock32","http://www.cnblogs.com/bedrock32",WS_OVERLAPPEDWINDOW,
,,,,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd); MSG msg;
while(GetMessage(&msg,NULL,,))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} LRESULT CALLBACK WinSunProc(
                HWND hwnd, // handle to window
                UINT uMsg, // message identifier
                WPARAM wParam, // first message parameter
                LPARAM lParam // second message parameter
                )
{
switch(uMsg)
{
case WM_CHAR:
char szChar[];
sprintf(szChar,"char code is %d",wParam);
MessageBox(hwnd,szChar,"char",);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,,,"我是高手",strlen("我是高手"));
//ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,,,"http://www.cnblogs.com/bedrock32",strlen("http://www.cnblogs.com/bedrock32"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage();
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return ;
}
Windows程序消息机制浅析