基于对话框打印程序--虚拟视图_第1页
基于对话框打印程序--虚拟视图_第2页
基于对话框打印程序--虚拟视图_第3页
基于对话框打印程序--虚拟视图_第4页
基于对话框打印程序--虚拟视图_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、基于对话框打印程序-采取虚拟的文档视图结构方法首先生成基于对话框的MFC程序,类名为CTestDlg; 往对话框上拖动2个按钮,一个事件为打印,一个为预览; void CTestDlg:OnBtnPrint() CPrintFrmWnd* WooPrintFrmWnd = new CPrintFrmWnd(1);void CTestDlg:OnBtnView() CPrintFrmWnd* WooPrintFrmWnd = new CPrintFrmWnd(0);然后按照以下步骤操作步骤1,从CView类派生新类CPrintView; 该类主要实现预览等需要重载函数OnPrepareDC On

2、PreparePrinting OnPrint等等#define HEADER_TOP 300/ 头部信息距离页面最顶端的距离class CPrintView : public CViewprotected:CPrintView(); / protected constructor used by dynamic creationDECLARE_DYNCREATE(CPrintView)/ AttributesUINT m_nPageNums;CFont m_FontHeader;/ 头部内容字体CFont m_FontMain;/ 主体内容字体CFont m_FontFooter;/ 尾部内

3、容字体CSize m_szView;/ 预览窗口大小UINT m_uHeadSmpGap;/ 头部信息和样本信息垂直方向间距UINT m_uMainLeftGap;/ 除头部内容外其他内容距左边的距离, 也是距离右边的距离PRINTINFO m_printInfo2;/ 存储需要打印的页内容public:void OnFilePrintPreview();void InitPrintInfo();void PrintContent(CDC *pDC, UINT nInex);/ Operationspublic:virtual void OnPrepareDC(CDC* pDC, CPrint

4、Info* pInfo);/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CPrintView)protected:virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnDraw(CDC* pDC); / overridden to draw this viewvirtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);/AFX_VIRTUAL/ Implementationpro

5、tected:virtual CPrintView();#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endif/ Generated message map functions/AFX_MSG(CPrintView)/AFX_MSGDECLARE_MESSAGE_MAP();实现如下:#include stdafx.h#include test.h#include PrintView.h#include PrintPreviewView.h#ifdef _DEB

6、UG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CPrintView/ 使类支持RUNTIMEIMPLEMENT_DYNCREATE(CPrintView, CView)/*/ Function name : _AfxMyPreviewCloseProc/ Description : 很关键,删除打印类资源/*/BOOL CALLBACK _AfxMyPreviewCloseProc(CFrameWnd* pFrameWnd)ASSERT_VALID(pFrameWnd);CPrintP

7、reviewView* pView = (CPrintPreviewView*) pFrameWnd-GetDlgItem(AFX_IDW_PANE_FIRST);ASSERT_KINDOF(CPreviewView, pView);pView-OnPreviewClose();return FALSE;/ CPrintView drawingvoid CPrintView:OnDraw(CDC* pDC)CDocument* pDoc = GetDocument();/ CPrintView diagnostics#ifdef _DEBUGvoid CPrintView:AssertVali

8、d() constCView:AssertValid();void CPrintView:Dump(CDumpContext& dc) constCView:Dump(dc);#endif /_DEBUG/ CPrintView message handlers/*/ Function name : CPrintView:CPrintView/ Description : 设定头及底部高度,字体等/*/CPrintView:CPrintView()m_uHeadSmpGap = 300;m_uMainLeftGap = 500;m_FontHeader.CreateFont(150, 80,

9、0, 0, FW_EXTRABOLD, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, _T(宋体);m_FontMain.CreateFont(100, 50, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, 宋体);m_FontFooter.CreateFont

