SoFunction
Updated on 2025-03-05

Example of usage of postmessage for VC custom message response function

This article describes the usage of postmessage, a custom message response function, in this article. Share it for your reference. The specific implementation steps are as follows:

1. Add the following code to the file to set your own message

Copy the codeThe code is as follows:
#define WM_MY_MESSAGE      WM_USER + 100       //---------------------by tyds

2. Add the file in... as follows:

Copy the codeThe code is as follows:
//{{AFX_MSG(CPostmessageView)
afx_msg void Ontydspostmessage();

afx_msg /*LRESULT*/ void OnMyMessage(/*WPARAM wParam, LPARAM lParam*/); //----- by tyds

//}}AFX_MSG
DECLARE_MESSAGE_MAP()

3. Add the following code to the... file

Copy the codeThe code is as follows:
BEGIN_MESSAGE_MAP(CPostmessageView, CView)
//{{AFX_MSG_MAP(CPostmessageView)
ON_COMMAND(ID_tyds_postmessage, Ontydspostmessage)
ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)      //Add message mapping-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

void CPostmessageView::Ontydspostmessage()
{
MessageBox("begin post message!");
//PostMessage(WM_MY_MESSAGE);     //The two differences here are:
SendMessage(WM_MY_MESSAGE);        //PostMessage returns when sent out, while SendMessage returns when sent out.
}

Message corresponding function
/*LPESULT*/void   CPostmessageView::OnMyMessage(/*WPARAM wParam, LPARAM lParam*/) //Note whether the parameters here are required or not to determine them according to your own return value.
{

MessageBox("post msg finished!");
// return 0;
}

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