Only if WTL had AfxGetMainWnd
-
In MFC there is an easy to get access to CMainFrame and call its methods from anywhere in the application. Just by simply doing this... CMainFrame pFrame = (CMainFrame*)AfxGetMainWnd(); pFrame->MyFunction(...); Although in WTL there is method GetTopLevelWindow() that can be used to get access to Top Application Window, but there seems to be no way to cast the return CWindow type to CMainFrame. Take for instance the following... CMainFrame frm = (CMainFrame)GetTopLevelWindow(); frm.MyFunction(...); ... will not work, as there seems to be no way to cast ATL::CWindow to CMainFrame. THE QUESTION!! How would one go about calling CMainFrame method from anywhere in the WTL application (say from distant dialog box)? Without using global variable to store pointer to CMainFrame? Is there such way? Or am I just dreaming :) Thanks in advance Mike M
-
In MFC there is an easy to get access to CMainFrame and call its methods from anywhere in the application. Just by simply doing this... CMainFrame pFrame = (CMainFrame*)AfxGetMainWnd(); pFrame->MyFunction(...); Although in WTL there is method GetTopLevelWindow() that can be used to get access to Top Application Window, but there seems to be no way to cast the return CWindow type to CMainFrame. Take for instance the following... CMainFrame frm = (CMainFrame)GetTopLevelWindow(); frm.MyFunction(...); ... will not work, as there seems to be no way to cast ATL::CWindow to CMainFrame. THE QUESTION!! How would one go about calling CMainFrame method from anywhere in the WTL application (say from distant dialog box)? Without using global variable to store pointer to CMainFrame? Is there such way? Or am I just dreaming :) Thanks in advance Mike M
Mike.NET wrote: Is there such way? Or am I just dreaming But there are things that you can do to get around this. When you call this in MFC: CMainFrame *pFrame = (CMainFrame*)AfxGetMainWnd(); it is returning a pointer to the window that is stored as a member variable inside of the app object. You could either mimick this behaviour by creating your own app module that derives from CAppModule, and stores this member variable. Since GetTopLevelWindow only returns a HWND there is no way to cast this handle to a CWindow object without creating a completely new instance of the object. The next best thing that you could do is store a pointer to the CWindow, (or CMainFrame) object in the user data field of the window. That way you could do this to get the pointer that you are interested in:
HWND hWnd = GetTopLevelWindow();
CMainFrame *mainFrm = dynamic_cast(::GetWindowLong(hWnd, GWL_USERDATA);
if (NULL == mainFrm)
{
// This window is not of type CMainFrame
// Do error handling, or dynamic_cast to a CWindow if you are interested.
}
// This would now be a valid call through your pointer.
mainFrm->MyFunction(...);Good Luck!
Build a man a fire, and he will be warm for a day
Light a man on fire, and he will be warm for the rest of his life!