MFC: Send Message to Main Window

To send a message to the main window in an MFC application, you can use the AfxGetMainWnd function to obtain a pointer to the main window, and then use the PostMessage or SendMessage function to send the message.

For example, you can use the following code to send a custom message to the main window in an MFC application:

// 获取主窗口指针
CWnd* pMainWnd = AfxGetMainWnd();

// 发送消息给主窗口
UINT msg = WM_USER + 1; // 自定义消息
pMainWnd->PostMessage(msg, 0, 0);

For example, this custom message can be handled in the message mapping function of the main window class.

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    ON_MESSAGE(WM_USER + 1, OnCustomMessage)
END_MESSAGE_MAP()

LRESULT CMainFrame::OnCustomMessage(WPARAM wParam, LPARAM lParam)
{
    // 处理自定义消息
    return 0;
}

By using this method, it is easy to send messages to the main window in an MFC application and handle these messages in the main window class.

bannerAds