Ryan
Ryan

Reputation: 467

Parent notification in MFC Dialog

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.

Upvotes: 3

Views: 4094

Answers (3)

Aidan Ryan
Aidan Ryan

Reputation: 11589

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.

Upvotes: 2

Serge Wautier
Serge Wautier

Reputation: 21878

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()

Upvotes: 1

kenny
kenny

Reputation: 22334

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

Upvotes: -1

Related Questions