- The CFrameWnd Derived Class
The class CMainFrame, derived from CFrameWnd, is the main frame window class for the Reader application. The frame window class defines the borders of the primary window and automatically positions and sizes the view window. It also "manages" the application adornments, such as menus, scroll bars, and toolbars.
Creating Objects Dynamically
The class declaration for CMainFrame includes the DECLARE_DYNCREATE macro to enable objects of CObject derived classes to be created dynamically at run time. DECLARE_DYNCREATE takes one parameter, the name of the class to be created dynamically, as shown in the following code:
class CMainFrame : public CFrameWnd
{
protected:
CMainFrame();
DECLARE_DYNCREATE(CMainFrame)
....
};
If the DECLARE_DYNCREATE macro is included in the class declaration, then the IMPLEMENT_DYNCREATE macro must be included in the class implementation. IMPLEMENT_DYNCREATE takes two parameters, the name of the class to be created dynamically and the name of its base class, as shown in the following code:
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
You can then use the RUNTIME_CLASS macro to create an object dynamically as shown in the CReader::InitInstance member function in the Reader.cpp file. The RUNTIME_CLASS macro takes one parameter, the name of the class, as shown in the following code:
pDocTemplate = new CSingleDocTemplate(....
RUNTIME_CLASS(CReaderDoc),....)
Creating the Window
The header file, CMainFrame.h, includes two member functions, PreCreateWindow and OnCreate, and two member variables, m_wndStatusBar and m_wndToolBar.
Before the window is created, the framework calls the PreCreateWindow member function. If you choose to override PreCreateWindow, you can determine whether the styles used in your application's base class provide the functionality you need by using information gathered from the MFC source code. In the following example, the framework simply calls the base class's PreCreateWindow:
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
return CFrameWnd::PreCreateWindow(cs);
}
Creating and Loading the Toolbar
The OnCreate member function creates and loads the toolbar using the member variable m_wndToolBar as follows:
if (!m_wndToolBar.Create(this) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
OnCreate creates and sets the status bar by executing the following code:
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
Docking the Toolbar
Finally, three member functions are called to dock the toolbar as shown in the following code:
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
The first line of code enables a control bar to be docked. The next line of code enables dockable control bars in a frame window. The last line of code causes a control bar to be docked to the frame window.
To see the source code for the files associated with the CMainFrame class, click this icon. For easy reference, the segments of code discussed in this section are highlighted in bold.
// MainFrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_MAINFRM_H__7EC9070B_F96E_11D0_B9C5_00AA00688598__INCLUDED_)
#define AFX_MAINFRM_H__7EC9070B_F96E_11D0_B9C5_00AA00688598__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
class CMainFrame : public CFrameWnd
{
protected: // create from serialization only
CMainFrame();
DECLARE_DYNCREATE(CMainFrame)
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMainFrame)
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
// Generated message map functions
protected:
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MAINFRM_H__7EC9070B_F96E_11D0_B9C5_00AA00688598__INCLUDED_)
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "Reader.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.Create(this) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Remove this if you don't want tool tips or a resizeable toolbar
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFrameWnd::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
No comments:
Post a Comment