Interesting discovery of Windows message mechanism (2)

Source: Internet
Author: User
Tags win32 window

3. A Window Function and window process function without message LoopsProgram

A program, if we create a window, also defines the window process function, but does not establish a message loop what will happen? In the Win32 console project, we write the following:Code:

# Include <windows. h> # define wm_test 10000 lresult callback wndproc (hwnd, uint, wparam, lparam); void main () {static tchar szappname [] = text ("test! "); Hwnd; wndclass = {null}; wndclass. lpfnwndproc = wndproc; wndclass. style = cs_hredraw | cs_vredraw; wndclass. lpszclassname = szappname; registerclass (& wndclass); hwnd = createwindow (szappname, text ("the test program"), struct, cw_usedefault, null, null, null, null); showwindow (hwnd, sw_show); updatewindow (hwnd); System ("pause");} lresult callback wndp ROC (hwnd, uint message, wparam, lparam iparam) {Switch (Message) {Case wm_test: MessageBox (null, text ("message response! "), Text (" message response! "), Mb_ OK); return 50;} return defwindowproc (hwnd, message, wparam, iparam );}

This is a simple Win32 window program, but we have not defined the processing of wn_paint messages in the window procedure function wndproc. After the window is created in the main function, no message loop is created, what will happen after running the program?

The operation found that the window was indeed created. However, when you move the mouse over the form, you will find that the form has no response. This is because all messages including the self-painted message wm_paint are put into the thread message queue, but we do not have a message loop! Messages in the message queue are not taken out, nor are these messages processed. We do not even have the message processing function corresponding to wm_paint in the window process. The interface will naturally be suspended.

4. Another difference between sendmessage and postmessage

I have written a blog post saying that sendmessage and postmessage are not returned after sendmessage is processed. if the message is not processed after sendmessage is called, sendmessage will be blocked until the processing is complete. However, postmessage is not blocked and will be returned directly without waiting for processing results. In fact, there is another difference between them: the messages sent by postmessage will enter the message queue for extraction, while the messages sent by sendmessage will not be sent to the message queue and will be directly handed over to the window function for processing. To verify this statement, we compile the following code:

 # include 
  
    # define wm_test 10000 lresult callback wndproc (hwnd, uint, wparam, lparam); void main () {static tchar szappname [] = text ("test! "); Hwnd; wndclass = {null}; wndclass. lpfnwndproc = wndproc; wndclass. style = cs_hredraw | cs_vredraw; wndclass. lpszclassname = szappname; registerclass (& wndclass); hwnd = createwindow (szappname, text ("the test program"), struct, cw_usedefault, null, null, null, null); showwindow (hwnd, sw_show); updatewindow (hwnd); int I = sendmessage (hwnd, wm_test, null, Null); System ("pause");} lresult callback wndproc (hwnd, uint message, wparam, lparam iparam) {Switch (Message) {Case wm_test: MessageBox (null, text ("message response! "), Text (" message response! "), Mb_ OK); return 50;} return defwindowproc (hwnd, message, wparam, iparam) ;}
  

After creating the window, we sent a wm_test custom message with sendmessage. In the window procedure function wndproc, we defined the processing method for wm_test to pop up a MessageBox and return 50.

After running, it is found that even if the created form is still in the suspended state, MessageBox is displayed, and the return value of sendmessage is 50. The code for processing the wm_test message defined in wndproc is successfully executed! Note that there is no message loop in this program, but the messages we send with sendmessage are still processed by the window process function wndproc.

Change sendmessage to postmessage and try again. MessageBox is not displayed after running. Why? Because the message sent by postmessage is to be sent to the message queue, but there is no message loop, there is no function such as getmessage to retrieve the message from the message queue, and dispatchmessage is not used for sending messages, therefore, wm_test sent with postmessage is still in the message queue. The code in wndproc is not executed.

This proves another difference between sendmessage and postmessage:The message sent by sendmessage is directly sent to the corresponding window process function for processing and is not sent to the Message Queue. The message sent by postmessage must be sent to the Message Queue for delivery and processing.

Related Article

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.