按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
单命令项目和工具栏按钮得以发挥效用。函数内容直接拷贝自图13…5,只要
修改其中第14 行即可。这两个函数是本节的技术重点。
#0001 void CMainFrame::OnWindowText()
#0002 {
#0003 CMDIChildWnd* pActiveChild = MDIGetActive();
#0004 CDocument* pDocument;
#0005 if (pActiveChild == NULL ||
#0006 (pDocument = pActiveChild…》GetActiveDocument()) == NULL)
#0007 {
#0008 TRACE0(〃Warning: No active document for WindowNew mandn〃);
#0009 AfxMessageBox(AFX_IDP_MAND_FAILURE);
#0010 return; // mand failed
#0011 }
#0012
#0013 // otherwise we have a new frame!
#0014 CDocTemplate* pTemplate = ((CTextApp*) AfxGetApp())…》m_pTemplateTxt;
#0015 ASSERT_VALID(pTemplate);
#0016 CFrameWnd* pFrame = pTemplate…》CreateNewFrame(pDocument; pActiveChild);
#0017 if (pFrame == NULL)
#0018 {
#0019 TRACE0(〃Warning: failed to create new framen〃);
#0020 AfxMessageBox(AFX_IDP_MAND_FAILURE);
#0021 return; // mand failed
#0022 }
#0023
#0024 pTemplate…》InitialUpdateFrame(pFrame; pDocument);
#0025 }
#0026
#0027 void CMainFrame::OnWindowHex()
732
…………………………………………………………Page 795……………………………………………………………
第 13 章 多重文件與多重顯示
#0028 {
#0029 CMDIChildWnd* pActiveChild = MDIGetActive();
#0030 CDocument* pDocument;
#0031 if (pActiveChild == NULL ||
#0032 (pDocument = pActiveChild…》GetActiveDocument()) == NULL)
#0033 {
#0034 TRACE0(〃Warning: No active document for WindowNew mandn〃);
#0035 AfxMessageBox(AFX_IDP_MAND_FAILURE);
#0036 return; // mand failed
#0037 }
#0038
#0039 // otherwise we have a new frame!
#0040 CDocTemplate* pTemplate = ((CTextApp*) AfxGetApp())…》m_pTemplateHex;
#0041 ASSERT_VALID(pTemplate);
#0042 CFrameWnd* pFrame = pTemplate…》CreateNewFrame(pDocument; pActiveChild);
#0043 if (pFrame == NULL)
#0044 {
#0045 TRACE0(〃Warning: failed to create new framen〃);
#0046 AfxMessageBox(AFX_IDP_MAND_FAILURE);
#0047 return; // mand failed
#0048 }
#0049
#0050 pTemplate…》InitialUpdateFrame(pFrame; pDocument);
#0051 }
如果你要两个view 都有打印预视的能力,必须在CHexView 中改写下面三个
虚拟函数,至于它们的内容,可以依样画葫芦地从CTextView 的同名函数中拷
贝一份过来:
// in HEXVIEW。H
class CHexView : public CView
{
。。。
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CHexView)
protected:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC; CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC; CPrintInfo* pInfo);
//}}AFX_VIRTUAL
。。。
};
733
…………………………………………………………Page 796……………………………………………………………
第篇 深入 MFC 程式設計
// in HEXVIEW。CPP
BEGIN_MESSAGE_MAP(CHexView; CView)
//{{AFX_MSG_MAP(CHexView)
// NOTE the ClassWizard will add and remove mapping macros here。
//}}AFX_MSG_MAP
// Standard printing mands
ON_MAND(ID_FILE_PRINT; CView::OnFilePrint)
ON_MAND(ID_FILE_PRINT_DIRECT; CView::OnFilePrint)
ON_MAND(ID_FILE_PRINT_PREVIEW; CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////
// CTextView printing
BOOL CHexView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CHexView::OnBeginPrinting(CDC* /*pDC*/; CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CHexView::OnEndPrinting(CDC* /*pDC*/; CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
本例并未示范Serialization 动作。
非制式作法的缺点
既然是走后门,就难保哪一天出问题。如果MFC 的版本变动,
CMDIFrameWnd::OnWindowNew 内容改了,你就得注意本节这个方法还能适用否。
734
…………………………………………………………Page 797……………………………………………………………
第 1