SDK message Loop Mechanism

Source: Internet
Author: User

In Windows, each thread can have a message queue. Generally, the UI thread has its own message queue by default. The work thread needs to call peekmessage to create its own message queue. A message is a data structure defined below: typedef struct tagmsg {hwnd; uint message; wparam; lparam; DWORD time; point pt; # ifdef _ Mac DWORD lprivate; # endif} MSG each message contains the handle of the window to receive the message, the Message ID, two parameters, and other information. The message loop in the SDK is usually written as follows: While (getmessage (& MSG, null, 0, 0) {If (! Translateaccelerator (MSG. hwnd, hacceltable, & MSG) {translatemessage (& MSG); dispatchmessage (& MSG) ;}} unless the getmessage function obtains the wm_quit message from the message queue, otherwise, the loop will continue forever. Inside the loop, the accelerator key and Keyboard Message are processed first, and then the message is sent to the specified window. Because the message itself contains the window handle, the destination does not need to be explicitly specified. When a message is received in the target window, the window procedure function is executed and processed based on different messages. Because the window procedure function and the window class name have a corresponding relationship when registering the window. When creating a window, we use the window class name again. Therefore, we can infer that the window handle and Window Process letter obtained after the window is created are mapped. The following is a typical window procedure function: lresult callback wndproc (hwnd, uint message, wparam, lparam) {int wmid, wmevent; paintstruct pS; HDC; switch (Message) {Case wm_command: wmid = loword (wparam); wmevent = hiword (wparam); // parse the menu selections: Switch (wmid) {Case idm_about: dialogbox (hinst, makeintresource (idd_aboutbox), hwnd, about); break; Case idm_exit: destroywindow (hwnd); break; default: Return defwindowproc (hwnd, message, wparam, lparam) ;}break; Case wm_paint: HDC = beginpaint (hwnd, & PS); // todo: add any drawing code here... endpaint (hwnd, & PS); break; Case wm_destroy: postquitmessage (0); break; default: Return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.