小辉程序员之路, since 1996 http://www.xiaohui.com
乐走天涯: 工作并快乐着,职业并休闲着
 » 首页 > 高级GUI编程源码: 文档视窗(DOC/VIEW)模型

从CFormView中进行打印


http://www.XiaoHui.com 日期: 2002-01-09 14:00

我们知道,缺省的CFormView类不能打印表单上的控件,本程序试图解决这个问题。

本程序的视窗类CPrintView派生于CFormView,并在其中增加了三个变量用于打印:

CDC * m_pMemDC; //A memory device context compatible with our printer DC.

CRect m_rect; //To hold the dimensions of our printing area while scaling.

CBitmap * m_pBm; //Capture the screen image as a Bitmap

在构造函数中初始化它们: CPrintView::CPrintView() : CFormView(CPrintView::IDD) { m_pMemDC = new CDC ; m_pBm = new CBitmap; //{{AFX_DATA_INIT(CPrintView) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT }

现在,重载CPrintView::OnBeginPrinting(..)函数:

void CPrintView::OnBeginPrinting(CDC* pDC, CPrintInfo* /*pInfo*/) { if (m_pMemDC->GetSafeHdc()) m_pMemDC->DeleteDC(); m_pMemDC->CreateCompatibleDC(pDC); CClientDC dc(this); CRect rect; GetClientRect(rect); m_pMemDC->SetMapMode(MM_ANISOTROPIC); m_pMemDC->SetWindowExt(dc.GetDeviceCaps(LOGPIXELSX),dc.GetDeviceCaps(LOGPIXELSY)); m_pMemDC->SetViewportExt(m_pMemDC->GetDeviceCaps(LOGPIXELSX),m_pMemDC->GetDeviceCaps(LOGPIXELSY)); if (m_pBm->GetSafeHandle()) m_pBm->DeleteObject(); m_pBm->CreateCompatibleBitmap(&dc,rect.Width(),rect.Height()); m_pMemDC->SelectObject(m_pBm); dc.DPtoLP(rect); //Convert to Logical Coordinates m_rect = rect; //Save Logical Coordinates m_pMemDC->BitBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,SRCCOPY); }

下一步,重载CPrintView::OnPrint(..)

void CPrintView::OnPrint(CDC* pDC, CPrintInfo*) { //The Following code scales the image based on printer resolution. pDC->SetMapMode(MM_ANISOTROPIC); pDC->SetWindowExt(m_pMemDC->GetDeviceCaps(LOGPIXELSX),m_pMemDC->GetDeviceCaps(LOGPIXELSY)); pDC->SetViewportExt(pDC->GetDeviceCaps(LOGPIXELSX),pDC->GetDeviceCaps(LOGPIXELSY)); pDC->StretchBlt(0,0,m_rect.Width(),m_rect.Height(),m_pMemDC,0,0,m_rect.Width(),m_rect.Height(),SRCCOPY); }

最后,在析构函数中撤除它们:

CPrintView::~CPrintView() { delete m_pMemDC; //CLEAN UP OUR VARIABLES delete m_pBm; }

>>> DOWN !!! >>>下载源代码及演示程序

Tags: VC 源码: DOC/VIEW


 文章评论

目前没有任何评论.

↓ 快抢占第1楼,发表你的评论和意见 ↓

发表你的评论
如果你想针对此文发表评论, 请填写下列表单:
姓名: * 必填
E-mail: 可选 (不会被公开)
网站 / Blog: 可选
反垃圾广告: 为了防止广告机器人自动发贴, 请计算下列表达式的值:
4 + 10 = * 必填
评论内容:
* 必填
你可以使用下列标签修饰文字:
[b] 文字 [/b]: 加粗文字
[quote] 文字 [/quote]: 引用文字

 

小辉程序员之路 建站于 1997 ◇ 做一名最好的开发者是我不变的理想……
Copyright(C) 1997-2009 XiaoHui.com   All rights reserved
声明:站内所有原创文字,未经许可,均可转载、复制。
转载时必须以链接形式注明作者和原始出处