Convert a modeless dialog to modal at runtime
Asked Answered
H

3

9

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.

Halinahalite answered 4/8, 2009 at 6:24 Comment(0)
H
14

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.

Halinahalite answered 6/8, 2009 at 9:52 Comment(0)
E
2

That can't be done easily without closing and reopening the dialog. Then you can call ShowWindow or DoModal as appropriate.

Endorse answered 4/8, 2009 at 6:43 Comment(0)
A
1

That is not correct. This can be done, if you look at MFC's source you will realize that it's modal dialogs are not technically even modal. You will have to do a lot of mucking about to make this work properly, but basically you just have to disable the parent of the 'modal' window, and re-enable it when the 'modal' window closes.

I have done this personally so this may work for you, though I am not exactly sure what you are trying to do.

Amie answered 4/8, 2009 at 20:22 Comment(1)
What do you mean with disable/enable the parent? I already tried to call GetParent()->EnableWindow(FALSE)/GetParent()->EnableWindow(TRUE), but this didn't work because my dialog also get disabled. The parent window happens to be the main frame I don't know if this is relevant. BTW: What I want to do is open a modeless dialog(Create/ShowWindow) when the user presses a button the dialog becomes modal. If the user presses the button again the dialog becomes modeless again.Halinahalite

© 2022 - 2024 — McMap. All rights reserved.