View - worked example
key : purple - original code / blue - new code
Step 1 :
This worked example is a continuation of the program, names, from Task 3 (Creating a dialog box). It is suggested that a copy should be taken of the dialog program and that the following steps should be followed.
Step 2 :
Go into the resources and create a new diaolg box.
This should be given an identity (i.e. IDD_OUTPUT_NAMES_DLG). The style of this box must be changed from Pop-up to Child. This should be called CFormNameView and inherit from the CFormView base class.
Step 3 :
Member variables should be added to the edit boxes.
Edit Box 1 : CString, m_strFirstName
Edit Box 2 : CString, m_strSecondName
Step 4 :
Inform the new class about the Document :
#include "namesDoc.h"
Step 5 :
To display this new view the code within the MainFrame and the Application classes must be edited.
Add the following pointer to the CNamesApp class (private access) :
CMultiDocTemplate* m_pFormNameViewTemplate;
Step 6 :
Add the following code to the InitInstance() function within the CNamesApp class :
CMultiDocTemplate*
pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_NAMESTYPE,
RUNTIME_CLASS(CNamesDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CNamesView));
AddDocTemplate(pDocTemplate);
m_pFormNameViewTemplate = new CMultiDocTemplate(
IDR_NAMESTYPE,
RUNTIME_CLASS(CNamesDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CFormNameView));
//
create main MDI Frame window
Step 7 :
This new template created must be deleted in the destructor. Declare a destructor in the CNamesApp class's header. Add the following code :
CNamesApp();
~CNamesApp();
Define this destructor in the souce code file :
CNamesApp::CNamesApp()
{
}
CNamesApp::~CNamesApp()
{
delete m_pFormNameViewTemplate;
}
Step 8 :
Inform the Application about the new class :
#include "FormNameView.h"
Step 9 :
Add a return function to the Application (public access) :
CMultiDocTemplate* GetFormNameViewTemplate();
Define this function :
CMultiDocTemplate*
CNamesApp::GetFormNameViewTemplate()
{
return m_pFormNameViewTemplate;
}
Step 10 :
Add a handler to the menu bar so that this form view may be displayed. This handler should be given a name such as OnFormView() and this should be added to the CMainFrame class :
void
CMainFrame::OnFormView()
{
CMDIChildWnd* pActiveChild = MDIGetActive();
if( pActiveChild != 0 )
{CDocument* pDocument = pActiveChild->GetActiveDocument();
if( pDocument != 0 )
{CNamesApp* pApp = (CNamesApp*)AfxGetApp();
CDocTemplate* pTemp;
CFrameWnd* pFrame;
pTemp = pApp->GetFormNameViewTemplate();
pFrame = pTemp->CreateNewFrame(pDocument, pActiveChild);
if( pFrame != 0 )
{pTemp->InitialUpdateFrame(pFrame, pDocument);
}
}
}
}
Step 11 :
Create an OnDraw() function in the CFormNameView class. Add the following code :
void
CFormNameView::OnDraw(CDC* pDC)
{
m_strFirstName = ((CNamesDoc*)m_pDocument)->GetFirstName();
m_strSecondName = ((CNamesDoc*)m_pDocument)->GetSecondName();
UpdateData(FALSE);
}
Step 12 :
In order that the form view will update correctly add the following virtual functions to the CFormNameView class :
void
CFormNameView::OnUpdate(CView* pSender, LPARAM lHint, CObject*
pHint)
{
InvalidateRect( NULL );
}
void CFormNameView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
InvalidateRect( NULL );
}
Step 12 :
Include the following code within the AddNames() function of the Document :
void
CNamesDoc::AddNames()
{
CEnterNamesDlg dlg;
if(dlg.DoModal() == IDOK)
{m_strFirstName = dlg.m_strFirstName;
m_strSecondName = dlg.m_strSecondName;
UpdateAllViews( NULL );}
}
Click here for the basic exercise
Click here for the source code
Click here to return to menu