{
ASSERT_VALID(pFrameWnd);
CMyPreviewView* pView = (CMyPreviewView*) pFrameWnd->GetDlgItem(AFX_IDW_PANE_FIRST);
ASSERT_KINDOF(CPreviewView, pView);
pView->OnPreviewClose();
return FALSE;
}
4、修改CMyPreviewView类,增加工具栏按钮的消息响应函数OnPreviewClose(),OnPreviewPrint(),由于此类较简单,列出该类代码如下(注意粗体部分):
MyPreviewView.h
// MyPreviewView.h: interface for the CMyPreviewView class.
//
////////////////////////////////////////
#if !defined(AFX_MYPREVIEWVIEW_H__0AE8B670_B1AE_11DA_812E_00E04C39032F__INCLUDED_)
#define AFX_MYPREVIEWVIEW_H__0AE8B670_B1AE_11DA_812E_00E04C39032F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afxpriv.h>
class CMyPreviewView : public CPreviewView
{
DECLARE_DYNCREATE(CMyPreviewView)
public:
afx_msg void OnPreviewClose();
protected:
CMyPreviewView();
virtual ~CMyPreviewView();
void OnDraw(CDC* pDC);
void CMyPreviewView::OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView);
afx_msg void OnPreviewPrint( );
DECLARE_MESSAGE_MAP( )
};
#endif // !defined(AFX_MYPREVIEWVIEW_H__0AE8B670_B1AE_11DA_812E_00E04C39032F__INCLUDED_)
MyPreviewView.cpp
// MyPreviewView.cpp: implementation of the CMyPreviewView class.
#include “stdafx.h”
#include “MyPrintPreviewDlg.h”
#include “MyPreviewView.h”
#include “MyFrame.h”
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////
IMPLEMENT_DYNCREATE(CMyPreviewView, CPreviewView)
CMyPreviewView::CMyPreviewView()
{
}
CMyPreviewView::~CMyPreviewView()
{
}
BEGIN_MESSAGE_MAP(CMyPreviewView, CPreviewView)
ON_COMMAND(AFX_ID_PREVIEW_CLOSE, OnPreviewClose)
ON_COMMAND(AFX_ID_PREVIEW_PRINT, OnPreviewPrint)
END_MESSAGE_MAP()
void CMyPreviewView::OnDraw(CDC *pDC)
{
CPreviewView::OnDraw(pDC);
m_pToolBar->PostMessage(WM_IDLEUPDATECMDUI, (WPARAM)TRUE);// 控制条的命令状态更新
}
void CMyPreviewView::OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView)
{
CPreviewView::OnEndPrintPreview(pDC, pInfo, point, pView);
}
void CMyPreviewView::OnPreviewClose()
{
CMyFrame* pf=(CMyFrame*)::AfxGetMainWnd();
CWinApp *pApp=AfxGetApp();
pApp->m_pMainWnd=pf->m_pOldW;
pf->DestroyWindow();
}
void CMyPreviewView::OnPreviewPrint()
{
m_pPrintView->SendMessage(WM_COMMAND, ID_FILE_PRINT);
}
至此,基于对话框应用程序的具有打印及打印预览的基本支持已经生成,完全由新生成的三个类来支持。正如上一部分介绍的,可以在CMyView类中定义CView类的几个在打印过程中虚拟函数(CView::OnPreparePrinting,CView::OnBeginPrinting,CView::OnPrepareDC,CView::OnPrint,CView::OnEndPrinting,具体内容可参见前一部分)来定制其打印或打印预览的内容。也可以将实现打印或打印预览新增的三个类,生成MFC扩展动态链接库,以方便加入到程序中。
参考文献:
[1][美]Michael J. Young.Visual C++6从入门到精通[M].美国:电子工业出版社.
[2]
[3]MSDN Library Visual Stud