OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
Creating a Printout Object

Creating a printout object is similar to writing a Paint member function for a window object: you use Windows' graphics functions to generate the image you want on a device context.

Just as the window object's display context takes care of interactions with the screen device, the printout object's device context insulates you from the printer device.

Creating printout objects

  • Derive a new object type from TPrintout that overrides the PrintPage member function. In very simple cases, that is all you need to do.
  • If the document has more than one page, you must also override the HasPage member function. It must return a non-zero value as long as there is another page to be printed. The current page number is passed as a parameter to PrintPage.

The printout object has fields that hold the size of the page and a device context that is already initialized to render to the printer. The printer object sets those values by calling the printout object's SetPrintParams member function. You should use the printout object's device context in any calls to Windows graphics functions.

Example

Here is the class TWindowPrintout, derived from TPrintout in the sample program PRINTING.CPP:

class TWindowPrintout : public TPrintout
{
public:
TWindowPrintout(const char* title, TWindow* window);
void GetDialogInfo(int& minPage, int& maxPage,
int& selFromPage, int& selToPage);
void PrintPage(int page, TRect& rect, unsigned flags);
void SetBanding(bool b) {Banding = b;}
bool HasPage(int pageNumber) {return pageNumber == 1;}
protected:
TWindow* Window;
bool Scale;
};

GetDialogInfo retrieves page-range information from a dialog box if page selection is possible. Since there is only one page, GetDialogInfo for TWindowPrintout looks like this:

TWindowPrintout::GetDialogInfo(int& minPage, int& maxPage,
int& selFromPage, int& selToPage)
{
minPage = 0;
maxPage = 0;
}

PrintPage must be overridden to print the contents of each page, band (if banding is enabled), or window. PrintPage for TWindowPrintout looks like this:

void TWindowPrintout::PrintPage(int, TRect& rect, unsigned)
{
// Conditionally scale the DC to the window so the printout
// will resemble the window
//
int prevMode;
TSize oldVExt, oldWExt;
if (Scale) {
prevMode = DC->SetMapMode(MM_ISOTROPIC);
TRect windowSize = Window->GetClientRect();
DC->SetViewportExt(PageSize, &oldVExt);
DC->SetWindowExt(windowSize.Size(), &oldWExt);
DC->IntersectClipRect(windowSize);
DC->DPtoLP(rect, 2);
}
// Call the window to paint itself
Window->Paint(*DC, false, rect);
// Restore changes made to the DC
if (Scale) {
DC->SetWindowExt(oldWExt);
DC->SetViewportExt(oldVExt);
DC->SetMapMode(prevMode);
}
}

SetBanding is called with banding enabled, as follows:

printout.SetBanding (true);

HasPage is called after every page is printed and by default returns false, which means that only one page will be printed. This function must be overridden to return true while pages remain in multipage documents.

See Also