Click here to Skip to main content
Licence CPOL
First Posted 31 May 2003
Views 95,009
Downloads 1,511
Bookmarked 41 times

Modal MFC frame window

By Alexey Nikitin | 9 Aug 2005
Utility class that helps to implement a modal dialog's behavior for any CFrameWnd-derived window class in an easy way.

1

2

3
4 votes, 23.5%
4
13 votes, 76.5%
5
4.85/5 - 17 votes
μ 4.66, σa 0.77 [?]

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)

About the Author

Alexey Nikitin

Team Leader
Complex A5 Co. Ltd.
Russian Federation Russian Federation

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThank you for your simple project PinmemberLi Shu20:50 23 Oct '08  
GeneralIs it possible to pass parameters to my view or frame after doModal and how! Pinmemberchiheb zak12:10 27 Feb '07  
AnswerRe: Is it possible to pass parameters to my view or frame after doModal and how! PinmemberAlexey Nikitin8:52 12 Mar '07  
GeneralRe: Is it possible to pass parameters to my view or frame after doModal and how! Pinmemberchiheb zak4:36 13 Mar '07  
GeneralWorks well but crashes when opening folder on desktop PinmemberWolfram Rösler4:22 31 Jan '07  
GeneralRe: Works well but crashes when opening folder on desktop PinmemberAlexey Nikitin13:00 15 Feb '07  
GeneralRe: Works well but crashes when opening folder on desktop PinmemberWolfram Rösler23:19 19 Feb '07  
GeneralVery useful and practical code Pinmemberskywalkerking8:57 28 Nov '06  
Generalaccess violation error with modal in modal PinmemberBrad Ulrich9:07 17 May '05  
GeneralRe: access violation error with modal in modal PinmemberAlexey Nikitin0:32 22 May '05  
GeneralRe: access violation error with modal in modal PinmemberBrad Ulrich5:19 2 Aug '05  
GeneralRe: access violation error with modal in modal PinsussAnonymous11:12 11 Aug '05  
GeneralRe: access violation error with modal in modal PinmemberAlexey Nikitin11:15 11 Aug '05  
QuestionHow to embody Modeless Frame Window ? PinmemberUriDokDo3:20 31 Mar '05  
Hi..
 
Thanks for Your Present...
 
Given much helpiness to me
 
Supplementary..
Curiosity got the better of me, How to embody Modeless Frame Window ?
 

Good Luck ..
 

 

 

 
high
GeneralModal CMDIFrameWnd PinmemberUrban Olars12:17 17 Dec '04  
GeneralRe: Modal CMDIFrameWnd Pinmemberdirtcooker3:48 25 May '05  
GeneralPassing parameters to my frame class PinmemberUrban Olars10:25 8 Dec '04  
GeneralRe: Passing parameters to my frame class PinmemberAlexey Nikitin23:27 10 Dec '04  
GeneralRe: Passing parameters to my frame class PinmemberUrban Olars9:26 13 Dec '04  
Questionmodal from modal? Pinmemberbagsmode7:42 3 Aug '03  
GeneralGood work, some remarks PinmemberFransB21:33 23 Jun '03  

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.

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