Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I found in internet a method to send messages from child to dialog, but if I want to send message from dialog to child?

What I have tried:

To send from child to dialog I founf this method:

#define UWM_CUSTOM (WM_APP+1)

// in child
CString str = _T("Test");
GetParent()->SendMessage(UWM_CUSTOM, (WPARAM)&str, 0);

// in parent
ON_MESSAGE(UWM_CUSTOM, OnCustom)

LRESULT Cxx::OnCustom(WPARAM wparam, LPARAM lparam)
{ CString* pstr = (CString*)wparam;
  ...use pstr
  return 0;
}



but I want to do the opposite
Posted
Updated 3-Oct-19 23:00pm
v2
Comments
Mohibur Rashid 4-Oct-19 2:43am    
If you know the CWnd pointer you definitely can write something like this
child_window->SendMessage(...);

You will have to store the child_window address in some sort of variable, Other wise you will have to find another method to identify your child.

1 solution

If your child window is modeless, you can use the pointer to your dialog and use
C++
#define UWM_CUSTOM (WM_APP+1)

// in parent
void CMFCApplicationTestDlg::OnBnClickedButCreatedlg()
{
	CString str = _T("Test");
	CChildDlg* pChildDlg = new CChildDlg(this);
	pChildDlg->Create(IDD_DIALOG_CHILD, this);

	pChildDlg->ShowWindow(SW_SHOW);

       //pChildDlg->SendMessage(UWM_CUSTOM, (WPARAM)&str, 0);
       // Better
       pChildDlg->PostMessage(UWM_CUSTOM, (WPARAM)&str, 0);
}



If your child window is modal, you can't because after
C++
DoModal()
returns, the dialog will already be destroyed.
 
Share this answer
 
v6
Comments
Member 14594285 4-Oct-19 5:31am    
what is pChildDlg?
Member 14594285 4-Oct-19 5:34am    
anyway I used ShowWindow(true) to show child dialog
KarstenK 4-Oct-19 5:36am    
pChildDlg is the pointer to the child dialog. You better use PostMessage to not block the internal message queue which may result in a blocking app.
Member 14594285 4-Oct-19 5:41am    
so I must write.
MyDlg * pChildDlg = new pChildDlg;

pChildDlg->SendMessage(UWM_CUSTOM, (WPARAM)&str, 0);
Leo Chapiro 4-Oct-19 5:43am    
Yes, better as Karsten mentioned, you use the PostMessage.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900