10、(100, 50, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, 宋体);InitPrintInfo();void CPrintView:InitPrintInfo()/设置 头部CString strHeader(_T(省立医院检验报告单);m_printInfo0.SetHeader(strHeader);/ 设置样本信息PATINFO ptInfo;ptInfo.strAge = 25;ptInfo.st

11、rName = 卢正云;ptInfo.strSex = 女;m_printInfo0.SetPatInfo(ptInfo);/ 设置列标题信息vector vColTitle;ColTitlePos colTp;/ 设置项目及其结果信息-同一个人可能检验多个项目colTp.strColName = 项目名称;colTp.vValue.push_back(AFP);colTp.vValue.push_back(T3);colTp.vValue.push_back(CEA);vColTitle.push_back(colTp);colTp.ReSet();colTp.strColName = 检验

12、结果;colTp.vValue.push_back(20);colTp.vValue.push_back(5);colTp.vValue.push_back(68);vColTitle.push_back(colTp);colTp.ReSet();colTp.strColName = 参考值;colTp.vValue.push_back(0-10);colTp.vValue.push_back(2-10);colTp.vValue.push_back(10-100);vColTitle.push_back(colTp);m_printInfo0.SetvColumnTitle(vColTitl

13、e);/ 设置尾部TAIL tl;tl.strAudit = 何旭峰;tl.strSender = 夏小斌;CTime time = CTime:GetCurrentTime();tl.strSendTime.Format(%d-%d-%d, time.GetYear(), time.GetMonth(), time.GetDay();tl.strAudiTime.Format(%d-%d-%d, time.GetYear(), time.GetMonth(), time.GetDay();m_printInfo0.SetTail(tl);/ 设置第二个样本打印信息/设置 头部strHeade

14、r.Format(%s, _T(省立医院检验报告单);m_printInfo1.SetHeader(strHeader);/ 设置样本信息ptInfo.strAge = 28;ptInfo.strName = 刘鹏;ptInfo.strSex = 男;m_printInfo1.SetPatInfo(ptInfo);/ 设置列标题vColTitle.clear();colTp.ReSet();/ 设置项目及其结果信息-同一个人可能检验多个项目colTp.strColName = 项目名称;colTp.vValue.push_back(ESP);colTp.vValue.push_back(25V

15、ID);colTp.vValue.push_back(T4);colTp.vValue.push_back(Pep);colTp.vValue.push_back(USP);vColTitle.push_back(colTp);colTp.ReSet();colTp.strColName = 检验结果;colTp.vValue.push_back(10);colTp.vValue.push_back(25);colTp.vValue.push_back(18);colTp.vValue.push_back(15);colTp.vValue.push_back(10);vColTitle.pus

16、h_back(colTp);colTp.ReSet();colTp.strColName = 参考值;colTp.vValue.push_back(3-30);colTp.vValue.push_back(1-100);colTp.vValue.push_back(10-20);colTp.vValue.push_back(10-100);colTp.vValue.push_back(0-0);vColTitle.push_back(colTp);m_printInfo1.SetvColumnTitle(vColTitle);/ 设置尾部tl.strAudit = 何旭芳;tl.strSend

17、er = 夏小斌;time = CTime:GetCurrentTime();tl.strSendTime.Format(%d-%d-%d, time.GetYear(), time.GetMonth(), time.GetDay();tl.strAudiTime.Format(%d-%d-%d, time.GetYear(), time.GetMonth(), time.GetDay();m_printInfo1.SetTail(tl);CPrintView:CPrintView()if (&m_FontHeader)m_FontHeader.DeleteObject();if (&m_Fo

18、ntFooter)m_FontFooter.DeleteObject();if (&m_FontMain)m_FontMain.DeleteObject();BEGIN_MESSAGE_MAP(CPrintView, CView)ON_COMMAND(ID_FILE_PRINT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)/AFX_MSG_MAP(CPrintView)/AFX_MSG_MAPE

19、ND_MESSAGE_MAP()/*/ Description : 设定每页行数及总行数/*/BOOL CPrintView:OnPreparePrinting(CPrintInfo* pInfo)/ int nLines = 30;/行数/ m_nPageNums = (nLines%20 = 0 ? nLines/20 : (1+nLines/20) );/页数,每页约20行/ m_nPageNums = (m_nPageNums = 0 ? 1 : m_nPageNums);m_nPageNums = sizeof(m_printInfo) /sizeof(m_printInfo0);/

20、 设定总共页数pInfo-SetMinPage(0);pInfo-SetMaxPage(m_nPageNums);return DoPreparePrinting(pInfo);void CPrintView:OnFilePrintPreview( )CPrintPreviewState* pState = new CPrintPreviewState;pState-lpfnCloseProc =_AfxMyPreviewCloseProc;/设置打印预览窗口关闭时的调用函数;if(!DoPrintPreview(AFX_IDD_PREVIEW_TOOLBAR, this, RUNTIME_C

21、LASS(CPrintPreviewView), pState)TRACE0(Error: DoPrintPreview failed.n);AfxMessageBox(AFX_IDP_COMMAND_FAILURE);delete pState;/*/ Description : 设置dc相关内容/*/void CPrintView:OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) CView:OnPrepareDC(pDC, pInfo);if (pDC-IsPrinting() & pInfo!=NULL)void CPrintView:OnPrint(C

22、DC* pDC, CPrintInfo* pInfo) if (pInfo-m_nCurPage m_nPageNums)return;/CSize size;size.cx = abs(pInfo-m_rectDraw.Width();size.cy = abs(pInfo-m_rectDraw.Height();pDC-SetWindowExt(size);m_szView = size;int xLogPixPerInch = pDC-GetDeviceCaps(LOGPIXELSX); int yLogPixPerInch = pDC-GetDeviceCaps(LOGPIXELSY)

23、;/ 得到设备坐标和逻辑坐标的比例 long xExt = (long)size.cx * xLogPixPerInch/96 ; long yExt = (long)size.cy * yLogPixPerInch/96 ;pDC-SetViewportExt(int)xExt, (int)yExt);/ 打印主体内容for (UINT i = 1; i m_nCurPage = i)PrintContent(pDC, i - 1);void CPrintView:PrintContent(CDC *pDC, UINT nInex)ASSERT(nInex SelectObject(&m_F

24、ontHeader);/ 必须在计算字符串宽度之前将字体选到DC中CSize sz = pDC-GetTextExtent(strInfo);/ 获取当前系统汉字的高度uRowHeight = sz.cy;uHeaderLeft = m_szView.cx / 2 - sz.cx / 2;/ 头部信息水平方向居中pDC-TextOut(uHeaderLeft, uHeaderTop, strInfo);pDC-SelectObject(pOldFont);/ 显示样本信息pOldFont = pDC-SelectObject(&m_FontMain);uHeaderTop += m_uHead

25、SmpGap;/ 头部信息与样本信息间隔距离稍微大些/ 画横线线-起点横坐标为距离左边一个m_uMainLeftGap,终点X为m_szView.cx - m_uMainLeftGappDC-MoveTo(uOtherLeft, uHeaderTop);pDC-LineTo(m_szView.cx - m_uMainLeftGap, uHeaderTop);uHeaderTop += uRowHeight/2;/ 横线下面内容与横线的间隔strInfo.Format(姓名: %s 性别: %s 年龄: %s, m_printInfonInex.patInfo.strName, m_printI

26、nfonInex.patInfo.strSex, m_printInfonInex.patInfo.strAge);pDC-TextOut(uOtherLeft, uHeaderTop, strInfo);uHeaderTop += uRowHeight;pDC-MoveTo(uOtherLeft, uHeaderTop);pDC-LineTo(m_szView.cx - m_uMainLeftGap, uHeaderTop);/ 根据列的数目以及列信息以及纸张宽度,计算各列间空隙UINT uSize = m_printInfonInex.vColumnTitle.size();/得到一共有几

27、列if (0 = uSize)return;UINT uColTotalWidth = 0;/ 各列字符串的宽度和vector:iterator itrCol = m_printInfonInex.vColumnTitle.begin();for (; itrCol != m_printInfonInex.vColumnTitle.end(); itrCol+)ColTitlePos colTp = *itrCol;sz = pDC-GetTextExtent(colTp.strColName);uColTotalWidth += sz.cx;/ 计算列间距UINT uSpace = (m_s

28、zView.cx - m_uMainLeftGap * 2 - uColTotalWidth) / (uSize - 1);/ 设定各列输出的起始位置并输出各列 itrCol = m_printInfonInex.vColumnTitle.begin();uHeaderTop += uRowHeight/2;/ 横线下面内容与横线的间隔/ 各列的位置的Y值UINT uNextColPrintX = uOtherLeft;/ 第一列输出的起始位置UINT uRowCount = 0;for (; itrCol != m_printInfonInex.vColumnTitle.end(); itr

29、Col+)uRowCount = 0;UINT uPrinxY = uHeaderTop;ColTitlePos ctp = *itrCol;ctp.uPrintXPos = uNextColPrintX;CString strColName = ctp.strColName;pDC-TextOut(uNextColPrintX, uPrinxY, strColName);uPrinxY += uRowHeight;vector:iterator itrVal = ctp.vValue.begin();for (; itrVal != ctp.vValue.end(); itrVal+)pDC

30、-TextOut(uNextColPrintX, uPrinxY, (*itrVal);uPrinxY += uRowHeight;uRowCount+;sz = pDC-GetTextExtent(strColName);uNextColPrintX += sz.cx + uSpace;/ 显示横线uHeaderTop += uRowHeight * (uRowCount + 1);pDC-MoveTo(uOtherLeft, uHeaderTop);pDC-LineTo(m_szView.cx - m_uMainLeftGap, uHeaderTop);pDC-SelectObject(p

31、OldFont);/ 显示尾部pDC-SelectObject(&m_FontFooter);uHeaderTop += uRowHeight/2;/ 横线下面内容与横线的间隔strInfo.Format(送检者: %s 送检日期: %s, m_printInfonInex.tailInfo.strSender, m_printInfonInex.tailInfo.strSendTime);pDC-TextOut(uOtherLeft, uHeaderTop, strInfo);uHeaderTop += uRowHeight;strInfo.Format(审核者: %s 审核日期: %s,

32、m_printInfonInex.tailInfo.strAudit, m_printInfonInex.tailInfo.strAudiTime);pDC-TextOut(uOtherLeft, uHeaderTop, strInfo);uHeaderTop += uRowHeight;pDC-MoveTo(uOtherLeft, uHeaderTop);pDC-LineTo(m_szView.cx - m_uMainLeftGap, uHeaderTop);pDC-SelectObject(pOldFont);void CPrintView:PrintPageHeader(CDC* pDC

33、, CPrintInfo* pInfo)pDC-SetTextColor(RGB(255, 0, 255);CString strHeader(header);CSize sz = pDC-GetTextExtent(strHeader);pDC-TextOut(m_szView.cx / 2 - sz.cx / 2, 100, strHeader);void CPrintView:PrintPageFooter(CDC* pDC,CPrintInfo* pInfo)pDC-SetTextColor(RGB(0, 0, 255);CString strFoot(foot);CSize sz =

34、 pDC-GetTextExtent(strFoot);pDC-TextOut(m_szView.cx / 2 - sz.cx / 2, 1000, strFoot);步骤2:#include / 该类提供打印按钮在预览窗口上,打印按钮是预览窗口自定义好的/ 用户在预览窗口上点击打印按钮也也可实现打印功能class CPrintPreviewView : public CPreviewView protected:CPrintPreviewView();DECLARE_DYNCREATE(CPrintPreviewView)public:afx_msg void OnPreviewClose(

35、);afx_msg void OnPreviewPrint();/ Generated message map functionsprotected:/AFX_MSG(CPrintPreviewView)/AFX_MSGDECLARE_MESSAGE_MAP();实现如下:#include stdafx.h#include test.h#include PrintPreviewView.h#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE=_FILE_;#define new DEBUG_NEW#endif#include PrintFrmWn

36、d.h/ Construction/Destruction/IMPLEMENT_DYNCREATE(CPrintPreviewView, CPreviewView)CPrintPreviewView:CPrintPreviewView()BEGIN_MESSAGE_MAP(CPrintPreviewView, CPreviewView)/AFX_MSG_MAP(CPrintPreviewView)ON_COMMAND(AFX_ID_PREVIEW_CLOSE, OnPreviewClose)ON_COMMAND(AFX_ID_PREVIEW_PRINT, OnPreviewPrint)/AFX

37、_MSG_MAPEND_MESSAGE_MAP()/ CPrintPreviewView message handlers/*/ Function name : OnPreviewClose/ Description : /关闭预览窗口/*/void CPrintPreviewView:OnPreviewClose()CPrintFrmWnd* pFrame = (CPrintFrmWnd*) :AfxGetMainWnd();AfxGetApp()-m_pMainWnd = pFrame-m_pOldWnd;pFrame-DestroyWindow();/*/ Function name :

38、 CPrintPreviewView:OnPreviewPrint/ Description : /从预览窗口直接打印/*/void CPrintPreviewView:OnPreviewPrint()m_pPrintView-SendMessage(WM_COMMAND, ID_FILE_PRINT_DIRECT);/ID_FILE_PRINT_DIRECT步骤三:从类CFrameWnd派生新类CPrintFrmWnd#include PrintView.hclass CPrintFrmWnd : public CFrameWndDECLARE_DYNCREATE(CPrintFrmWnd)

39、public:BOOL m_bDirectPrint;CWnd* m_pOldWnd;CPrintView* m_pView;/用于保存视图类对象CPrintFrmWnd(); / protected constructor used by dynamic creationCPrintFrmWnd(BOOL bPrint); / protected constructor used by dynamic creation/ Operationspublic:/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIR

40、TUAL(CPrintFrmWnd)protected:virtual BOOL PreCreateWindow(CREATESTRUCT& cs);/AFX_VIRTUAL/ Implementationprotected:virtual CPrintFrmWnd();/ Generated message map functions/AFX_MSG(CPrintFrmWnd)afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);/AFX_MSGDECLARE_MESSAGE_MAP();实现如下:#include stdafx.h#incl

41、ude test.h#include PrintFrmWnd.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CPrintFrmWndIMPLEMENT_DYNCREATE(CPrintFrmWnd, CFrameWnd)CPrintFrmWnd:CPrintFrmWnd()/*/ Function name : CPrintFrmWnd:CPrintFrmWnd/ Description : BOOL bPrint true打印 false预览/*/CPrint

42、FrmWnd:CPrintFrmWnd(BOOL bPrint)m_bDirectPrint = bPrint;m_pOldWnd = AfxGetApp()-m_pMainWnd;if(!Create(NULL, 打印预览, WS_OVERLAPPEDWINDOW|FWS_ADDTOTITLE,CRect(200,200,600,600)TRACE0(Failed to create view window!n);CPrintFrmWnd:CPrintFrmWnd()m_pOldWnd-ShowWindow(SW_SHOW);BEGIN_MESSAGE_MAP(CPrintFrmWnd, C

43、FrameWnd)/AFX_MSG_MAP(CPrintFrmWnd)ON_WM_CREATE()/AFX_MSG_MAPEND_MESSAGE_MAP()/ CPrintFrmWnd message handlersBOOL CPrintFrmWnd:PreCreateWindow(CREATESTRUCT& cs) cs.style &= (WS_THICKFRAME);cs.style &= (WS_MAXIMIZEBOX);return CFrameWnd:PreCreateWindow(cs);int CPrintFrmWnd:OnCreate(LPCREATESTRUCT lpCr

44、eateStruct) if (CFrameWnd:OnCreate(lpCreateStruct) = -1)return -1; CCreateContext context;context.m_pNewViewClass = RUNTIME_CLASS(CPrintView);context.m_pCurrentFrame = this;context.m_pCurrentDoc = NULL;context.m_pLastView = NULL;m_pView = STATIC_DOWNCAST(CPrintView, CreateView(&context);if(m_pView != NULL) m_pView-ShowWindow(SW_SHOW);SetActiveView(m_pView);SetIcon(m_pOldWnd-GetIcon(FALSE),FALSE);SetIcon(m_pOldWnd-GetIcon(TRUE

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论