WTL work around WM_NOTIFY
-
Hi, I came up with a way how to call member functions directly so sending WM_NODIFY messages is no longer neccessary and I am interested in your opinions! I have a MDI window (derived from CMDIFrameWindowImpl) that notifies the active MDI child (CChildFrame derived from CMDIChildWindowImpl) if its document should be opened/saved/reopened ect... The only way I knew to do this was to send the MDI child a WM_NOTIFY message with different notification code and NMHDR*. I thought it would be nice to call member function like Save(), Open(CString file),... directly on the CChildFrame object because it would be a lot easier to read and the casting of NMHDR* would no longer be neccessary. So here is the code:
// The notification code #define ATL_GET_THIS 100 template<typename T> T* AtlGetObject(HWND hWnd) { NMHDR nmhdr = { hWnd, 0, ATL_GET_THIS }; return (T*)(::SendMessage(hWnd, WM_NOTIFY, 0, (LPARAM)&nmhdr)); }
The CChildFrame must handle the ATL_GET_THIS notification:BEGIN_MSG_MAP_EX(CChildFrame) ... NOTIFY_CODE_HANDLER_EX(ATL_GET_THIS, _AtlOnNotifyGetThis) ... END_MSG_MAP() LRESULT _AtlOnNotifyGetThis(NMHDR* /*phdr*/) { return (LRESULT)this; }
So the code in the parent to call some member function in the active MDI child would look like this:CChildFrame* pWndChild = AtlGetObject(MDIGetActive()); CString sFilename = pWndChild->GetFilename(); CFileDialog dlg(FALSE, 0, sFilename); if (dlg.DoModal() == IDOK) { pWndChild->SaveAs(dlg.m_ofn.lpstrFile); }
What do you say? I know it works, but are there some traps I didn't see? And if this is a good idea, why is nothing like this in the ATL/WTL already? Or there is and I didn't see it? Thanks for your time! Constantin