Print Documents
Document objects provide an interface for rendering output to a printer device or file. The CPrintDoc class encapsulates the Windows DOCINFO structure, printer configuration data, and a printer device context (DC). The printer configuration data stored by CPrintDoc is used to create a device context for the printer on which the document will be printed. The printer device context is passed, along with the DOCINFO structure, to the Windows API function StartDoc(). The StartDoc() method returns a job identifier and ensures that output from multiple print jobs is not mixed together by the printer. In other words, the StartDoc() method ensures that documents are queued to the printer.
The CPrintDoc class actually maintains two DCs in order to support print preview: a print DC and an output DC. The print DC always represents a printer even if the output is sent to a window, as in the case with print preview. The output DC can represent either a window or a printer. In the case of print preview, the output DC represents a window. In the case of normal printing, the output DC and print DC are identical. Two DCs are needed because print preview emulates printing in a window, which means that two devices are involved, each with different characteristics—such as device resolution. The CPrintDoc class defines the GetOutputDC() and GetPrintDC() methods for accessing the two device contexts.
Printable objects use the device contexts maintained by the CPrintDoc class in order to render pages of output to the document. Notice that the BeginPage(), PrintPage(), and EndPage() methods of the IPrintable interface all take a CPrintDoc as a parameter. Printable objects also query the document for other information relevant to printing, such as the current page number.