Into the world of Windows programming-----Message processing functions (1)

Source: Internet
Author: User

WIN32 message mechanismprocess-driven: programs are executed in the order that we pre-defined them, and each step is executed in the order in which they are executed until the end of the program. Event-driven: The order in which programs are executed is unordered. The code executed at a certain point in time is notified by the outside world. Since we cannot determine the order in which the programs are executed. So the execution of the code is also unordered. WIN32 Basic MessagesWm_destroy:
Window destroyed when the message can be done to exit or aftercare processingWm_create:
Window creates a message that is the first message that the window handler receives after the window is created
In this message, you can initialize or wear a child window.
WPARAM WPARAM-Not used
LPARAM lparam-createstruct Hands
Wm_size:
This message is received when the window size changes.
You can adjust the layout of the window in this message
Wm_syscommand:
System command messages that are received when you click on the System menu and buttons
In this message, you can prompt the user to save the data, etc.
WM_PAINT:
Drawing messages
Keyboard message:
Mouse messages
Wm_time: Timer message getting and sending messages
Get Getmessage/peekmessage
GetMessage getting messages, blocking functions
peekmessage Get message, non-blocking function
Send Sendmessage/postmessage
SendMessage sends a message and waits for the end of the message processing to return.
PostMessage returns immediately after the message is sent, and does not care about the results of the message processing.
LRESULT Sendmessage/postmessage (          hwnd hwnd,      //Processing message window          UINT MSG,       //message ID          WPARAM WPARAM,  // The parameters of the message          LPARAM LPARAM);//Message parameters

3 Message composition and classification
3.1 Message composition
Window handle/Message ID/message parameter (Wparam.lparam)
3.2 Message Classification
3.2.1 System messages-messages defined and used by the system
Example: Wm_create/wm_size
The message ID range is: 0-0x03ff (wm_user-1)
3.2.2 User-defined messages-messages that the application can define and use itself, Wm_user (0x0400)
Starting with the ID of Wm_user, to 0X7FFF, is the message that the user can define to use.
3.2.3 Other message Ranges
Wm_app (0x8000) -0XBFFF: Message ID of the Application Access window
0XC000-0XFFFF: Application access message, using string registration system to generate the corresponding message ID
3.2.4 use of user-defined messages
1) define a custom message ID:

#define   Wm_firstmsg  (wm_user+1)

2) in the window processing function, the response message

 switch   (nmsg) { case  Span style= "margin:0px; padding:0px; line-height:1.5; " > wm_firstmsg:  //   processing function  break  ;}  

3) sendmessage/postmessage Send Message

    0 0 );

4 Message Queuing
4.1 Message Queuing-the memory space in which messages are stored, and messages are first-in-first-out in the queue.
4.2 Classification of message queues
4.2.1 System Message Queuing-Message Queuing maintained by the system.
4.2.2 Application Message Queuing (thread message-to-column)-belongs to each thread's own message queue.

5 message and Message Queuing
5.1 Messages are divided into two types based on message and Message Queuing relationships:
Queue message-A message that can be stored in a message queue.
Non-queue message-does not enter Message Queuing when it is sent.
5.2 Queue Messages
It is first stored in the message queue and then taken out by getmessage/peekmessage and processed.
Example: Mouse message/keyboard message/wm_paint/wm_quit/m_timer message
5.3 Non-queue messages
The message is sent directly to the specified window, finds the window's handler function, and returns the processing result.

6 Getting the message
6.1 Message Loops
6.1.1 Getmesssage Gets the message from the queue, determines if it is a wm_quit message, and if it finds a wm_quit message, the message loop ends, otherwise proceed to the next step.
6.1.2 TranslateMessage translate the key message, if you find a keystroke message, generate a character message into the message pair column, continue to the next step
6.1.3 DispatchMessage finds the processing function of the window that the message was sent to process the message. When processing is complete, return to 6.1.1.
6.2 Getmesssage and PeekMessage
6.2.1 Gets the message from the thread message queue, returns a message if it finds a message, and processes the message. If no message is found, execute 6.2.2
6.2.2 finds the system message queue. By querying the system message queue, if a message is found, the message is retrieved and returned, and the message is processed. If no message is found, execute 6.2.3
6.2.3 checks the extent that the window needs to be redrawn and generates a WM_PAINT message if a redrawn scope is found. Then message processing, if not found, executes the 6.2.4
The 6.2.4 checks the WM_TIMER timer message and generates a WM_TIMER message if a timer is found to be present. Message processing. If not found, execute 6.2.5
6.2.5 perform memory management work.
6.2.6 depending on the function, the processing results are different:
Getmesssage-blocking, waiting for the next message
PeekMessage-Give up control and hand it over to the following code execution.

7 message Sent
7.1 message is sent in two kinds of
Send message-sent directly to the specified window and waits for the result.
Post messages-sent to the message queue, returned immediately, and processed by the message loop.
7.2 PostMessage and SendMessage
PostMessage generates a queue message and cannot determine whether the message was processed successfully because it did not wait for the message processing result after it was sent.
SendMessage generates a non-queue message to determine whether the message was successful. 

