Tutorials
|
|
Menu of tutorialsTutorial 1: The Simplest Window
In this tutorial we will demonstrate how to send a bitmap to a printer.
The bitmap in this case will be the one we've been drawing on in the view
window. The resulting printout will be resized to match the size of the
original drawing. This task can be broken down into several steps: Extract the bitmap from the view window When we draw on the view window, we are actually drawing on to a bitmap
attached to view window's device context. Here we will copy this bitmap to a
compatible bitmap. Choose a printer This step is fairly straight forward. We declare a PRINTDLG struct and
use this in the PrintDlg function. The PrintDlg function brings up a dialog,
allowing us to choose the printer, and stores its device context in the
PRINTDLG struct. Note: CPrintDialog::DoModal will throw a CResourceException if there is
no printer configured, or if the dialog can't be created. A try/catch block
should be used to catch an exception thrown by DoModal. Start the Print Job The StartDoc function should be called before sending output to a
printer. This function ensures that multipage documents are not interspersed
with other print jobs. StartPage (and a corresponding EndPage) is then
called for each page of printout. Extract the bitmap image data from the bitmap In order to use StretchDIBits we first need the bitmap image data. This
is retrieved by using the GetDIBits. It is called twice in the following
code sample, the first time to get the size byte array to hold the data, and
the second to fill the byte array. Copy the resized bitmap to the printer's device context StretchDIBits is the function used here to copy the bitmap information to
the printer's device context because the bitmap needs to be resized in order
to retain the same dimensions on the printed page as the original. The
following code segment shows how the scaling factors are calculated and the
StretchDIBits function is called. End the print job To finish the print job, EndPage is called to indicate that printing to
this page is complete, then EndDoc is called the end the print job. Accessing the CView from within CDoc The CDoc::Print uses GetView to access the CView class. GetView is
defined as follows. The source code for this tutorial is located within the Tutorial folder of the software available from SourceForge at http://sourceforge.net/projects/win32-framework. |