For many (probably most for most people) applications, the MFC Document/View Architecture is not flexible enough. It is designed assuming that data are in files such that, for example, the CFileDialog class is useful for determining the data to be stored and restored. In my opinion, the problem is not the document class or view classes; the problem is the other classes, in particular CSingleDocTemplate, CMultiDocTemplate and related classes such as the undocumented CDocManager class. Quite often, a dialog-based solution is used due to it's initial simplicity. However a dialog-based application can get unnecessarily complicated when it needs to provide capabilities that are provided by default by MFC SDI applications. This article describes how to convert an application that was generated by the AppWizard as a MFC SDI application without Document/View Architecture support to an application that uses a view. The result is an application that can be a reasonable alternative to a dialog-based solution.
I have attempted to be as accurate and thorough as possible but since this is the first version of these instructions there might be some corrections necessary. Please feel free to email me at "feedback" at SimpleSamples.Info. Some of the following must be done in the order described but not all must be done in this exact order. There are many alternative ways to do most of these things so you can certainly try other things too.
Note that I recommend using a document A document does not complicate things and can make some things easier; for one thing, it makes communication with the view from the application much easier.
CNonTemplatedDocument *m_pNonTemplatedDocument;
pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
With:
m_pNonTemplatedDocument = new CNonTemplatedDocument;
CCreateContext CreateContext;
CreateContext.m_pNewViewClass = RUNTIME_CLASS(CNonTemplatedView);
CreateContext.m_pCurrentFrame = pFrame;
CreateContext.m_pCurrentDoc = m_pNonTemplatedDocument;
if (!pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW,
NULL, &CreateContext)) {
AfxMessageBox("Frame Load Error");
return FALSE;
}
pFrame->InitialUpdateFrame(m_pNonTemplatedDocument, TRUE);
where:
- m_pNonTemplatedDocument
- is the pointer to the document
- CNonTemplatedView
- is the view class you created
| NonTemplatedDocument.cpp |
stdafx.h |
NonTemplatedDocument.h |
NonTemplated.h |
| NonTemplated.cpp |
stdafx.h |
NonTemplatedDocument.h |
NonTemplated.h |
NonTemplatedView.h |
MainFrm.h |
| NonTemplatedView.cpp |
stdafx.h |
NonTemplatedDocument.h |
NonTemplated.h |
NonTemplatedView.h |
| MainFrm.cpp |
stdafx.h |
NonTemplatedDocument.h |
NonTemplated.h |
NonTemplatedView.h |
MainFrm.h |
The project should compile and work after the changes above were made. There are miscellaneous improvements that are useful but this will get a basic application working. I will add suggestions for other improvements later.
Initial toolbar in main frame
Initial status bar in main frame
3D Controls
Uses shared DLL implementation (MFC42.DLL)
ActiveX Controls support enabled
Localizable text in: English [United States]
No Document/View
No Database support
Normal toolbars
Comments
Download NonTemplated.zip for the project I built to write this article. I added a few lines that show put data into the document and how to show the data in the view.
See my Visual C++ Programmer Stuff page for more C++ stuff.