Printing Tips and Tricks
Here Are A Few Tips, Tricks, and answers to frequently asked Questions to help you write better printing code and resolve problems That Aren't Addressed in this Chapter's sample program.
Using the print Dialog's Selection Button
The Print dialog that MFC displays before printing begins includes a Selection radio button that the user can click to print the current selection rather than the entire document or a range of pages. By default, the button is disabled. You can enable it by adding the FOLLOWING Statement to Onprepareprinting Just Before The Call To DopRepareprinting:
Pinfo-> m_ppd-> m_pd.flags & = ~ pd_noselection;
To select the Radio Button After It's Enabled, Add this Statement As Well:
PINFO-> m_ppd-> m_pd.flags | = pd_selection;
The m_pPD data member of the CPrintInfo structure passed to OnPreparePrinting points to the CPrintDialog object that DoPreparePrinting uses to display the Print dialog box. CPrintDialog :: m_pd holds a reference to the PRINTDLG structure the dialog is based on, and PRINTDLG's Flags field holds bit flags that define the dialog box's properties. Removing the PD_NOSELECTION flag added by CPrintInfo's constructor enables the Selection button, and adding a PD_SELECTION flag selects the button. If DoPreparePrinting returns a nonzero value, indicating that the dialog was dismissed with the OK button, you can find Out WHETHER THE Selection Button Was Selected by Calling CprintDialog :: PrintSelection. A Nonzero Return Value Means The Button Was Selected; 0 means it wasn't:
IF (Pinfo-> m_ppd-> printselection ()) {
//Print the current selection.
}
You can call PrintSelection and other CPrintDialog functions that return information about settings entered in a Print or Print Setup dialog through the pInfo parameter passed to OnPreparePrinting after DoPreparePrinting returns. You can also call them through the pInfo parameter passed to OnBeginPrinting and other CView print overridables. You can use CPrintInfo :: m_pPD in other ways to modify the appearance and behavior of the Print dialog that DoPreparePrinting displays. Refer to the documentation that accompanies Visual C for more information about PRINTDLG and its data members.
Assume Nothing-And Test Thoroughly!
When you send output to the printed page, it's generally a mistake to assume anything about the printable area of the pages you'll be printing. Even if you know you're printing to, say, an 8½-by-11-inch page , the printable page area will differ for different printers. The printable page area can even differ for the same printer and the same paper size depending on which printer driver is being used, and the horizontal and vertical dimensions of the printable page area will be switched if the user opts to print in landscape rather than portrait mode. Rather than assume you have a given amount of space to work with, do as HexDump does and call GetDeviceCaps through the CDC pointer provided to CView print functions to determine the printable page area each time you print, or use CPrintInfo :: m_rectDraw in your OnPrint function. This simple precaution will enable your printing code to work with any printer Windows can throw at it and will greatly reduce the number of problem reports you rec EIVE from Users.
. As you've already learned, calling GetDeviceCaps with HORZRES and VERTRES parameters returns the horizontal and vertical dimensions of the printable page area You can pass the following values to GetDeviceCaps to get more information about a printer or other hardcopy device:
ValueDescriptionHORZRESReturns the width of the printable page area in pixels.VERTRESReturns the height of the printable page area in pixels.HORSIZEReturns the width of the printable page area in millimeters.VERTSIZEReturns the height of the printable page area in millimeters.LOGPIXELSXReturns the number of pixels per inch in the horizontal direction (300 for a 300-dpi printer) .LOGPIXELSYReturns the number of pixels per inch in the vertical direction (300 for a 300-dpi printer) .PHYSICALWIDTHReturns the page width in pixels (2,550 for an 8½-by- 11-inch page on a 300-dpi printer) .PHYSICALHEIGHTReturns the page height in pixels (3,300 for an 8½-by-11-inch page on a 300-dpi printer) .PHYSICALOFFSETXReturns the distance in pixels from the left side of the page to the beginning of the page's printable area.PHYSICALOFFSETYReturns the distance in pixels from the top of the page to the beginning of the page's printable area.TECHNOLOGYReturns a value that identifies the type of output device the D C pertains to. The most common return values are DT_RASDISPLAY for screens, DT_RASPRINTER for printers, and DT_PLOTTER for plotters.RASTERCAPSReturns a series of bit flags identifying the level of GDI support provided by the printer driver. For example, an RC_BITBLT flag indicates that the printer supports BitBlts, and RC_STRETCHBLT indicates that the printer supports StretchBlts.NUMCOLORSReturns the number of colors the printer supports. The return value is 2 for black-and-white printers.
You've already seen one use for the GetDeviceCaps NUMCOLORS parameter: to detect when a black-and-white printer is being used so that you draw print previews in shades of gray The PHYSICALOFFSETX and PHYSICALOFFSETY parameters are useful for setting margin widths based on. information the user enters in a Page Setup dialog. (MFC's CWinApp :: OnFilePrintSetup function displays a Print Setup dialog instead of a Page Setup dialog, but you can display a Page Setup dialog yourself using MFC's CPageSetupDialog class.) If the user wants 1- inch margins on the left side of the page, for example, you can subtract the PHYSICALOFFSETX value returned by GetDeviceCaps from the number of pixels printed per inch (LOGPIXELSX) to compute the x offset from the left of the printable page area where printing should begin ........................................................................................................................... SiCalWidth, PhysicalHeight, PhysicalOffsetx, and PhysicaloffSetx, And PhysicaloffSety Values to Characterize the Printable Area of a page and pinpoint exactly where on the page the printable area.
If you're concerned about the occasional hardcopy device that will not draw bitmaps, you can find out whether CDC :: BitBlt and CDC :: StretchBlt are supported by calling GetDeviceCaps with a RASTERCAPS parameter and checking the return flags. For the most part , only vector devices such as plotters do not support the GDI's Blt functions. If the driver for a raster device does not support blitting directly, the GDI will compensate by doing the blitting itself. You can determine outright whether printed output is destined for A Plotter by Calling GetDeviceCaps with a technology. Parameter and checking to see et_plotter.
When you use a number of different printers to test an application that prints, you'll find that printer drivers are maddeningly inconsistent in the information they report and the output they produce. For example, some printer drivers return the same values for PHYSICALWIDTH and PHYSICALHEIGHT as they return for HORZRES and VERTRES. and sometimes an ordinary GDI function such as CDC :: TextOut will work fine on hundreds of printers but will fail on one particular model because of a driver bug. Other times, a GDI function will not fail outright but will behave differently on different printers. I once ran across a printer driver that defaulted to the TRANSPARENT background mode even though other drivers for the same family of printers correctly set the device context's default background mode to OPAQUE. Printer drivers are notoriously flaky, SO You NEED TO ANTICIPATE Problems and test as thoroughly as you can on as aprinters as possible. The more armitious your program's printing needs, The more likely that driver quirks will request, you to write us, om, copy, printers.adding default Pagination Support
HexDump calls CPrintInfo :: SetMaxPage from OnBeginPrinting rather than from OnPreparePrinting because the pagination process relies on the printable page area and OnBeginPrinting is the first virtual CView function that's called with a pointer to a printer DC. Because the maximum page number is not set until after OnPreparePrinting returns, the From box in the Print dialog is filled in (with a 1) but the To box is not. Some users might think it incongruous that an application can correctly paginate a document for print preview but can not fill in the maximum page number in a dialog box. in addition to displaying the maximum page number correctly, many commercial applications display page breaks outside print preview and "Page mm of nn" strings in status bars. How do these applications know how the document will be Page desk, sing, sing st guess based on the properties of the default printer. The following code snippet initializes a CSize object with the pixel dimensions of the printable page area on the default printer or the last printer that the user selected in Print Setup. You can call it from OnPreparePrinting OR elsewhere to compute a page count or to get the information you need to provide Other Forms of default Pagination Support:
CSIZE SIZE;
CPrintinfo Pi;
IF (AfxGetApp () -> getprinterDeviceDefaults (& pi.m_ppd-> m_pd)) {
HDC HDC = pi.m_ppd-> m_pd.hdc;
IF (HDC == NULL)
HDC = pi.m_ppd-> createprinterdc ();
IF (hdc! = null) {
CDC DC;
Dc.attach (HDC);
Size.cx = dc.getDeviceCaps (Vertres);
Size.cy = dc.getDeviceCaps (Horzres);
:: deletedc (dc.detach ());
}
}
CWinApp :: GetPrinterDeviceDefaults initializes a PRINTDLG structure with values describing the default printing configuration. A 0 return means that the function failed, which usually indicates that no printers are installed or that a default printer has not been designated. CPrintInfo :: CreatePrinterDC creates a device context handle from the information in the PRINTDLG structure encapsulated in a CPrintInfo object. With the device context in hand, it's a simple matter to wrap it in a CDC object and use CDC :: GetDeviceCaps to measure the printable page area.
ENUMERATING Printers
Sometimes it's useful to be able to build a list of all the printers available so that the user can select a printer outside a Print or Print Setup dialog box. The following routine uses the Win32 :: EnumPrinters function to enumerate the printers currently installed and adds An entry for each to the combo box pointd to by pcomboBox.
#include
DWORD DWSIZE, DWPRINTERS;
:: ENUMPRINTERS (Printer_enum_local, NULL, 5,
NULL, 0, & DWSIZE, & DWPRINTERS;
BYTE * PBUFFER = New byte [dwsize];
:: ENUMPRINTERS (Printer_enum_local, NULL, 5,
PBuffer, DWSIZE, & DWSIZE, & DWPRINTERS
IF (dwprinters! = 0) {
PRINTER_INFO_5 * PPRNINFO = (Printer_INFO_5 *) PBuffer;
For (uint i = 0; i PCOMBOBOX-> AddString (PPRNINFO-> PprinterName); PPRNINFO ; } } delete [] PBuffer; The first call to :: EnumPrinters retrieves the amount of buffer space needed to hold an array of PRINTER_INFO_5 structures describing individual printers. The second call to :: EnumPrinters initializes the buffer pointed to by pBuffer with an array of PRINTER_INFO_5 structures. On return, dwPrinters holds a count of the printers enumerated (which equals the count of PRINTER_INFO_5 structures copied to the buffer), and each structure's pPrinterName field holds a pointer to a zero-delimited string containing the device name of the associated printer. Enumerating printers with PRINTER_INFO_5 structures is fast because no remote calls are required;. all information needed to fill the buffer is obtained from the registry For fast printer enumerations in Windows NT or Windows 2000, use PRINTER_INFO_4 structures instead.If a printer is selected from the combo box and you want to Create a device context for it, you can pass the device name copied from the printer_info_5 structure to cdc :: create DC As Follows: Cstring strprintername; INT NINDEX = PCOMBOBOX-> getcursel (); PCOMBOBOX-> getlbtext (nindex, strprintername); CDC DC; Dc.createdc (NULL, STRPRINTERNAME, NULL, NULL); You can use the resulting cdc Object Just Like The CDC Objects Whose Addresses Are Passed to Onbeginprinting and other CView Print functions.