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...
- 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'. - 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 controls IDC_DFVCTRL1
, IDC_DFVCTRL2
, IDC_DFVCTRL3
, ...
- Create the dialog class associated with this resource (
CSampleDialog
). - 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)
- 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();
#ifdef _AFXDLL
Enable3dControls(); #else
Enable3dControlsStatic(); #endif
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings();
CSampleDialog Dlg;
Dlg.DoModal();
return TRUE;
}
(of course, #include "SampleDialog.h"
)
- 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).
- With the
ClassWizard
, create an InitDialog
method for the dialog and add the following code:
BOOL CSampleDialog::OnInitDialog()
{
CDialog::OnInitDialog();
m_DFVCtrl1.Create(this, IDC_DFVCTRL1,
IDR_MAINFRAME,
RUNTIME_CLASS(CSampleDoc),
RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(CSampleView),
WS_CHILD | WS_BORDER | WS_VISIBLE, 0L);
m_DFVCtrl2.Create(this, IDC_DFVCTRL2,
IDR_MAINFRAME,
RUNTIME_CLASS(CSampleDoc),
RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(CSampleView),
WS_CHILD | WS_BORDER | WS_VISIBLE, 0L);
m_DFVCtrl3.Create(this, IDC_DFVCTRL3,
IDR_MAINFRAME,
RUNTIME_CLASS(CSampleDoc),
RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(CSampleView),
WS_CHILD | WS_BORDER | WS_VISIBLE, 0L);
return TRUE; }
Of course, you have to include the doc/frame/view files:
#include "SampleDoc.h"
#include "SampleView.h"
#include "MainFrm.h"
- 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.
- 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.
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.