'Send User Message to child window in CCtrlTab control

I have a dialog window (MainDlg : CDialogEx), in this window is tabcontrol (MyTab : CTabCtrl) and in this CTabCtrl I have child windows (Tab1Dlg : CDialogEx). This "tab" windows is show or hidden according to tabs selection. And I need to send message from MainDlg to TabDlg. How to do it? Thanks for help.



Solution 1:[1]

I used User messages. You can use SendMessageA or SendMessageTimeout to send a User message to a child form.

STEP 1. Add user messages to pch.h or stdafx.h

#define WM_USERMESSAGE  WM_APP+30

STEP 2. Add user message handlers Tab1Dlg.h before DECLARE_MESSAGE_MAP()

afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam); 

STEP 3. Add a macro to the Tab1Dlg.cpp file that accepts the message mapping

BEGIN_MESSAGE_MAP(Child_Dlg, CDialogEx)
    ON_MESSAGE(WM_USERMESSAGE, OnMyMessage)
END_MESSAGE_MAP()

// Child_Dlg message handlers
LRESULT Child_Dlg::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
    MessageBox(L"recv msg success");
    return 0;
}

STEP 4. Send the Msg in MainDlg

//send msg to child wnd in tab control
void CMFCTestDlg::OnBnClickedButton1()
{
    //SendMessageA(m_childDlg.m_hWnd, WM_USERMESSAGE, 0, 0);

    DWORD result;
    SendMessageTimeout(m_childDlg.m_hWnd,
        WM_USERMESSAGE,
        0,
        0,
        SMTO_ABORTIFHUNG |
        SMTO_NORMAL,
        2000,
        &result);
}

Here is my test project and test gif; https://github.com/SchrdingerCAT123/SendMsgToChild/tree/main/MFC_Test

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1