Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / MFC

Inserting a Doc/Frame/View in a Dialog using a Custom Control

Rate me:
Please Sign up or sign in to vote.
4.89/5 (35 votes)
12 Sep 20013 min read 320.6K   9.1K   129   59
A custom control allowing to insert a doc/view/frame architecture in a dialog/formview

Sample Image

Introduction

MFC architecture is often based on Doc/View/Frame architecture. Sometimes, it may be useful to have a doc/view/frame architecture in a dialog frame, or a formview. For example, if we want to edit a file (text, image, table, ...) in a dialog, or whatever. The set of class presented here can be useful.

For the time being, I have never seen such an architecture, that is why I decided to implement it. 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.

How That Works

The class CDFVCtrl is a custom control. Its Create method takes the following information:

  • Parent pointer (dialog/formview/...), the ID of the custom control (defined with the dialog editor - see below)
  • Doc/View/Frame class
  • style and extended style of the frame

The custom control creates a document template (CDFVDocTemplate). That allows us to benefit all the MFC implementation. Some of this implementation had to be reimplemented in order to satisfy our need. That's why we have the following classes:

  • CDFVDocument (base class: CDocument)
  • CDFVFrameWnd (base class: CFrameWnd)
  • CDFVDocTemplate (base class: CSingleDocTemplate)

How to Use It

Files needed:

  • DFVDocument.h and DFVDocument.cpp
  • DFVFrameWnd.h and DFVFrameWnd.cpp
  • DFVDocTemplate.h and DFVDocTemplate.cpp
  • DFVCtrl.h and DFVCtrl.cpp

Of course, you need to have Doc/Frame/View classes. In the demo project, these classes are called:

  • CSampleDoc/CMainFrame/CSampleView (I simply created an SDI project named "Sample")

