Working with:
- Visual Studio 2017;
- MFC, C++.
I'm trying to modify a MFC project, for a CDialog derived class to detect when a child control is added to it ( CButton derived classes ). Preferably getting a handle (HWND) to those CButtons to process them further, like I can get from the parent dialog window with the following code:
HWND handleParent = ::GetTopWindow(this->GetSafeHwnd());
I've read one way to do it, by handling WM_PARENTNOTIFY, but I cannot trigger it by any way using main event function: OnParentNotify (or WindowProc from some sources).
I've done the following, at least for OnParentNotify:
- added a message export:
ON_WM_PARENTNOTIFY()
- in member function
DerivedDialog::OnInitDialog()removedWS_EX_NOPARENTNOTIFYstyle from all control handles possible, afterCDialog::OnInitDialog()line of code:
CDialog::OnInitDialog(); HWND hwnd = ::GetTopWindow(this->GetSafeHwnd()); while (hwnd) { LONG lExStyle; lExStyle = GetWindowLong(hwnd, GWL_EXSTYLE); if (lExStyle & WS_EX_NOPARENTNOTIFY) { lExStyle &= ~WS_EX_NOPARENTNOTIFY; SetWindowLong(hwnd, GWL_EXSTYLE, lExStyle); } hwnd = ::GetNextWindow(hwnd, GW_HWNDNEXT); }
- declared and defined function just to see if it is called:
OnParentNotify(UINT message, LPARAM lParam) { CDialog::OnParentNotify(message, lParam) { switch (LOWORD(message)) { case WM_CREATE: { int a = 3; int b = 2; } break; case WM_PARENTNOTIFY: { int c = 1; int d = 0; } } }
Unfortunately, only WM_CREATE is called once (don't think it is related or correct, since I have 2 buttons that must be added to dialog..so I expect 2 WM_CREATES if it is the case ??).
I am really not sure how to trigger that message to be called. Any tip would be very helpful !