MFC modeless and modal dialog

1.7k views Asked by At

I have an application with tray icon. There is a hidden main window (CMainFrm), which is used to process tray icon messages. The tray icon has a context menu: Settings, Help, Exit commands.

When user chooses Settings, the modeless settings dialog is displayed (parent: GetDesktopWindow()). Settings dialog has a browse button which displays MyBrowseFolderDialog as modal! So, there is a problem when this dialog is displayed and user tries to close application using Exit command from the tray menu.

Does anyone know how to gracefully close the application with all these dialogs? tray menu => Settings dialog (modeless) => BrowseDialog (modal)

2

There are 2 answers

0
Alex F On BEST ANSWER

Add CDialog* m_pModaldDlg member to Settings dialog class, initialize it to NULL in constructor. When MyBrowseFolderDialog is shown, set it to this dialog pointer:

MyBrowseFolderDialog dlg();
m_pModaldDlg = &dlg;
dlg.DoModal();
m_pModaldDlg = NULL;

In Exit message handler:

if ( m_pModaldDlg )
    m_pModaldDlg->EndDialog(0);
// Close settings dialog
0
Jeff On

Alex answer is still good - you have to store the m_pModalDlg in the CMainFrm so both Settings and Exit handlers can get to it as needed.

Some other possible solutions:

  1. Settings handler disables Exit option when Folder browse is active

  2. Register a custom message - have the Exit handler send this message to the browse folder (although you are still going to need some kind of window handle)

I like Alex's answer the best - just store the pointer somewhere in CMainFrm