您现在的位置: 中国教师站 >> 教师关注 >> 电脑技术 >> 编程技术 >> Visual C >> 正文

Google
Printing with Documents and Views
作者:Siny 文章来源:中国教师站cn-teacher 点击数: 更新时间:2007-4-22 13:05:33

Printing with Documents and Views

MFC's print architecture is built around a kernel formed by GDI printing functions and virtual CView member functions. To understand what's on the outside, it helps to first understand what's on the inside. Here's the approach we'll take in this chapter as we study the code that enables an MFC application to support printing and print previewing:

  • Look at the Windows printing model, and examine the steps an SDK-style application goes through to print a document.

  • Understand the relationship between the Windows print architecture and the MFC print architecture and the mechanics of printing from MFC applications.

  • Develop a bare-bones printing program that demonstrates how the same code can be used to send output to either the screen or the printer.

  • Develop a more ambitious printing program whose printing and previewing capabilities are on a par with those of commercial applications.

As you'll discover, printing from an MFC application isn't altogether different than rendering to the screen except for the fact that printed output must be paginated. Because MFC handles almost everything else, much of the effort you expend writing printing code will be devoted to figuring out where the page breaks go and how to position your output on the printed page.

The Windows Print Architecture

Printing a document from a Windows application without the benefit of MFC involves a number of steps. You begin by obtaining a device context (DC) for the printer that output will go to. Just as an application needs a screen DC to send output to the screen, it needs a printer DC to send output to a printer. If you know the device name of the printer you want to print to, you can create a device context yourself with the Win32 ::CreateDC function or MFC's CDC::CreateDC:

CDC dc;dc.CreateDC (NULL, _T ("HP LaserJet IIP"), NULL, NULL);

If you don't know the device name but would like the application to print to the default printer, you can use MFC's handy CPrintDialog::GetDefaults and CPrintDialog::GetPrinterDC functions to create the device context:

CDC dc;CPrintDialog dlg (FALSE);dlg.GetDefaults ();dc.Attach (dlg.GetPrinterDC ());

If you'd like to let the user select a printer, you can use CPrintDialog::DoModal to display a Print dialog (one of the common dialogs supplied by the operating system) and call CPrintDialog::GetPrinterDC to get a DC after the dialog is dismissed:

CDC dc;CPrintDialog dlg (FALSE);if (dlg.DoModal () == IDOK)



        

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 相关专题:
     
     网友评论:(评论内容只代表网友观点,与本站立场无关!)
    GOOGLE广告

    阅读排行

    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 |
    中国教师站

    中国教师站 版权所有 Copyright © 2006-2020 All Rights Reserved 站长:Sina & Siny
    [备用域名:www.JXZYW.Com] 有事请留言有事请留言
    【实力成就精品 诚信呵护品牌】

    信息产业部备案
    苏ICP备06018635号