Click here to Skip to main content
11,631,525 members (71,661 online)
Click here to Skip to main content

Modal MFC frame window

, 9 Aug 2005 CPOL 115.6K 2.4K 43
Rate this:
Please Sign up or sign in to vote.
Utility class that helps to implement a modal dialog's behavior for any CFrameWnd-derived window class in an easy way.

Introduction

Sometimes, it may be useful to have a modal window with Doc/View architecture, toolbars/statusbar support (i.e. base features implemented in MFC frame windows). How to get these without implementing all of these features in a CDialog-derived class? The CModalFrame utility class presented here helps you implement a modal dialog's behavior for any CFrameWnd-derived window class in an easy way.

If you have to use it and have a bit of time, please add a comment at the bottom of this page and tell, in which context you decided to use this code. I would be interested.

Background

Some basic facts:

  • All required features are already implemented in CFrameWnd-derived classes.
  • The basic support of modal message loop is already implemented in the CWnd class.

Brief implementation description:

  1. The utility code uses only the CFrameWnd class interface and does not make any suggestions about the internal implementation details of any CFrameWnd-derived classes.
  2. CModalFrame::Run creates an instance of the internal CModalDialogLock class which:
    • stores modal loop context,
    • runs modal message loop and,
    • supports multiple running modal frames.
  3. CModalDialogLock subclasses the created frame window to provide:
    • update command UI for the frame on WM_KICKIDLE message,
    • exit from modal message loop on WM_NCDESTROY message and,
    • restore standard modal window behavior on WM_NCACTIVATE message.
  4. You should not use WS_CHILD window style for a frame. Because its parent window is disabled during modal loop. So child frame window also will be disabled (for example, MDI child frame)!

Using the code

  • Files needed:
    • ModalFrame.h
    • ModalFrame.cpp

Sample of usage:

void CMyView::OnRunModalFrame()
{
    CCreateContext context;
    context.m_pNewViewClass = RUNTIME_CLASS(CMyOtherView);
    context.m_pCurrentDoc = GetDocument();
    context.m_pNewDocTemplate = GetDocument()->GetTemplate();
    context.m_pLastView = this;
    context.m_pCurrentFrame = GetParentFrame();

    CRuntimeClass * pFrameClass = RUNTIME_CLASS(CMyModalFrame);

    if (!CModalFrame::Run(*pFrameClass, false, IDR_MYFRAMERESOURCE,
                          WS_OVERLAPPEDWINDOW | 
                          FWS_ADDTOTITLE, this, &context))
    {
        AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
    }
}

// don't forget close modal frame like a modal dialog box!
void CMyModalFrame::OnClose()
{
    int nModalResult = -1;
    CModalFrame::End(this, nModalResult);
}

See demo project code for more details.

History

  • Version 1.2
    • Bug fixing.
    • More accurate work with a stack of modal frames.
  • Version 1.1
    • Implemented support for multiple running modal frames.
    • More accurate restoring active window after the end of the modal message loop.
  • Version 1.0
    • Initial version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Alexey Nikitin
