The V C++ GUI Framework:vPrintDC

From EDM2
Jump to: navigation, search

A printer drawing canvas.

Synopsis

Header:
<v/vprintdc.h>
Class name:
vPrintDC

Description

This drawing canvas can be used to draw to a printer. Like all drawing canvases, the available methods are described in [vdc.htm vDC]. A very effective technique for combining a printer DC and a screen DC is to pass a pointer to either a vCanvasPaneDC or a vPrintDC to the code that draws the screen. The same code can then be used to draw or print.

To successfully use a vPrintDC, your code must obtain the physical size of the page in units using GetPhysWidth and GetPhysHeight. On paper, these represent 1/72 inch points, and correspond very closely, but not exactly, to a pixel on the screen.

You must bracket the printing with calls to BeginPrinting and EndPrinting. Use BeginPage and EndPage to control paging. Note that the width of text will not necessarily be the same on a vCanvasPaneDC and a vPrintDC, even for the same fonts. Also, the size of the paper represents the entire page. Most printers cannot actually print all the way to the edges of the paper, so you will usually use vDC:SetTranslate to leave some margins. (Don't forget to account for margins when you calculate what can fit on a page.)

The implementation of vPrintDC is somewhat platform dependent. For X, vPrintDC represents a PostScript printer, and is derived from the class vPSPrintDC. For Windows, vPrintDC is derived from the vWinPrintDC class. To get platform independent operation for your application, use vPrintDC. On Windows, you can also use the PostScript version directly if you want by using the vPSPrintDC class, but the program will not conform to standard Windows behavior.

Methods

void SetPrinter(vPrinter& printer)

This method is used to associate a vPrinter with a vPrintDC. By default, a vPrintDC represents standard 8.5x11 inch Letter paper printed in black and white in portrait orientation. You can use vPrinter::Setup to allow the user to change the attributes of the printer, then use SetPrinter to associate those attributes with the vPrintDC. Note: If you change the default printer attributes, you must call SetPrinter before doing any drawing to the DC.

Example

This is a simple example taken from the VDraw demo program. Print is called to print the current drawing. Print calls vPrinter::Setup to set the printer characteristics, and then calls DrawShapes with a pointer to the vPrintDC. DrawShapes is also called to repaint the screen using the vCanvasPaneDC. By carefully planning for both screen and printer drawing, your program can often share drawing code in this fashion.

//===================>>> myCanvasPane::Print <<<=================
  void myCanvasPane::Print()
  {
    // Print current picture

    vPrintDC pdc;               // create a vPrintDC object
    vPrinter printer;           // and a printer to set attributes

    printer.Setup("test.ps");   // setup the printer
    pdc.SetPrinter(printer);    // change to the printer we setup

    if (!pdc.BeginPrinting())   // call BeginPrinting first
        return;

    pdc.SetTranslate(36,36);    // Add 1/2" (36 * 1/72") margins

    DrawShapes(&pdc);           // Now, call shared drawing method

    pdc.EndPrinting();          // Finish printing
  }

//===================>>> myCanvasPane::DrawShapes <<<=================
  void myCanvasPane::DrawShapes(vDC* cp)
  {
    // Common code for drawing both on Screen and Printer
    ...
  }

See Also

vPrinter