Click here to Skip to main content
6,594,432 members and growing! (15,840 online)
Email Password   helpLost your password?
Desktop Development » Document / View » Form Views     Intermediate

Inserting a Doc/Frame/View in a dialog using a custom control

By Jean-Louis Guenego

A custom control allowing to insert a doc/view/frame architecture in a dialog/formview
VC6, Windows, MFC, Dev
Posted:12 Sep 2001
Views:177,646
Bookmarked:96 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
39 votes for this article.
Popularity: 7.54 Rating: 4.74 out of 5

1

2
1 vote, 4.3%
3
1 vote, 4.3%
4
21 votes, 91.3%
5

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 never seen a such architecture, it 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 create 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 class:

  • 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 a SDI project named "Sample")

A sample...

  1. Create a 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)

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

  3. Create the dialog class associated with this resource (CSampleDialog).
  4. In the Application class (SampleApp.cpp), remove the ONCOMMAND line :
      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 :
    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
    
        //  the 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:
    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:
    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

    #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 like 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 seems not 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

About the Author

Jean-Louis Guenego


Member
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.
Location: United States United States

Other popular Document / View articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 52 (Total in Forum: 52) (Refresh)FirstPrevNext
GeneralGood~~ Help Pinmembercsb19:55 18 Feb '08  
GeneralResizing the View automatically when the Control is resized PinmemberIain Clarke23:17 13 Aug '07  
QuestionCompare two files ? PinmemberTrangLy8:56 10 Jun '07  
Generali use this code because: Pinmembergergo835:42 4 Apr '07  
Generalhow to acces each view Pinmemberaldo hexosa18:08 23 Aug '06  
GeneralHow to get the pointer of Dialog in the View class Pinmemberfuchies16:45 19 Aug '06  
GeneralRe: How to get the pointer of Dialog in the View class Pinmemberalex12318:30 18 May '08  
Generalvery good Pinmembernightmouse20:26 1 Aug '06  
GeneralMemory Leaks Pinmember089890880:24 11 Jul '06  
AnswerRe: Memory Leaks PinmemberPanicMan0:26 4 Feb '09  
GeneralPrint Preview Pinmemberbbianco10:25 11 Apr '06  
GeneralHow to... Doc View FROM a dialog (not inside a dialog)? PinmemberRajeswari, T.17:57 30 Mar '06  
GeneralVery useful article Pinmemberbobibe20:15 15 Feb '06  
QuestionMDI concept Pinmember_pw17:21 26 Dec '05  
Generalthis does not work for Cscrollview class, class has different memory address it is functions Pinmembersteve39fl3:05 23 Nov '05  
Generalvery good Pinmemberhanlwang18:34 25 Oct '05  
Generalvery good Pinmemberhanlwang18:32 25 Oct '05  
GeneralGood thoughts, I will use your idea in my project. Pinmemberpingdee8:06 16 May '05  
GeneralGood Code, but seems pretty useless PinmemberCharlieC17:36 18 Apr '04  
GeneralMDI version? Pinmemberdavem18:03 25 Mar '04  
GeneralMenu handle resource leak PinmemberJim Tarrant1:21 15 Mar '04  
QuestionRe: Menu handle resource leak Pinmembergotopt16:51 27 Aug '06  
GeneralENTER and TAB keys not working - solution PinmemberJim Tarrant3:07 5 Mar '04  
Generalsorry, I didn't say it clear Pinmemberzycmy610416:06 2 Sep '03  
GeneralGood Code and View Pinmemberzycmy610415:59 2 Sep '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Sep 2001
Editor: Nishant Sivakumar
Copyright 2001 by Jean-Louis Guenego
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project