Team Leader Complex A5 Co. Ltd.
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionProblem through repeated loading of resource statuses from the registry. Pin
SNMP.H10-Jul-14 23:12
memberSNMP.H10-Jul-14 23:12 
QuestionIs it possible to pass parameters to my frame constructor? Pin
SNMP.H8-May-14 5:07
memberSNMP.H8-May-14 5:07 
AnswerRe: Is it possible to pass parameters to my frame constructor? Pin
Alexey Nikitin8-May-14 9:53
memberAlexey Nikitin8-May-14 9:53 
QuestionInsert controls to dialog Pin
SNMP.H29-Apr-14 20:15
memberSNMP.H29-Apr-14 20:15 
AnswerRe: Insert controls to dialog Pin
Alexey Nikitin8-May-14 10:54
memberAlexey Nikitin8-May-14 10:54 
QuestionCan it be possible to take a CMFCToolbar instead of CToolbar? Pin
Member 88337239-Apr-14 20:41
memberMember 88337239-Apr-14 20:41 
AnswerRe: Can it be possible to take a CMFCToolbar instead of CToolbar? Pin
Alexey Nikitin13-Apr-14 19:10
memberAlexey Nikitin13-Apr-14 19:10 
GeneralHow to use this class on WINCE project? Pin
NewttleLi2-Apr-13 23:16
memberNewttleLi2-Apr-13 23:16 
GeneralThank you Pin
_Jason23-May-12 16:10
member_Jason23-May-12 16:10 
GeneralThank you for your simple project Pin
Li Shu23-Oct-08 19:50
memberLi Shu23-Oct-08 19:50 
GeneralIs it possible to pass parameters to my view or frame after doModal and how! Pin
chiheb zak27-Feb-07 11:10
memberchiheb zak27-Feb-07 11:10 
AnswerRe: Is it possible to pass parameters to my view or frame after doModal and how! Pin
Alexey Nikitin12-Mar-07 7:52
memberAlexey Nikitin12-Mar-07 7:52 
GeneralRe: Is it possible to pass parameters to my view or frame after doModal and how! Pin
chiheb zak13-Mar-07 3:36
memberchiheb zak13-Mar-07 3:36 
GeneralWorks well but crashes when opening folder on desktop Pin
Wolfram Rösler31-Jan-07 3:22
memberWolfram Rösler31-Jan-07 3:22 
GeneralRe: Works well but crashes when opening folder on desktop Pin
Alexey Nikitin15-Feb-07 12:00
memberAlexey Nikitin15-Feb-07 12:00 
GeneralRe: Works well but crashes when opening folder on desktop Pin
Wolfram Rösler19-Feb-07 22:19
memberWolfram Rösler19-Feb-07 22:19 
GeneralVery useful and practical code Pin
skywalkerking28-Nov-06 7:57
memberskywalkerking28-Nov-06 7:57 
Generalaccess violation error with modal in modal Pin
Brad Ulrich17-May-05 8:07
memberBrad Ulrich17-May-05 8:07 
GeneralRe: access violation error with modal in modal Pin
Alexey Nikitin21-May-05 23:32
memberAlexey Nikitin21-May-05 23:32 
GeneralRe: access violation error with modal in modal Pin
Brad Ulrich2-Aug-05 4:19
memberBrad Ulrich2-Aug-05 4:19 
GeneralRe: access violation error with modal in modal Pin
Anonymous11-Aug-05 10:12
sussAnonymous11-Aug-05 10:12 
GeneralRe: access violation error with modal in modal Pin
Alexey Nikitin11-Aug-05 10:15
memberAlexey Nikitin11-Aug-05 10:15 
QuestionHow to embody Modeless Frame Window ? Pin
UriDokDo31-Mar-05 2:20
memberUriDokDo31-Mar-05 2:20 
GeneralModal CMDIFrameWnd Pin
Urban Olars17-Dec-04 11:17
memberUrban Olars17-Dec-04 11:17 
GeneralRe: Modal CMDIFrameWnd Pin
dirtcooker25-May-05 2:48
memberdirtcooker25-May-05 2:48 
GeneralPassing parameters to my frame class Pin
Urban Olars8-Dec-04 9:25
memberUrban Olars8-Dec-04 9:25 
GeneralRe: Passing parameters to my frame class Pin
Alexey Nikitin10-Dec-04 22:27
memberAlexey Nikitin10-Dec-04 22:27 
GeneralRe: Passing parameters to my frame class Pin
Urban Olars13-Dec-04 8:26
memberUrban Olars13-Dec-04 8:26 
Questionmodal from modal? Pin
bagsmode3-Aug-03 6:42
memberbagsmode3-Aug-03 6:42 
GeneralGood work, some remarks Pin
FransB23-Jun-03 20:33
memberFransB23-Jun-03 20:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150723.1 | Last Updated 9 Aug 2005
Article Copyright 2003 by Alexey Nikitin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid