locked
How do I programmatically close all modal dialogs opened by a MDI Child?

    Question

  • We have a MFC application that has a USB security key that, when removed, causes the document to post a WM_CLOSE message to the view.  The goal is to shut down the document, but leave the application running so that the user can still get to the license information screen.  The problem is that the document/view closes, but there may be one or more modal dialogs and/or message boxes that remain open, and when the user dismisses them, there is an exception thrown.  Is there a way to have the view go through all the modal dialogs that it opened and close them before it exits?  I have tried paging through all child windows and posting WM_CLOSE messages, for both the closing view and the closing child frame, but the modal dialogs seem unaffected.
    Saturday, June 26, 2010 6:02 PM

All replies

  • Can you broadcast a user-defined message to all dialogs, and when the popup dialogs get the message, they start a timer and call EndDialog when they are the most recent active popup.

    The reason of suggesting this method is that the oder of modal dialog destruction is rather important. There could be better ways to end a dialog box, but I do not like the global variable used in the solution.

     



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Sunday, June 27, 2010 4:49 AM
  • We have a MFC application that has a USB security key that, when removed, causes the document to post a WM_CLOSE message to the view. The goal is to shut down the document, but leave the application running so that the user can still get to the license information screen. The problem is that the document/view closes, but there may be one or more modal dialogs and/or message boxes that remain open, and when the user dismisses them, there is an exception thrown. Is there a way to have the view go through all the modal dialogs that it opened and close them before it exits? I have tried paging through all child windows and posting WM_CLOSE messages, for both the closing view and the closing child frame, but the modal dialogs seem unaffected.
    Modal dialogs are not child windows, but in MFC their owner is usually the main application window. So maybe you could eneumerate all the windows in the system and check if their owner is your application, and if so then send the WM_CLOSE message.

    David Wilkinson | Visual C++ MVP
    Sunday, June 27, 2010 9:38 AM
  • Also try calling GetActiveWindow and closing dialogs while it returns a window of CDialog type (can be checked with DYNAMIC_DOWNCAST).

    Sunday, June 27, 2010 8:23 PM