SendMessage custom message function is used in vc

  • 2020-04-02 03:05:07
  • OfStack

The basic structure of SendMessage is as follows:


SendMessage(
  HWND hWnd, //Handle to the target window or thread of the message passing.
  UINT Msg, //Message categories (here can be some 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.
  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're probably still a little fuzzy about the message categories, so don't worry, I'll get to that in a minute.
We send out a message, and the receiver has to be able to identify what the message is doing, which is to distinguish by the type of message,
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.
 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) is required.
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. Write even if nothing is passed in, otherwise you will suffer, vc will not remind you to write one less,
But something strange happens.
3: type with LRESULT, return 0;

Two: let the receiver know when to do this:

We are in


//{{AFX_MSG_MAP
//. Write here
ON_MESSAGE(WM_MY_DOSOME,DoSomeThing)
//Write another message if you have any
ON_MESSAGE(WM_DOOTHER,DoOther)
//}}AFX_MSG_MAP

So here, when you send a WM_MY_DOSOME message with SendMessage,
The receiver is going to do DoSomeThing(WPARAM iParam1,LPARAM iParam2)
When I send a WM_DOOTHER message,
The receiver is going to 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 didn't go into the details of the parameter, iParam1, because I haven't used very complex cases yet,
If you have any ideas we can improve together.

In the header file


#define WM_MYMSG  WM_USER+5 //Customize a message
afx_msg void OnMyMessage(WPARAM wParam, LPARAM lParam); //The handler declaration for the custom message is

In the.cpp file
ON_MESSAGE (WM_MYMSG OnMyMessage)
// USES 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

The above is all the content of this article, I hope you can enjoy it.


Related articles: