|
Prev: PostMessage and keyboard commands (specifically control)
Next: Access CList across other modules
From: xmp333 on 18 Mar 2005 16:39 Hi, I'm using VC++ 6. Is it possible for a parent to detect when a modeless (child) dialog closes? I couldn't find any event for this, and even tried a work-around where the child sent a message to the parent as it was closing -- but for some reason the message would not go through. Here is a snippet of the source I'm using: void CMyDialog::OnOK() { GetParent()->SendMessage(ID_MY_MESSAGE); CDialog::OnOK(); } I defined ID_MY_MESSAGE via the resource editor (as a menu item) and added a corresponding event handler to the parent (OnMyMessage()), but while the code compiles and runs, the parent's event handler never gets called. Why? I even tried storing the parent in a pointer (from the constructor) and using that: CMyDialog::CMyDialog(CWnd* parent) : CDialog(IDD, parent) { m_parent = parent; } void CMyDialog::OnOK() { m_parent->SendMessage(ID_MY_MESSAGE); CDialog::OnOK(); } Any help on getting SendMessage to call the parent's method, or other ways for the parent to detect the closure of CMyDialog would be greatly appreciated. Thank you in advance.
From: "CheckAbdoul" <checkabdoul at mvps dot on 18 Mar 2005 16:48 <xmp333(a)yahoo.com> wrote in message news:1111181993.393920.90820(a)o13g2000cwo.googlegroups.com... > Hi, > > > I'm using VC++ 6. > > Is it possible for a parent to detect when a modeless (child) dialog > closes? I couldn't find any event for this, and even tried a > work-around where the child sent a message to the parent as it was > closing -- but for some reason the message would not go through. > > Here is a snippet of the source I'm using: > > void CMyDialog::OnOK() { > GetParent()->SendMessage(ID_MY_MESSAGE); > CDialog::OnOK(); > } > > I defined ID_MY_MESSAGE via the resource editor (as a menu item) and > added a corresponding event handler to the parent (OnMyMessage()), but > while the code compiles and runs, the parent's event handler never gets > called. Why? > > I even tried storing the parent in a pointer (from the constructor) and > using that: > > CMyDialog::CMyDialog(CWnd* parent) : CDialog(IDD, parent) { > m_parent = parent; > } > > void CMyDialog::OnOK() { > m_parent->SendMessage(ID_MY_MESSAGE); > CDialog::OnOK(); > } > > > Any help on getting SendMessage to call the parent's method, or other > ways for the parent to detect the closure of CMyDialog would be greatly > appreciated. > > > Thank you in advance. > Try GetParent()->PostMessage(ID_MY_MESSAGE); -- Cheers Check Abdoul [VC++ MVP] -----------------------------------
From: Ajay Kalra on 18 Mar 2005 16:52 When you call SendMessage, parent should get the message. Did you put a break point in CParentofDialog::OnMessage() to see it gets the message? Also ensure that there is relevant entry in message map of the parent window. -------- Ajay Kalra ajaykalra(a)yahoo.com
From: Scott McPhillips [MVP] on 18 Mar 2005 18:33 xmp333(a)yahoo.com wrote: > Hi, > > > I'm using VC++ 6. > > Is it possible for a parent to detect when a modeless (child) dialog > closes? I couldn't find any event for this, and even tried a > work-around where the child sent a message to the parent as it was > closing -- but for some reason the message would not go through. > > Here is a snippet of the source I'm using: > > void CMyDialog::OnOK() { > GetParent()->SendMessage(ID_MY_MESSAGE); > CDialog::OnOK(); > } > > I defined ID_MY_MESSAGE via the resource editor (as a menu item) and > added a corresponding event handler to the parent (OnMyMessage()), but > while the code compiles and runs, the parent's event handler never gets > called. Why? That's not a message, it's an ID. Menu item IDs are sent as a parameter to the WM_COMMAND message. Make your own message like #define UWM_MY_MESSAGE (WM_APP + 1) Map it with ON_MESSAGE(UWM_MY_MESSAGE, OnMyMessage) Handle it with *exactly* this function prototype LRESULT Ccccc::OnMyMessage(WPARAM, LPARAM) -- Scott McPhillips [VC++ MVP]
From: Joseph M. Newcomer on 19 Mar 2005 11:43 Given that it is a modeless dialog shutting down, you must NOTCALL CDialog::OnOK. This is only for modal dialogs. You must remove the superclass call, and replace it with DestroyWindow(); You should not defined ID_MY_MESSAGE as a menu item, which is completely useless in any case; it will only lead to other disasters. If you were going to send a menu item message, you would have done m_Parent->SendMessage(WM_COMMAND, ID_MY_MESSAGE); (I think; you should go read the WM_COMMAND message details). What you have done is cause the ID number associated with ID_MY_MESSAGE to be interpreted as a Windows message; for example, if it happens to be the same integer as WM_SETFOCUS, WM_DESTROY, WM_CLOSE, etc. you are utterly doomed. Read my essay on message management. I would use a Registered Window Message here. It is pointless to store an m_parent variable when GetParent() does the job. Note also that for most modeless dialogs, you want to add a PostNcDestroy handler that says delete this; so the actual CMyDialog * heap object is deleted when it is no longer needed (you cannot delete it at the point where you intercept the user-defined message; that is far too early, and your app will amost certainly crash with an access fault) joe On 18 Mar 2005 13:39:53 -0800, xmp333(a)yahoo.com wrote: >Hi, > > >I'm using VC++ 6. > >Is it possible for a parent to detect when a modeless (child) dialog >closes? I couldn't find any event for this, and even tried a >work-around where the child sent a message to the parent as it was >closing -- but for some reason the message would not go through. > >Here is a snippet of the source I'm using: > >void CMyDialog::OnOK() { > GetParent()->SendMessage(ID_MY_MESSAGE); > CDialog::OnOK(); >} > >I defined ID_MY_MESSAGE via the resource editor (as a menu item) and >added a corresponding event handler to the parent (OnMyMessage()), but >while the code compiles and runs, the parent's event handler never gets >called. Why? > >I even tried storing the parent in a pointer (from the constructor) and >using that: > >CMyDialog::CMyDialog(CWnd* parent) : CDialog(IDD, parent) { > m_parent = parent; >} > >void CMyDialog::OnOK() { > m_parent->SendMessage(ID_MY_MESSAGE); > CDialog::OnOK(); >} > > >Any help on getting SendMessage to call the parent's method, or other >ways for the parent to detect the closure of CMyDialog would be greatly >appreciated. > > >Thank you in advance. Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
|
Pages: 1 Prev: PostMessage and keyboard commands (specifically control) Next: Access CList across other modules |