Solved

Ending DoModal loop when owner window is destroyed

Posted on 1997-12-08
3
1,071 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:postrowski
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 200 total points
ID: 1311822
Loop thru all your child windows (and children of children)

Get a pointer to the CWnd* (derived) object for each one

Check if the pointer IsKindOf CDialog.

If so, call EndDialog for the window.

0
 

Author Comment

by:postrowski
ID: 1311823
Well, I can't loop through all of the child windows, since an owned window is not a child window, but the IsKindOf function helped out a lot.

What I can do is enumerate all of the top level windows in the system with:

EnumWindows(CMyApp::CloseIfOwned, (LPARAM)MAIN->m_hWnd);

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(hwnd);
   if(pWnd != NULL)
   {
      if(pWnd->IsKindOf(RUNTIME_CLASS(CDialog)) ||
         pWnd->IsKindOf(RUNTIME_CLASS(CPropertySheet)))
         ((CDialog *)pWnd)->EndDialog(IDCANCEL);
   }

   ::DestroyWindow(hwnd);

   return TRUE; // always continue enumeration.
}
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1311824
Looks like all you needs was a push in the right direction.

Glad to have helped

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maxBlock challenge 30 151
zeroMAx challenge 20 135
matchUp  challenge 9 134
firstChar challenge 13 149
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question