How to send messages to a window

Source: Internet
Author: User

The SendMessage and PostMessage member functions of the CWnd class.
Step 1: Customize the message in the header file, for example:
[Cpp]
# Define WM_USER_MSG (wm_user+ 100)
Step 2: Click the Message tab in the Class Wizard to add the custom Message WM_USER_MSG.
Step 3: implement the response function of the custom message.
Step 4: send a message.
There are two ways to send messages.
1. synchronous SendMessage)
[Plain]
LRESULT SendMessage (
UINT message,
WPARAM wParam = 0,
LPARAM lParam = 0
);
Message: message ID.
WParam: parameter 1
LParam: parameter 2
Returned value: the processing result of the message function.
Synchronous transmission means that the message is returned only after the message is sent and processed. For example:
Http://msdn.microsoft.com/en-US/library/t64sseb3 (v = vs.80)
[Cpp]
// In a dialog-based app, if you add a minimize button to your
// Dialog, you will need the code below to draw the icon.
Void CMyDlg: OnPaint ()
{
If (IsIconic ())
{
CPaintDC dc (this); // device context for painting
 
SendMessage (WM_ICONERASEBKGND, (WPARAM) dc. GetSafeHdc (), 0 );
 
// Center icon in client rectangle
Int cxIcon = GetSystemMetrics (SM_CXICON );
Int cyIcon = GetSystemMetrics (SM_CYICON );
CRect rect;
GetClientRect (& rect );
Int x = (rect. Width ()-cxIcon + 1)/2;
Int y = (rect. Height ()-cyIcon + 1)/2;
 
// Draw the icon represented by handle m_hIcon
Dc. DrawIcon (x, y, m_hIcon );
}
Else
{
CDialog: OnPaint ();
}
}
SendMessage is described as follows in MSDN:
[Plain]
The SendMessage member function callthe window procedure directly and does not return until that window procedure has processed the message
That is, the "send" operation will return and end only after the message processing function completes processing the message.
2. asynchronous (PostMessage)
Http://msdn.microsoft.com/en-US/library/9tdesxec (v = vs.80)
[Plain] view plaincopy
BOOL PostMessage (
UINT message,
WPARAM wParam = 0,
LPARAM lParam = 0
);

The parameter description is the same as SendMessage, but the return value is different. The interface returns a message immediately after it is sent to the message queue, and does not wait for the message to be returned after processing.
You can also use the s api function to send messages:
[Cpp]
Lresult winapi SendMessage (
_ In _ HWND hWnd,
_ In _ UINT Msg,
_ In _ WPARAM wParam,
_ In _ LPARAM lParam
);
HWnd is the handle of the window to be sent.
[Cpp]
LRESULT Res =: SendMessage (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam );
The corresponding asynchronous sending interface is:
[Cpp]
Bool winapi PostMessage (
_ In_opt _ HWND hWnd,
_ In _ UINT Msg,
_ In _ WPARAM wParam,
_ In _ LPARAM lParam
);
Use the same as: SendMessage.

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.