postrowski
asked on
Ending DoModal loop when owner window is destroyed
I need to close a whole "stack" of windows when a stimulus occurs at the top level. The Windows Owner/Owned relationship will destroy an owned window when the owner is destroyed, but this will not cause EndDialog to run (or, more importantly, EndModalLoop). So, after the window is destroyed, the app is still (at some level of the call stack), running in RunModalLoop and when the app returns there, the call to ContinueModal still returns TRUE (because EndModalLoop has yet to run).
I need to fix this for a number of cases in the application, and it would be cumbersome to add a special handler (say, OnDestroy) to every class or to interpose a special MyAppCDialog class with this handler in the class hierarchy.
What can I do to get EndDialog or EndModalLoop to run?
I need to fix this for a number of cases in the application, and it would be cumbersome to add a special handler (say, OnDestroy) to every class or to interpose a special MyAppCDialog class with this handler in the class hierarchy.
What can I do to get EndDialog or EndModalLoop to run?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Looks like all you needs was a push in the right direction.
Glad to have helped
Glad to have helped
ASKER
What I can do is enumerate all of the top level windows in the system with:
EnumWindows(CMyApp::CloseI
And then check each top level window to see if it is owned by the main application window (or owned by a window that is owned by the main window, etc.). If so, check to see if it IsKindOf(CDialog) or IsKindOf(CPropertySheet) (since CPropertySheet is not descended from CDialog). This approach seems to work just fine. The enumeration callback function is listed below. Thanks for your help.
BOOL CALLBACK CMyApp::CloseIfOwned(
HWND hwnd,// handle to window in question
LPARAM lParam // handle to main app window
)
{
HWND hOwner;
HWND hTmp = hwnd;
do
{
hOwner = ::GetWindow(hTmp, GW_OWNER);
if(hOwner == NULL)
return TRUE; // always continue enumeration.
hTmp = hOwner;
} while (hOwner != (HWND)lParam);
// Here we know that hwnd is owned by us.
CWnd *pWnd;
pWnd = CWnd::FromHandlePermanent(
if(pWnd != NULL)
{
if(pWnd->IsKindOf(RUNTIME_
pWnd->IsKindOf(RUNTIME_CLA
((CDialog *)pWnd)->EndDialog(IDCANCE
}
::DestroyWindow(hwnd);
return TRUE; // always continue enumeration.
}