Message Mechanism in SDK

Source: Internet
Author: User

After learning VC for so long, I still cannot fully understand the message mechanism of WINDOWS. I will sort out the message mechanism in the SDK this evening. Let's explain it first, this article was written when I checked the information. If there is an error, I hope you can help me correct it. Thank you very much.

The first is the message structure:

The old method is to check MSDN first

The MSG structure contains message information from a thread's message queue.

Syntax

Ypedef struct {
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG, * PMSG; Members

Hwnd
Handle to the window whose window procedure contains es the message.
// Handle pointing to the message window
Message
Specifies the message identifier. Applications can only use the low word; the high word is reserved by the system.
// The message identifier. The application can only use the low byte, and the high byte must be used as the reserved word.
WParam
Specifies additional information about the message. The exact meaning depends on the value of the message member.
// The general meaning is that different messages have different meanings.
LParam
Specifies additional information about the message. The exact meaning depends on the value of the message member.
// The definition is the same as that of Wparam.
Time
Specifies the time at which the message was posted.
// Message sending time
Pt
Specifies the cursor position, in screen coordinates, when the message was posted.
// Specify the coordinates of message sending
In fact, there is nothing to say about the message structure.

The key point is how messages are sent and processed.

If you have understood these two aspects, you should be able to understand the message mechanism in the SDK.

First, send messages: When will a window send messages? How to send messages in the function body? That is, how to call the message function in a function to send messages and then let the specified window make corresponding actions.

There are three ways to send a message to a window: sending, sending, and broadcasting.
The message sending functions include SendMessage, SendMessageCallback, sendpolicymessage, and SendMessageTimeout;

The functions used to send messages include PostMessage, PostThreadMessage, and PostQuitMessage;

I only know BroadcastSystemMessage and BroadcastSystemMessageEx.

Start with SendMessage...

MSDN:

The SendMessage function sends the specified message to a window or windows. It callthe window procedure for the specified window and does not return until the window procedure has processed the message.

To send a message and return immediately, use the SendMessageCallback or sendpolicymessage function. To post a message to a thread's message immediately, queue and return use the PostMessage or PostThreadMessage function.

// SendMessage sends a message to wait for the returned result. Use SendMessageCallback or sendpolicymessage to return the message immediately. Use PostMessage or PostThreadMessage to send the message to a thread.
Syntax

LRESULT SendMessage (HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
); Parameters

HWnd
[In] Handle to the window whose window procedure will receive the message. if this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.

If this parameter is HWND_BROADCAST, the message will be sent to all top-level windows, including invalid, hidden, overlapping, and pop-up windows, but this message will not be sent to the subwindows.
Msg
[In] Specifies the message to be sent.
WParam
[In] Specifies additional message-specific information.
LParam
[In] Specifies additional message-specific information.
Return Value

The return value specifies the result of the message processing; it depends on the message sent.

Remarks

Applications that need to communicate using HWND_BROADCAST shocould use the RegisterWindowMessage function to obtain a unique message for inter-application communication.

The system only does grouping alling for system messages (those in the range 0 to WM_USER). To send other messages (those above WM_USER) to another process, you must do custom grouping alling.

The above sentence is a key part of the message mechanism. Please help me translate it. Thank you.

If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. if the specified window was created by a different thread, the system switches to that thread and callthe appropriate window procedure. messages sent between threads are processed only when the processing ing thread executes message retrieval code. the sending thread is blocked until the processing ing thread processes the message. however, the sending thread will process incoming nonqueued messages while waiting for its message to be processed. to prevent this, use SendMessageTimeout with SMTO_BLOCK set. for more information on nonqueued messages, see Nonqueued Messages.

There are three main functions for receiving messages: GetMessage, PeekMessage, and WaitMessage.

The following code is used to write programs using sdks:

Switch (message)
{
Case WM_COMMAND:
WmId = LOWORD (wParam );
WmEvent = HIWORD (wParam );
// Parse the menu selections:
Switch (wmId)
{Case IDM_ABOUT:
DialogBox (hInst, (LPCTSTR) IDD_ABOUTBOX, hWnd, (DLGPROC) About );
Break;
Case IDM_EXIT:
DestroyWindow (hWnd );
Break;
Default:
Return DefWindowProc (hWnd, message, wParam, lParam );
}

The above code can still be executed when there are few messages, but the message will be too unbearable for a long time.

The concept of message delivery is proposed.

Some content in WindowsX. h is as follows:

******************************* *********************/

# Define HANDLE_MSG (hwnd, message, fn )/
Case (message): return HANDLE _ # message (hwnd), (wParam), (lParam), (fn ))

/* Void Cls_OnCompacting (HWND hwnd, UINT compactRatio )*/
# Define HANDLE_WM_COMPACTING (hwnd, wParam, lParam, fn )/
(Fn) (hwnd), (UINT) (wParam), 0L)
# Define FORWARD_WM_COMPACTING (hwnd, compactRatio, fn )/
(Void) (fn) (hwnd), WM_COMPACTING, (WPARAM) (UINT) (compactRatio), 0L)

/* Void Cls_OnWinIniChange (HWND hwnd, LPCTSTR lpszSectionName )*/
# Define HANDLE_WM_WININICHANGE (hwnd, wParam, lParam, fn )/
(Fn) (hwnd), (LPCTSTR) (lParam), 0L)
# Define FORWARD_WM_WININICHANGE (hwnd, lpszSectionName, fn )/
(Void) (fn) (hwnd), WM_WININICHANGE, 0L, (LPARAM) (LPCTSTR) (lpszSectionName ))

/* Void Cls_OnSysColorChange (HWND hwnd )*/
# Define HANDLE_WM_SYSCOLORCHANGE (hwnd, wParam, lParam, fn )/
(Fn) (hwnd), 0L)
# Define FORWARD_WM_SYSCOLORCHANGE (hwnd, fn )/
(Void) (fn) (hwnd), WM_SYSCOLORCHANGE, 0L, 0L)

/* BOOL Cls_OnQueryNewPalette (HWND hwnd )*/
# Define HANDLE_WM_QUERYNEWPALETTE (hwnd, wParam, lParam, fn )/
MAKELRESULT (BOOL) (fn) (hwnd), 0L)
# Define FORWARD_WM_QUERYNEWPALETTE (hwnd, fn )/
(BOOL) (DWORD) (fn) (hwnd), WM_QUERYNEWPALETTE, 0L, 0L)

/* Void Cls_OnPaletteIsChanging (HWND hwnd, HWND hwndPaletteChange )*/
# Define HANDLE_WM_PALETTEISCHANGING (hwnd, wParam, lParam, fn )/
(Fn) (hwnd), (HWND) (wParam), 0L)
# Define FORWARD_WM_PALETTEISCHANGING (hwnd, hwndPaletteChange, fn )/
(Void) (fn) (hwnd), WM_PALETTEISCHANGING, (WPARAM) (HWND) (hwndPaletteChange), 0L)
........................................ .............

The following describes the effect of traffic distribution.
Switch (message)
{
HANDLE_MSG (hWnd, WM_COMMAND, MsgCracker );
Handle_msg (hwnd, wm_destroy, msgcracker );
Default:
Return defwindowproc (hwnd, message, wparam, lparam );
}

I will study it tomorrow.

Sleeping :(

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.