windows程序设计 消息循环
2015-04-02 22:49
267 查看
The Message Loop
A programretrieves these messages from the message queue by executing a block of codeknown as the "message loop":
while (GetMessage (&msg, NULL, 0,0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
Themsgvariable is a structure of type MSG, which is defined in the WINUSER.Hheader file like this:
typedef struct tagMSG
{
HWND hwnd ;
UINT message ;
WPARAM wParam ;
LPARAM lParam ;
DWORD time ;
POINT pt ;
}
MSG, * PMSG ;
The POINTdata type is yet another structure, defined in the WINDEF.H header file likethis:
typedef struct tagPOINT
{
LONG x ;
LONG y ;
}
POINT, * PPOINT;
TheGetMessagecall that begins the message loop retrieves a message from themessage queue:
GetMessage (&msg, NULL, 0, 0)
This callpasses to Windows a pointer to a MSG structure named msg. The second, third,and fourth arguments
are set toNULL or 0 to indicate that the program wants all messages for all windowscreated by the program.
Windowsfills in the fields of the message structure with the next message from themessage queue. The fields of
thisstructure are:
hwnd Thehandle to the window which the message is directed to. In the HELLOWIN program,this is thesame as the hwndvalue returned from CreateWindow, because that's theonly window the program has.
message Themessage identifier. This is a number that identifies the message. For eachmessage, there is a corresponding identifier defined in the Windows headerfiles (most of them in WINUSER.H) that
begins with the identifier WM("window message"). For example, if you position the mouse pointerover HELLOWIN's client area and press the left mouse button, Windows will put amessage in the message queue with a messagefield equal to WM_LBUTTONDOWN, whichis
the value 0x0201.
wParam A32-bit "message parameter," the meaning and value of which depend onthe particular message.
lParamAnother 32-bit message parameter dependent on the message. time The time the message was placed in themessage queue.
If themessagefield of the message retrieved from the message queue is anything exceptWM_QUIT (which equals 0x0012), GetMessagereturns a nonzero value. A WM_QUITmessage causes GetMessageto return 0.
Thestatement:
TranslateMessage (&msg) ;
passes themsgstructure back to Windows for some keyboard translation. (I'll discuss thismore in Chapter 6.)
Thestatement
DispatchMessage (&msg) ;
again passesthe msgstructure back to Windows. Windows then sends the message to theappropriate window procedure for processing. What this means is that Windowscalls the window procedure. In HELLOWIN,
the window procedure is WndProc. AfterWndProcprocesses the message, it returns control to Windows, which is still servicingthe DispatchMessagecall. When Windows returns to HELLOWIN following theDispatchMessagecall, the message loop continues with the next
GetMessagecall.
A programretrieves these messages from the message queue by executing a block of codeknown as the "message loop":
while (GetMessage (&msg, NULL, 0,0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
Themsgvariable is a structure of type MSG, which is defined in the WINUSER.Hheader file like this:
typedef struct tagMSG
{
HWND hwnd ;
UINT message ;
WPARAM wParam ;
LPARAM lParam ;
DWORD time ;
POINT pt ;
}
MSG, * PMSG ;
The POINTdata type is yet another structure, defined in the WINDEF.H header file likethis:
typedef struct tagPOINT
{
LONG x ;
LONG y ;
}
POINT, * PPOINT;
TheGetMessagecall that begins the message loop retrieves a message from themessage queue:
GetMessage (&msg, NULL, 0, 0)
This callpasses to Windows a pointer to a MSG structure named msg. The second, third,and fourth arguments
are set toNULL or 0 to indicate that the program wants all messages for all windowscreated by the program.
Windowsfills in the fields of the message structure with the next message from themessage queue. The fields of
thisstructure are:
hwnd Thehandle to the window which the message is directed to. In the HELLOWIN program,this is thesame as the hwndvalue returned from CreateWindow, because that's theonly window the program has.
message Themessage identifier. This is a number that identifies the message. For eachmessage, there is a corresponding identifier defined in the Windows headerfiles (most of them in WINUSER.H) that
begins with the identifier WM("window message"). For example, if you position the mouse pointerover HELLOWIN's client area and press the left mouse button, Windows will put amessage in the message queue with a messagefield equal to WM_LBUTTONDOWN, whichis
the value 0x0201.
wParam A32-bit "message parameter," the meaning and value of which depend onthe particular message.
lParamAnother 32-bit message parameter dependent on the message. time The time the message was placed in themessage queue.
If themessagefield of the message retrieved from the message queue is anything exceptWM_QUIT (which equals 0x0012), GetMessagereturns a nonzero value. A WM_QUITmessage causes GetMessageto return 0.
Thestatement:
TranslateMessage (&msg) ;
passes themsgstructure back to Windows for some keyboard translation. (I'll discuss thismore in Chapter 6.)
Thestatement
DispatchMessage (&msg) ;
again passesthe msgstructure back to Windows. Windows then sends the message to theappropriate window procedure for processing. What this means is that Windowscalls the window procedure. In HELLOWIN,
the window procedure is WndProc. AfterWndProcprocesses the message, it returns control to Windows, which is still servicingthe DispatchMessagecall. When Windows returns to HELLOWIN following theDispatchMessagecall, the message loop continues with the next
GetMessagecall.
相关文章推荐
- windows程序设计1 消息循环
- Windows程序设计零基础自学_1_Windows程序消息循环机制
- windows程序设计——消息循环
- 利用Windows消息循环实现消息延迟和消息合并
- windows 消息循环机制
- Windows的消息队列与消息循环
- Windows程序设计零基础自学_2_Windows程序的显示和更新_之WM_PAINT消息
- 利用Windows消息循环,使窗体不能改变大小
- Windows的消息队列与消息循环
- 9.VC(custom)-windows消息机制(消息循环)
- .NET对Windows消息循环的封装
- windows消息循环原理实例总结
- 理解Windows消息循环
- Windows程序设计——消息对话框的使用小结
- 一步步学破解-windows消息循环原理实例总结(二)
- Windows事件驱动机制及消息循环
- Windows 程序设计之 ------窗口和消息 学习总结
- 窗口破坏过程与Windows消息循环
- 揭秘.NET对Windows消息循环的封装
- MFC Windows 程序设计->消息反射