Parent notification in MFC Dialog

I have a first dialog with a simple button on it, and when the button is clicked, 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, that is, without adding the M_pParent-> Notify () line in the OnDestroy method. I've tried OnParentNotify, PreTranslateMessage, SubclassWindow on the parent dialog with no success. I didn't use the WS_CHILD style for the second dialog. Any idea?

To finish: actually, I have a ComboBox derived class (but the problem is the same with buttons) and I am showing a modeless dialog instead of showing a list. But I would like to keep the control as general as possible so that any modeless dialog can be used. So I don't want to add a custom notification in the second dialog. If I have to, I'll use this trick, but I asked for a more general solution. PreTranslateMessage only catches WM_PAINT, WM_NCMOUSELEAVE and WM_NCMOUSEMOVE.

+1


source to share


3 answers


Use the base class and the parent parent only refers to the cool child of the base class. In PostNcDestroy, you have a message for the parent.



It doesn't make sense for a parent to do a bunch of filtering / spying on all posts. It makes sense to implement a behavior in a base class that you want to have in common with all the different future tastes that a non-model child might have.

+2


source


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

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



Of course, the first thing that comes to mind is why you don't want to add a custom notification to your model code code.

EDIT: Another method is to set the message hook (for the current thread, not the whole system!). This will help you catch all messages for all windows associated with the same thread as dialog1. See SetWindowsHookEx ()

+1


source


How do I send an event message of the main parent form to the message queue?

0


source







All Articles