Message mechanism for Windows
Implementation mechanism of the program
Process-driven-program execution is performed in a predetermined order
Event-driven-Program execution is unordered and the user randomly triggers the corresponding event as needed
Win32 window program is implemented in a time-driven manner, that is, the message mechanism
What is a message
When the system notifies the window to work, it uses the message to distribute (DispatchMessage) to the window's custom handler function Wndclassex.lpfnwndproc
Message composition:
Window handle
Message ID
Two parameters of the message such as a mouse click, two parameters are as follows:
The time the message was generated
The mouse position generated by the message
Window processing functions and messages
Each window must have a window-handling function
callback function: Define a function yourself, do not call to the operating system itself
LRESULT CALLBACK WindowProc (
HWND hwnd,//Window handle
UINT umsg,//Message ID
WPARAM WPARAM,//Message parameters
LPARAM LPARAM//Message Parameters
);
When the system notifies the window, the window handler function is called to pass the message ID and message parameters to the window handler
In the window handler, the message is not processed, using the default window handler function, such as Defwindowsproc
Message-related functions
1 getmessage-get message, can only crawl the message of this process
BOOL GetMessage (
Lpmsg lpmsg,//store gets to the message buff HWND hwnd,//window handle, crawl which window message, if empty fetch all messages of this process UINT wmsgfiltermin, Gets the minimum ID of the message UINT Wmsgfiltermax//Gets the maximum ID of the message);
Lpmsg-When a message is obtained, the parameters of the message are stored in the MSG structure
HWND-Gets the message to the window specified by the HWND
Wmsgfiltermin and Wmsgfiltermax-can only get messages within the range of messages specified by them, if both are 0, indicating that no range is discovered through a phenomenon postquitmessage can make getmessage return 0
The following is an explanation of MSDN:
GetMessage return Value:
If The function retrieves a message other than Wm_quit, the return value is nonzero.
If The function retrieves the wm_quit message, the return value is zero.
PostQuitMessage Function
The postquitmessage function indicates to the system, a thread has made a request to terminate (quit). It is typically used in response to a WM_DESTROY message.
Syntax
void PostQuitMessage (
int nexitcode);
Parameters
-
Nexitcode
-
[in] specifies an application exit code. This value is used as the
wParam parameter of the WM_QUIT message.
2 TranslateMessage-Translate the message. Translates the key message into a character message.
BOOL TranslateMessage (
CONST MSG *lpmsg//message address to translate
);
Checks whether the message presses the message, if it is not a key message, does not do any processing, continue to execute.
3 DispatchMessage-Distribute the message. Dispatches a message to the window processing function of the window to which the message belongs
LRESULT Dispathmessage (
CONST MSG *lpmsg//message to be distributed
);
Message loops for Windows core programming 06-windows