Figure 5 CFORM
Doc.H
//////////////////
// Standard doc
//
class CMyDoc : public CDocument {
public:
virtual ~CMyDoc();
//{{AFX_VIRTUAL(CMyDoc)
public:
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL
protected:
CMyDoc();
DECLARE_DYNCREATE(CMyDoc)
//{{AFX_MSG(CMyDoc)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
View.H
////////////////
// Main fram handles sizing and overrides RecalcLayout
// to move view so that form appears centered.
//
class CMainFrame : public CFrameWnd {
public:
virtual ~CMainFrame();
protected:
DECLARE_DYNCREATE(CMainFrame)
CStatusBar m_wndStatusBar; // sataus bar window, as normal
CToolBar m_wndToolBar; // ditto for tool bar
// Following two are new CFORM stuff
CRect m_rcNormalView; // rectangle for normal (MFC) view pos
BOOL m_bCenterForm; // whether to center the form
BOOL m_bFixPaint; // whether to fix painting
CMainFrame();
// override to center form
virtual void RecalcLayout(BOOL bNotify = TRUE);
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint(); // override required to paint rectangles.
afx_msg void OnCenterForm();
afx_msg void OnUpdateCenterForm(CCmdUI* pCmdUI);
afx_msg void OnFixPaint();
afx_msg void OnUpdateFixPaint(CCmdUI* pCmdUI);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
MainFrm.h
////////////////
// Main fram handles sizing and overrides RecalcLayout
// to move view so that form appears centered.
//
class CMainFrame : public CFrameWnd {
public:
virtual ~CMainFrame();
protected:
DECLARE_DYNCREATE(CMainFrame)
CStatusBar m_wndStatusBar; // sataus bar window, as normal
CToolBar m_wndToolBar; // ditto for tool bar
// Following two are new CFORM stuff
CRect m_rcNormalView; // rectangle for normal (MFC) view pos
BOOL m_bCenterForm; // whether to center the form
BOOL m_bFixPaint; // whether to fix painting
CMainFrame();
// override to center form
virtual void RecalcLayout(BOOL bNotify = TRUE);
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint(); // override required to paint rectangles.
afx_msg void OnCenterForm();
afx_msg void OnUpdateCenterForm(CCmdUI* pCmdUI);
afx_msg void OnFixPaint();
afx_msg void OnUpdateFixPaint(CCmdUI* pCmdUI);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
MainFrm.cpp
#include "stdafx.h"
#include "cform.h"
#include "doc.h"
#include "view.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CMyFormView, CFormView)
BEGIN_MESSAGE_MAP(CMyFormView, CView)
//{{AFX_MSG_MAP(CMyFormView)
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CMyFormView::CMyFormView()
: CFormView(CMyFormView::IDD)
{
//{{AFX_DATA_INIT(CMyFormView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CMyFormView::~CMyFormView()
{
}
void CMyFormView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyFormView)
//}}AFX_DATA_MAP
}
int CMyFormView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
void CMyFormView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
ModifyStyle(WS_BORDER, 0);
ModifyStyleEx(WS_EX_CLIENTEDGE, 0);
}
Cform.h
////////////////////////////////////////////////////////////////
// CFORM Copyright 1996 Microsoft Systems Journal.
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// See CFORM.CPP for Description of program.
//
#include "resource.h" // main symbols
class CMyApp : public CWinApp {
public:
CMyApp();
//{{AFX_VIRTUAL(CMyApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
protected:
//{{AFX_MSG(CMyApp)
afx_msg void OnAppAbout();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
Cform.cpp
////////////////////////////////////////////////////////////////
// CFORM Copyright 1996 Microsoft Systems Journal.
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// CFORM illustrates how to center the controls in a CFormView.
// Interesting stuff is in mainfrm.cpp.
// Compiles with VC++ 4.0 or later.
#include "stdafx.h"
#include "cform.h"
#include "mainfrm.h"
#include "doc.h"
#include "view.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
//{{AFX_MSG_MAP(CMyApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
//}}AFX_MSG_MAP
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()
CMyApp::CMyApp()
{
}
CMyApp theApp;
BOOL CMyApp::InitInstance()
{
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMyDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CMyFormView));
AddDocTemplate(pDocTemplate);
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
if (!ProcessShellCommand(cmdInfo))
return FALSE;
return TRUE;
}
void CMyApp::OnAppAbout()
{
CDialog(IDD_ABOUTBOX).DoModal();
}