SoFunction
Updated on 2025-03-05

Example of the usage of SendMessage custom message function in vc

This article describes the usage of SendMessage custom message function in vc, and is shared with you for your reference. The details are as follows:

The basic structure of SendMessage is as follows:

Copy the codeThe code is as follows:
SendMessage(
HWND hWnd, //The address of the target window or thread for the message passing.
UINT Msg, //Message category (here can be some system messages, or you can define them yourself, as detailed below,)
WPARAM wParam, // Parameter 1 (WPARAM is actually of the same type as UINT,
//The right-click in the vc compiler has an option to view it.
LPARAM lParam); //parameter 2

Some of these parameters are originated as follows:

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

For example, you can use the following statement:

Copy the codeThe code is as follows:
::SendMessage(this->m_hWnd, WM_MY_DOSOME, (WPARAM) 0, (LPARAM) 0);

The message I sent here was received by this form, so the handle is: this->m_hWnd
The message category WM_MY_DOSOME here is my customization.
In the header file where the message is received or the thread is located:

Copy the codeThe code is as follows:
#define WM_MY_DOSOME WM_USER+1 // do something

Of course you can define more such as:
Copy the codeThe code is as follows:
#define WM_DOOTHER WM_USER+2 // do other

It means to do something.

At this point, you may still be a little vague about the message category, so don’t worry, as I will talk about it soon.
We sent a message, so the receiver needs to be able to identify what the message is for, that is, distinguish it through the message category and start doing what the message needs to be processed. as follows:

1. Write a thing:
We define such a thing (process) in the receiving form,

Copy the codeThe code is as follows:
afx_msg LRESULT DoSomeThing(WPARAM iParam1,LPARAM iParam2)
{
MessageBox("Received the message, I'm going to start doing something.","Received", MB_OK);
//You can use iParam1 and iParam2 to do some things.
 return 0;
}

There are 3 things that everyone should pay attention to, and they are very important:

1. Use afx_msg, and you want to use afx_msg LRESULT DoSomeThing(WPARAM iParam1,LPARAM iParam2)
Rewrite to header file
//{{AFX_MSG
//. . . After rewriting this, the color will turn gray. This is very important.
//}}AFX_MSG
2. There are 2 parameters, WPARAM iParam1, LPARAM iParam2. You have to write even if nothing is passed in, otherwise you will suffer. The vc will not remind you to write less.
But something inexplicable will happen.
3. Use LRESULT for type, and return 0 after the end;

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

Copy the codeThe code is as follows:
//{{AFX_MSG_MAP
//. . . Write it here
ON_MESSAGE(WM_MY_DOSOME,DoSomeThing)
//If there is any other message, write another one
ON_MESSAGE(WM_DOOTHER,DoOther)
//}}AFX_MSG_MAP

At this point, when you send a WM_MY_DOSOME type message with SendMessage, the receiver will do DoSomeThing (WPARAM iParam1,LPARAM iParam2)
When a message of WM_DOOTHER type comes, the receiver will do DoOther (WPARAM iParam1, LPARAM iParam2). Of course, I have not defined DoOther here yet.

This is a complete message sending and receiving process. The parameters are not discussed in detail here, iParam1, because there has not been a very complicated situation yet.

In the header file:

Copy the codeThe code is as follows:
#define WM_MYMSG WM_USER+5 //Customize a message

afx_msg void OnMyMessage(WPARAM wParam, LPARAM lParam); //Custom message processing function declaration

In the .cpp file:

Copy the codeThe code is as follows:
ON_MESSAGE(WM_MYMSG, OnMyMessage)
//Use the ON_MESSAGE() macro to establish a mapping relationship between custom messages and their processing functions

void CModelessDlg::OnMyMessage(WPARAM wParam, LPARAM lParam)
//Take out the pointer of the CString object from lParam and display the string content in IDC_MSGEDIT
{

    CString *str;
    str=(CString *)lParam;

    SetDlgItemText(IDC_EDIT,*str);
}

Press the button to send a message

Copy the codeThe code is as follows:
void CModelessDlg::OnMsgBTN()
{
CString str= "The custom message was triggered!";
 SendMessage(WM_MYMSG, 0, (LPARAM) &str);
//Send a custom message to ModellessDlg
}

I hope this article will be helpful to everyone's VC programming.