|
|
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:
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.
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)
网友评论:(评论内容只代表网友观点,与本站立场无关!) |
阅读排行
|