How to convert modeless dialog to modal dialog at runtime?

abc abc 351 Reputation points
2020-12-03T06:57:05.443+00:00

Hi,

In MFC MDI application , on clicking a menu I am launching a modal dialog and when clicking a button from this modal dialog I am launching modeless dialog and when clicking on a button from this modeless dialog I am launching a modal dialog., at this point before launching modal dialog I want to change modeless parent dialog to modal dialog? How to achieve it?

How to convert modeless dialog to modal dialog at runtime?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,540 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 40,756 Reputation points
    2020-12-03T17:44:27.647+00:00

    For example, try the following -

    MDI application launches first modal dialog -

    void CMainFrame::OnViewModal()
    {
        // TODO: Add your command handler code here
        CModal dlg(this);
    
        dlg.DoModal();
    }
    

    Button clicked on first modal dialog to launch modeless dialog. The modal dialog is the owner window of the modeless dialog. Dismissing the modal dialog will automatically close the modeless dialog.

    void CModal::OnBnClickedLaunch()
    {
        // TODO: Add your control notification handler code here
        CModeless *pModeless = new CModeless;
        pModeless->Create(IDD_MODELESS, this);
        pModeless->ShowWindow(SW_SHOW);
    }
    

    The user clicks a button on the modeless dialog to launch the second modal dialog. Before launching the second modal dialog, the modeless dialog disables its owner window and then enables it after DoModal() returns.

    void CModeless::OnBnClickedLaunchModal2()
    {
        // TODO: Add your control notification handler code here
        CModal2 dlg2(this);
        CWnd *pOwner = GetOwner();
        ASSERT(pOwner->IsKindOf(RUNTIME_CLASS(CModal))); // Make sure owner window is a CModal object
        pOwner->EnableWindow(FALSE); // Disable first modal dialog so it can't be dismissed
        dlg2.DoModal();  // Do second modal dialog
        pOwner->EnableWindow(TRUE); // Enable first modal dialog
    }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jeanine Zhang-MSFT 9,181 Reputation points Microsoft Vendor
    2020-12-03T08:03:40.76+00:00

    Hi,

    I suggest you could try to use CFrameWnd :: BeginModalState and CFrameWnd :: EndModalState .

    I suggest you could try to the following code:

    // disable all main window descendants  
    AfxGetMainWnd()->BeginModalState();  
      
    //re-enable this window  
    EnableWindow(TRUE);  
    

    Best Regards,

    Jeanine


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.