MFC How to close a modeless dialog when switching views

256 Views Asked by At

I have a view that contains a modeless dialog.

Everything works fine except interacting with the dialog sends a message to modify objects on current active view. It causes crush when i switching to a new view or another view, because the object does not exist in that new view.

I want to ask how to close out the modeless dialog whenever the view is switched?

Should it be handled in the view class or document class?

The message route is Dialog ->send user defined message -> mainframe -> get current active view -> pass on the message -> view message handle receive the msg

Thanks

1

There are 1 best solutions below

0
lakeweb On

And, it may make more sense to do it in the frame of the view. So...

In the .h:

class MyFrm: public CFrameWnd
{
    MyDialog dlg;
};

in the .cpp:

MyFrm::MyFrm( )
    :dlg( this )
}

void MyFrm::OnInitialUpdate()
{
    tagDlg.Create( this );
    tagDlg.ShowWindow( SW_NORMAL );
}

And if you would want to toggle the dialog, say with a menu item as a switch. Instead of creating it in OnInitialUpdate, you could:

void MyFrm::OnToggleDlg( )
{
    if( ! dlg.GetSafeHwnd( ) )
    {
        dlg.Create( this );
        dlg.ShowWindow( SW_NORMAL );
    }
    else
        dlg.DestroyWindow( );
}

I hope that makes sense.