tags:

views:

961

answers:

3

I have a first dialog with a simple button on it and while clicking the button, a second dialog is created using CDialog::Create(IDD,this). I would like the parent to be notified when the second dialog is destroyed but without adding any code to the second dialog i.e., without adding a m_pParent->Notify() line in OnDestroy method. I have tried OnParentNotify, PreTranslateMessage, SubclassWindow in the parent dialog with no success. I have not used the WS_CHILD style for the second dialog. Any idea?

To complete: in fact, I have a ComboBox derived class (but the issue is the same with buttons) and I'm displaying a modeless Dialog instead of displaying the listbox. But I would like the control to be as generic as possible so that any modeless dialog could be used. That's why I do not want to add a specific notification in the second dialog. If I'm obliged, I will use this trick but I asked for a more generic solution. PreTranslateMessage only catches WM_PAINT, WM_NCMOUSELEAVE and WM_NCMOUSEMOVE.

A: 

How about posting a main parent form event to the message queue?

kenny
+1  A: 

OnParentNotify() is not called since dialog2 is not a child of dialog1.

PreTranslateMessage() should help here (although I don't like this bullet). The trick is that a modeless dialog doesn't destroy itself when it's closed. If you want the dialog to die, it must call DestroyWindow() when it closes, such in an OnCancel() override.

Of course, the first thing that comes to mind is t wonder why you don't want to add custom notification in your modeless dialog code.

EDIT: Another method would consist in installing a message hook (for the current thread, Not the whole system!). This would help you catch all messages for all windows associated to the same thread as dialog1. See SetWindowsHookEx()

Serge - appTranslator
In fact, I have a ComboBox derived class and I'm displaying a modeless Dialog instead of displaying the listbox. But I would like the control to be as generic as possible so that any modeless dialog could be used. PreTranslateMessage only catches WM_PAINT, WM_NCMOUSELEAVE and WM_NCMOUSEMOVE.
Ryan
I suggest you edit your question to explain your scenario.How is your modeless dialog closed and destroyed? Losing the focus should be enough. But, according to your scenario, you don't want to put that code in the modelss dialog either.See my Edit about hooks
Serge - appTranslator
Thank you for your help Serge,it finally worked using a mix of OnActivate/PreTranslateMessage(WM_NCMOUSEMOVE).But following ajryan advice,I will use a base class for my modeless dialogs as my original idea is not very practical:with many comboboxes of this type,parent management will be too complex.
Ryan
+1  A: 

Use a base class and have your parent refer to the modeless child by base class only. In the base PostNcDestroy have it post to the parent.

It doesn't make sense to have the parent do a bunch of filtering / spying on all messages. It does make sense to implement behavior in a base class that you want to have common to all the different future flavors you might have of the modeless child.

Aidan Ryan
Thanks ajryan,it finally worked using a mix of OnActivate/PreTranslateMessage(WM_NCMOUSEMOVE).I will follow your advice for a base class as it may be very complex on the parent level without it. Thank you.
Ryan