A Sample...

  1. Create an SDI project and add the DFV files. If you want to see these files in the ClassWizard, close the project, delete the .clw and .ncb files, reopen the project, and open ClassWizard, Click 'yes' on the first dialog, 'Add All' on the second and 'OK'.
  2. Create a dialog resource and add PICTURE control on it (this method to create custom control is - I think - the simplest way to create a custom control).

    Image 2

    For each picture control, uncheck the "visible" style, and name these controls IDC_DFVCTRL1, IDC_DFVCTRL2, IDC_DFVCTRL3, ...

    Image 3

  3. Create the dialog class associated with this resource (CSampleDialog).
  4. In the Application class (SampleApp.cpp), remove the ONCOMMAND line:
    C++
    ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
    ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
    ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
    
  5. In the InitInstance method, remove the DocTemplate instructions, and the others following instructions. Add the initialization dialog instructions. You should have something like:
    C++
    BOOL CSampleApp::InitInstance()
    {
        AfxEnableControlContainer();
    
        // Standard initialization
        // If you are not using these features and wish to reduce the size
        // of your final executable, you should remove from the following
        // specific initialization routines you do not need.
    
    #ifdef _AFXDLL
        Enable3dControls();       // Call this when using MFC in a shared DLL
    #else
        Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endif
    
        // Change the registry key under which our settings are stored.
        // TODO: You should modify this string to be something appropriate
        // such as the name of your company or organization.
        SetRegistryKey(_T("Local AppWizard-Generated Applications"));
    
        LoadStdProfileSettings();  // Load standard INI file options (including MRU)
    
        // Register the application's document templates.  Document templates
        //  serve as the connection between documents, frame windows and views.
        CSampleDialog Dlg;
        Dlg.DoModal();
    
        return TRUE;
    }

    (of course, #include "SampleDialog.h")

  6. In the Dialog Class (CSampleDialog), #include "DFVCtrl.h" and add the following attributes:
    C++
    CDFVCtrl m_DFVCtrl1, m_DFVCtrl2, m_DFVCtrl3;

    (corresponding to the quantity of DFV control you want to add).

  7. With the ClassWizard, create an InitDialog method for the dialog and add the following code:
    C++
    BOOL CSampleDialog::OnInitDialog() 
    {
        CDialog::OnInitDialog();
        
        // TODO: Add extra initialization here
        m_DFVCtrl1.Create(this, IDC_DFVCTRL1,
            IDR_MAINFRAME,
            RUNTIME_CLASS(CSampleDoc),
            RUNTIME_CLASS(CMainFrame),       // main SDI frame window
            RUNTIME_CLASS(CSampleView),
            WS_CHILD | WS_BORDER | WS_VISIBLE, 0L);
    
        m_DFVCtrl2.Create(this, IDC_DFVCTRL2,
            IDR_MAINFRAME,
            RUNTIME_CLASS(CSampleDoc),
            RUNTIME_CLASS(CMainFrame),       // main SDI frame window
            RUNTIME_CLASS(CSampleView),
            WS_CHILD | WS_BORDER | WS_VISIBLE, 0L);
    
        m_DFVCtrl3.Create(this, IDC_DFVCTRL3,
            IDR_MAINFRAME,
            RUNTIME_CLASS(CSampleDoc),
            RUNTIME_CLASS(CMainFrame),       // main SDI frame window
            RUNTIME_CLASS(CSampleView),
            WS_CHILD | WS_BORDER | WS_VISIBLE, 0L);
        return TRUE;  // return TRUE unless you set the focus to a control
                      // EXCEPTION: OCX Property Pages should return FALSE
    }

    Of course, you have to include the doc/frame/view files:

    C++
    #include "SampleDoc.h"
    #include "SampleView.h"
    #include "MainFrm.h"
  8. Replace the base class for the following classes (do not forget to replace all the occurrences of the base class, you can reupdate the ClassWizard as seen previously in Step 1):
    • document class (CSampleDocument): CSampleDocument inherits from CDFVDocument
    • frame class (CMainFrame): CMainFrame inherits from CDFVFrameWnd

    Of course, you have to include the according files.

  9. Compile and execute. Funny, huh?

Known Bugs

  • Some keys seem to not be working in the second CEditView (Return, Tab, ...). May be it is a bug inside CEditView regarding this application.
  • The menu bar does not appear when the frame has a parent (if someone can tell me why...)

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.


Written By
United States United States
Professor in physics at Universty of Marne-La-Vallee (Paris, France).
Ex-Consultant (in Telecom) for Cap Gemini America at Atherton (Silicon Valley, San Francisco, CA USA).
I like very much coding.

Comments and Discussions

 
GeneralGood Code, but seems pretty useless Pin
Charlie Curtis18-Apr-04 16:36
Charlie Curtis18-Apr-04 16:36 
QuestionMDI version? Pin
cave coder25-Mar-04 7:03
cave coder25-Mar-04 7:03 
GeneralMenu handle resource leak Pin
Jim Tarrant15-Mar-04 0:21
Jim Tarrant15-Mar-04 0:21 
QuestionRe: Menu handle resource leak Pin
gotopt27-Aug-06 15:51
gotopt27-Aug-06 15:51 
GeneralENTER and TAB keys not working - solution Pin
Jim Tarrant5-Mar-04 2:07
Jim Tarrant5-Mar-04 2:07 
Generalsorry, I didn't say it clear Pin
zycmy61042-Sep-03 15:06
zycmy61042-Sep-03 15:06 
GeneralGood Code and View Pin
zycmy61042-Sep-03 14:59
zycmy61042-Sep-03 14:59 
QuestionWhat about the FormView ? Pin
Yasen Georgiew3-Jun-03 4:39
Yasen Georgiew3-Jun-03 4:39 
How can I use CFormView, instead of CDialog ? I mean that my app is SDI with FormView view. And I want to insert the custom control in this FormView, not in Dialog. How can I do that ?


AnswerRe: What about the FormView ? Pin
carmelo11-Dec-04 8:10
carmelo11-Dec-04 8:10 
GeneralGreat job! Pin
jrivero18-Mar-03 14:11
jrivero18-Mar-03 14:11 
QuestionHow can I resize controls on the dialog? Pin
JaroBartko10-Dec-02 0:46
JaroBartko10-Dec-02 0:46 
AnswerRe: How can I resize controls on the dialog? Pin
#realJSOP10-Dec-02 1:06
mve#realJSOP10-Dec-02 1:06 
GeneralRe: How can I resize controls on the dialog? Pin
amn18-Nov-03 3:15
amn18-Nov-03 3:15 
AnswerRe: How can I resize controls on the dialog? Pin
carmelo11-Dec-04 8:18
carmelo11-Dec-04 8:18 
Generalthere is a run-time error in debug version Pin
xue236-Oct-02 22:26
xue236-Oct-02 22:26 
QuestionHow can I open an URL later on? Pin
Liu, Yi25-Aug-02 16:53
sussLiu, Yi25-Aug-02 16:53 
AnswerRe: How can I open an URL later on? Pin
carmelo11-Dec-04 8:25
carmelo11-Dec-04 8:25 
Questioncan we do opposite Pin
4-Jun-02 2:46
suss4-Jun-02 2:46 
GeneralrejeeshGood work!!! Pin
27-Nov-01 3:46
suss27-Nov-01 3:46 
GeneralRe: rejeeshGood work!!! Pin
Liu, Yi25-Aug-02 17:03
sussLiu, Yi25-Aug-02 17:03 
Generalmemory leaks Pin
Mohamed KHADRAOUI17-Sep-01 5:44
Mohamed KHADRAOUI17-Sep-01 5:44 
GeneralRe: memory leaks Pin
15-Jan-02 21:53
suss15-Jan-02 21:53 
GeneralRe: memory leaks Pin
Paolo Vernazza3-Feb-02 13:23
Paolo Vernazza3-Feb-02 13:23 
GeneralRe: memory leaks Pin
Shawn Liu17-Apr-02 19:18
Shawn Liu17-Apr-02 19:18 
GeneralRe: memory leaks Pin
carmelo11-Dec-04 8:02
carmelo11-Dec-04 8:02 

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

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