Printing


The System::Drawing::Printing namespace contains the classes that implement the printing functionality within GDI+. Printing isn’t particularly hard, but it can be a rather involved process because of the number of classes involved.

The first class you’ll meet when printing is PrintDocument. A PrintDocument object represents your link with a printer and can be used for more than one print job. PrintDocument has four main properties:

  • DefaultPageSettings, which gets or sets a PageSettings object that represents the default page settings

  • DocumentName, which represents the document name

  • PrintController, which gets or sets a PrintController object that controls the printing process

  • PrinterSettings, which gets or sets a PrinterSettings object that controls where and how the document is printed

The PrinterSettings and PageSettings classes hold data about the printer to be used—such as whether different paper trays are available—and PageSettings also holds data about the setup of the document, such as page orientation and number of copies.

The following exercise shows you how to use the printing functionality within GDI+. You’ll use the existing application you’ve been developing during this chapter and add a menu item that will let you print the contents of the form.

  1. Add a using directive for the System::Drawing::Printing namespace to the other using directives at the top of the code.

  2. Add a menu to the form by dragging a MainMenu from the Toolbox onto the form. Create a File menu, and add two menu items: one named printItem with the text &Print, and a second named exitItem with the text E&xit. Add a separator bar between the printItem and exitItem.

    Note

    Setting up menus like this should be familiar to you by now. If you need to refresh your knowledge of how menus work, see Chapter 16.

  3. Use the Properties editor to set up event handlers for the Print and Exit menu items.

  4. Add the code for the printItem_Click handler function.

    private: System::Void printItem_Click(System::Object * sender, System::EventArgs * e) { // The PrintDocument holds the settings PrintDocument* pdoc = new PrintDocument(); // Create a dialog and attach it to the document PrintDialog* pd = new PrintDialog(); pd->Document = pdoc; // Show the dialog if (pd->ShowDialog() == DialogResult::OK) { // Add the page handler pdoc->PrintPage += new PrintPageEventHandler(this, &Form1::PrintAPage); // Print the page pdoc->Print(); } else MessageBox::Show("Print cancelled", "Information"); } private: System::Void exitItem_Click(System::Object * sender, System::EventArgs * e) { Application::Exit(); }

    The handler code determines which menu item raised the event. If it was the Exit item, the application exits. If it was the Print item, a PrintDialog gets displayed; PrintDialog is one of the common dialog boxes, and it will display the familiar printer selection dialog box shown in the following figure.

    click to expand

    The purpose of this dialog box is to gather the information needed for the printing process; this information is stored in a PrintDocument object for later use. So, before you display the PrintDialog, you need to create a PrintDocument object and assign it to the dialog box’s Document property. You can then show the PrintDialog, and when the user dismisses the dialog box, check the return value to see whether you should continue printing.

    The PrintDocument controls the printing process, so you call its Print function to start printing. You also need to provide a callback function that PrintDocument will call once for each page that needs to be printed. You add the callback in the normal way, by adding an event handler to the PrintDocument.

    Here’s the code for the callback function:

    void PrintAPage(Object* pSender, PrintPageEventArgs* pe) { Graphics* gr = pe->Graphics; Pen* pen1 = new Pen(Color::Black); // Draw the image Bitmap* bmp = new Bitmap(S"ramp1.gif"); gr->DrawImage(bmp, 10,10); for(int i=0; i<list->Count; i++) { Line* pl = dynamic_cast<Line*>(list->get_Item(i)); gr->DrawLine(pen1, pl->p1.X,pl->p1.Y, pl->p2.X,pl->p2.Y); } }

    Notice that the code is exactly the same as that in the Paint event handler. In this case, I’m taking a very simple-minded approach to printing and simply dumping the content of the form straight to the printer. In real life, you’d probably want to add headers and footers and perhaps format the output differently.

    Note

    What if your output fits on more than one page? The page handler function is going to be called once for each page, and it’s up to you to keep track of where you are and what needs to be printed. As long as there are more pages to print, set the HasMorePages property of the PrintPageEventArgs object to true before returning from the handler function, and it will get called again.

  5. Build and run the application. Draw some lines and text on the screen, and verify that you can send the output to your printer.

    Note

    You’ll find that the lines appear on the printed output but the text doesn’t. The data for the lines was saved so that it could be redrawn in PrintAPage; the text data wasn’t saved, so it won’t be redrawn.




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net