Creating a CFormView View


Click here to change the theme.

The following is a sample of creating a view using CFormView in a MFC MDI application.

CMDIViewWindow Class

First, derive a class from CMDIChildWnd. I called mine CMDIViewWindow. Then override PreCreateWindow and LoadFrame. Also create a member variable m_hMenu as in:

HMENU m_hMenu;

PreCreateWindow

The following will remove the WS_THICKFRAME, WS_MINIMIZEBOX and WS_MAXIMIZEBOX styles. You might not want to remove the minimize and/or maximize styles.

BOOL CMDIViewWindow::PreCreateWindow(CREATESTRUCT& cs) {
cs.style &= ~WS_THICKFRAME;
cs.style |= WS_BORDER;
cs.style &= ~(WS_MINIMIZEBOX|WS_MAXIMIZEBOX);
return CMDIChildWnd::PreCreateWindow(cs);
}

LoadFrame

The following will do most of the work of creating the view.

BOOL CMDIViewWindow::LoadFrame(UINT nIDResource, CFrameWnd *pCurrentFrame,
		CRuntimeClass *pNewViewClass, DWORD dwDefaultStyle) {
CCreateContext Context;
Context.m_pCurrentFrame = pCurrentFrame;
Context.m_pNewViewClass = pNewViewClass;
if (!CMDIChildWnd::LoadFrame(nIDResource, dwDefaultStyle, NULL, &Context))
	return FALSE;
CString strFullString, strTitle;
if (strFullString.LoadString(nIDResource))
	AfxExtractSubString(strTitle, strFullString, CDocTemplate::docName);
SetTitle(strTitle);
m_hMenu = ::LoadMenu(AfxGetResourceHandle(), MAKEINTRESOURCE(nIDResource));
SetHandles(m_hMenu, NULL);
InitialUpdateFrame(NULL, TRUE);
return TRUE;
}

Using CMDIViewWindow

Use the CMDIViewWindow class in your Main Frame as in:

CMDIViewWindow* pMDIChildWnd = (CMDIViewWindow*) RUNTIME_CLASS(CMDIViewWindow)->CreateObject();
if (!pMDIChildWnd) {
	MessageBox("Error creating window object");
	return;
	}
if (!pMDIChildWnd->LoadFrame(IDD_DIALOG1, this,
		RUNTIME_CLASS(CYourView), WS_OVERLAPPEDWINDOW)) {
	MessageBox("Error creating window");
	delete pMDIChildWnd;
	}

Where:

IDD_DIALOG1
is the dialog control id of your form's dialog
CYourView
is the name of your CFormView-derived class. Notice that the class is not enclosed in quotes; RUNTIME_CLASS is a macro.