Converting a CDialog to a CFormView |
|
You have just learned about CFormView. You want to convert a dialog (perhaps your dialog from your old dialog-based app) to be a CFormView, without having to rewrite everything.
This essay is based on a couple assumptions:
alternatively
By the time we are finished, you should see how we can extend this to multiple documents, views, etc.
To convert a dialog-based app to an SDI or MDI CFormView-based app, first generate an app with a CFormView. Move the projectView.h and projectView.cpp files to some other directory, and copy your oldprojectdlg.h and oldprojectdlg.cpp files over as the names proejctView.h and projectView.cpp files.
Open them up, and replace the class name with CprojectView as the class name; so if your dialog class was called CMyDlg it is renamed as CprojectView.
Note the name of the dialog of your CFormView class, such as IDR_PROJECTTYPE. Write this down somewhere.
Delete the dialog from your SDI/MDI project.
Copy the dialog from your old project to your new project.
Rename it to the name that been used, such as IDR_PROJECTTYPE
Now proceed to the Common Conversion Section
CMultiDocTemplate * YourClassNameHereTemplate;
YourClassNameHereTemplate= new CMultiDocTemplate( IDR_YOURCLASSNAMEHERE, RUNTIME_CLASS(YourExistingDocTemplateHere), RUNTIME_CLASS(CChildFrame), RUNTIME_CLASS(YourClassNameHere));
DO NOT call AddDocTemplate on this template!!!
#include "YourClassNameHere.h"
IDR_SOMETYPE nnn\nName\nName\n\n\nName.Document\nName Document
Just duplicate it and rename it to
IDRmmm\nName\nName\n\n\nName.Document\nName Document
Note there is no change in content. The content doesn't really matter at this point, nor is it likely it ever will.
CYourClassNameHeredlg; dlg.DoModal();
You now replace it with
NewView(YourClassNameHereTemplate);
CDocument * CYourAppNameApp::NewView(CMultiDocTemplate * templ) { CDocument * doc = GetActiveDocument(); if(doc == NULL) return NULL; CFrameWnd * frame = templ->CreateNewFrame(doc, NULL); if(frame == NULL) { /* failed to create */ return NULL; } /* failed to create */ templ->InitialUpdateFrame(frame, doc, TRUE); return doc; } // CFontExplorerApp::NewItem
Now proceed to the Common Conversion Section
IMPLEMENT_DYNCREATE(CYourClassName, CFormView)
DECLARE_DYNCREATE(CYourClassNameHere)
CYourClassName::CYourClassName(CWnd * pParent) : CDialog(CYourClassName::IDD, pParent)to
CYourClassName::CYourClassName() : CFormView(CYourClasName::IDD)
#ifdef _DEBUG void CYourClassNameHere::AssertValid() const { CFormView::AssertValid(); } void CYourClassNameHere::Dump(CDumpContext& dc) const { CFormView::Dump(dc); } #endif //_DEBUG
BOOL CYourClassNameHere::OnInitDialog() { CDialog::OnInitDialog(); ... return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }to be
void CYourClassNameHere::OnInitialUpdate() { CFormView::OnInitialUpdate(); ... // Remove the return TRUE; and the comments }
Compile and execute it.
The views expressed in these essays are those of the author, and in no way represent, nor are they endorsed by, Microsoft.