Convert a modeless dialog to modal at runtime

Javier De Pedro picture Javier De Pedro · Aug 4, 2009 · Viewed 8.3k times · Source

I have a dialog (CDialog derived class) that can be used in two different ways (edition mode and programming mode).

When the dialog is open to be used in programming mode it is a modeless dialog that it is used for modifying the main view (kind of a toolbar). When it is open in edition mode the user can change the configuration of the dialog itself and in this case it is a modal dialog.

Right now they are two different dialogs with few differences and I would like to have just want dialog and let the user change between programming mode and edition mode just by pressing a button in the dialog.

So I need to convert the modeless dialog in a modal dialog and vice versa at runtime. Is there a way to achive that?

Thanks.

Answer

Javier De Pedro picture Javier De Pedro · Aug 6, 2009

As maybe someone could be interested in doing something similar in the future, this is the way I eventually did it:

I use this two functions of main frame: CMainFrame::BeginModalState() and CMainFrame::EndModalState().

The problem with these functions is the same that with disabling the parent window. The window you want to make modal also gets disabled. But the solution is easy, just re-enable the window after calling BeginModalState.

void CMyDialog::MakeModal()
{
   //disable all main window descendants
   AfxGetMainWnd()->BeginModalState();

   //re-enable this window
   EnableWindow(TRUE);
}

void CMyDialog::MakeModeless()
{
   //enable all main window descendants
   AfxGetMainWnd()->EndModalState();
}

Thanks for your help.