look at the following code example: 
/*file:message.cpp *auth:sjin *date:20140519 *mail: [email protected] */#include <iostream> #include < windows.h> #include <stdio.h>hinstance g_hinst = NULL; HWND G_button = NULL;                         LRESULT CALLBACK WndProc (HWND hwnd, UINT nmsg, WPARAM WPARAM,    LPARAM LPARAM);/* Registration window */bool Registerwnd (LPSTR pszclassname) {wndclassex WCE = {0};    Wce.cbclsextra = 0;    wce.cbsize = sizeof (WCE);    Wce.cbwndextra = 0;    Wce.hbrbackground = Hbrush (color_btnface+1);    Wce.hcursor = NULL;    Wce.hicon = NULL;    WCE.HICONSM = NULL;    Wce.hinstance = G_hinst;    Wce.lpfnwndproc = WndProc;    Wce.lpszclassname = Pszclassname;    Wce.lpszmenuname = NULL;    Wce.style = Cs_hredraw|cs_vredraw;    ATOM Natom = RegisterClassEx (&AMP;WCE);    if (0 = = Natom) return FALSE; return TRUE;} /* Create window */hwnd createwnd (LPSTR pszclassname) {hwnd hwnd = CREATEWINDOWEX (0, Pszclassname, "Mywnd", Ws_overlappedwi Ndow, Cw_usedefault, Cw_usedefault,cw_usedefault,cw_usedefault, null,null,g_hinst,null); return hWnd;}    /* Display window */void Displaywnd (HWND hwnd) {ShowWindow (hwnd, sw_show); UpdateWindow (hWnd);} void Wm_create (HWND hwnd,uint nmsg, WPARAM wparam,lparam LPARAM) {lpcreatestruct pcreatestruct = lpcreatestruct (l Param);/* The name of the Print window pops up this dialog box, click OK after the pop-up window *///messagebox (NULL, Pcreatestruct->lpszname, "wm_create", MB_OK);/* Create a child window * /G_button = CreateWindowEx (0, "button", "button", ws_child| Ws_visible, 20,100,50, hwnd,null,g_hinst,null);} void Wm_size (HWND hwnd,uint nmsg, WPARAM wparam,lparam LPARAM) {int nwidth = LOWORD (LPARAM); int nheight = HiWord ( LParam); CHAR sztext[256] = {'};sprintf ' (Sztext, "w:%d; h:%d ", nwidth,nheight);//messagebox (NULL, Sztext," wm_size ", MB_OK);        if (NULL! = G_button) {int nX = (nWidth-100)/2;        int NY = (nHeight-100)/2;    MoveWindow (G_button, NX, NY, +, +, TRUE); }}/* executes the system command function to process the */bool Wm_syscommand (HWND Hwnd,uint nmsg, WPARAM wparam,lparam LPARAM) {switch (WPARAM) {case Sc_close:if (IDOK = = MessageBox (NULL, "Save File", "Hint", mb_okcancel| mb_iconwarning) {return TRUE;} else {return FALSE;} Break;default:break;} return FALSE;}                         /* Message handler function */lresult CALLBACK WndProc (HWND hwnd, UINT nmsg, WPARAM WPARAM,       LPARAM LPARAM) {switch (nmsg) {/*win 32 basic message Wm_destroy: The window is destroyed when the message can be done to exit or aftercare wm_create:       The window creates the message, which is the first message that the window handler receives after the window is created, either in the message, in the initialization or in the WPARAM WPARAM-not using the LPARAM lparam-createstruct pointer wm_size:   This message is received when the window size changes. You can adjust the layout of the window in this message wm_syscommand: System command message, when you click on the System menu and button will receive can in this message, prompting the user to save data such as WM_PAINT: Drawing message Keyboard message: Mouse message W M_time: Timer message */Case wm_destroy://window destroyed message */* Send wm_quit message to Window *///sendmessage (hWnd, wm_quit, 0, 0);/* Send message and wait for message processing to finish Returns */PostMessage (hWnd, wm_quit, 0, 0);/* Returns immediately after sending a message, not concerned with the results of the message processing *///postquitmessage (0); return 0;case wm_create: /* Create window */wm_creatE (Hwnd,nmsg,wparam,lparam); Break;case wm_size:/* window drag */wm_size (Hwnd,nmsg,wparam,lparam); Break;case WM_SYSCOMMAND :/* Execute system Command */if (!wm_syscommand (Hwnd,nmsg,wparam,lparam)) {return 0;}            Break } return DefWindowProc (HWnd, Nmsg, WParam, LParam);} /* Messages Loop */void message () {/* msg struct BODY parameter description typedef struct TAGMSG {//MSG HWND hwnd;//message window handle UINT message;// WPARAM wparam;//Message parameter LPARAM lparam;//message parameter DWORD time;//message time point pt;//message is generated, mouse position} MS    G      */msg msg = {0};/* BOOL GetMessage (lpmsg lpmsg,//holds the obtained message data by the system fills in the parameter about the message HWND hwnd,//gets the window handle of the message, can accept the specified window message   UINT wmsgfiltermin,//Message Filter Start message UINT Wmsgfiltermax//Message Filter termination message);   Return:true: The message was successfully obtained; FALSE: When getting to the wm_quit message. You can use PostQuitMessage to send wm_quit messages to a window GetMessage: Get message, block function PeekMessage: Get message, non-blocking function */while (GetMessage (&msg, NULL, 0 , 0)) {/*translatemessage: is to convert the keyboard message into a character message 1, first check whether it is a keyboard message 2, if found to be a key message, according to the key press, the resulting character message in the next GetMessage execution, received this message 3, if No keys foundMessage, did not do any processing */TranslateMessage (&msg);/*dispatchmessage according to the window handle inside the message data, find the window handler function, call the handler function, and do the message processing. If the HWND window handle is empty in MSG, DispatchMessage does not do any processing.    */DispatchMessage (&AMP;MSG); }}int apientry WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR LpC    mdline, int ncmdshow) {g_hinst = hinstance;    Registerwnd ("Mywnd");    HWND hwnd = Createwnd ("Mywnd");    Displaywnd (HWND);    Message (); 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.