Custom message function usage instance of SendMessage in vc

  • 2020-04-02 02:56:09
  • OfStack

This article illustrates the use of SendMessage custom message function in vc. The details are as follows:

The basic structure of SendMessage is as follows:

SendMessage(
    HWND hWnd,  //Handle to the target window or thread of the message passing. < br / >     UINT Msg, //Message categories (which can be either system messages or self-defined, as described below)
    WPARAM wParam, //Parameter 1 (WPARAM is actually the same type as UINT,
  //Right click on the vc compiler to see the "go to WPARAM definition" option. < br / >     LPARAM lParam); // parameter 2

Some of the parameters are derived as follows:

/ / typedef unsigned int UINT;
/ / typedef UINT WPARAM.
/ / typedef LONG LPARAM;
/ / typedef LONG LRESULT;

For example, use the following statement:

::SendMessage(this->m_hWnd, WM_MY_DOSOME, (WPARAM) 0, (LPARAM) 0);

Here the message I send is received by the form, so the handle is: this- > m_hWnd
The message category here WM_MY_DOSOME is my custom,
In the header file of the form or thread receiving the message:

#define WM_MY_DOSOME WM_USER+1 // do something

Of course, you can define more such as:
#define WM_DOOTHER WM_USER+2 // do other

It means to do something.

At this point, you may still be a little fuzzy about the message categories, so don't worry, we'll get to that soon.
We send out a message, and the receiver has to be able to identify what the message is doing by sorting through the message categories and start doing what the message corresponds to. As follows:

One, write a thing:
We define such a thing in the receive form,

afx_msg LRESULT DoSomeThing(WPARAM iParam1,LPARAM iParam2)
{
 MessageBox(" I got the message. I'm going to do something. "," received ",MB_OK);
 //You can use iParam1, iParam2 to do something. < br / >  return 0;
}

There are three important things to note about this:

1. Afx_msg is used, and afx_msg LRESULT DoSomeThing(WPARAM iParam1,LPARAM iParam2)
Overwrite the header file
/ / {{AFX_MSG
/ /... If I rewrite it here, it's going to be gray. This is very important.
/ /}} AFX_MSG
2. There are 2 parameters, WPARAM iParam1,LPARAM iParam2, even if nothing is passed in, you have to write, otherwise you will suffer, vc will not remind you to write one less,
But something strange happens.
3. Type is LRESULT, return 0 when finished;

2. Let the recipient know when to do this:
We are in

//{{AFX_MSG_MAP
//. I'll say
ON_MESSAGE(WM_MY_DOSOME,DoSomeThing)
//If there are other messages, write another
ON_MESSAGE(WM_DOOTHER,DoOther)
//}}AFX_MSG_MAP

Here, when you send a WM_MY_DOSOME type message with SendMessage, the receiver will do DoSomeThing(WPARAM iParam1,LPARAM iParam2)
When a message of type WM_DOOTHER is sent, the receiver will do DoOther(WPARAM iParam1,LPARAM iParam2) and of course I haven't defined DoOther yet.

So this is a complete message sending and receiving process, and I'm not going to go into the parameters here, iParam1, because I haven't used very complex cases yet,

In the header file:

#define WM_MYMSG  WM_USER+5 //Customize a message
afx_msg void OnMyMessage(WPARAM wParam, LPARAM lParam); // Custom handler declarations for messages 

In the. CPP file:

ON_MESSAGE(WM_MYMSG, OnMyMessage)
//Use the ON_MESSAGE() macro to map between the custom message and its handler void CModelessDlg::OnMyMessage(WPARAM wParam, LPARAM lParam)
//Take the pointer to the CString object from lParam and display the string contents in IDC_MSGEDIT
{     CString *str;
    str=(CString *)lParam;     SetDlgItemText(IDC_EDIT,*str);
}

Press the button to send the message

void CModelessDlg::OnMsgBTN() 
{
 CString str= " Custom message triggered! ";
 SendMessage(WM_MYMSG, 0, (LPARAM) &str);
 //Send ModelessDlg itself a custom message < br / > }

Hope that this article described the VC programming for you to help.


Related articles: