540 likes | 550 Views
Printing Petzold, Chapter 21 . Jim Fawcett CSE778 – Advanced Windows Programming Summer 2003 Derived from a presentation by Ryan Nugent and Ed Friers. Windows Form Printing. A printer is described by an object of type PrinterSettings.
E N D
PrintingPetzold, Chapter 21 Jim Fawcett CSE778 – Advanced Windows Programming Summer 2003 Derived from a presentation by Ryan Nugent and Ed Friers
Windows Form Printing • A printer is described by an object of type PrinterSettings. • Defined in the System.Drawing.Printing namespace.PrinterSettings prnset = new PrinterSettings();
Printer Setting Properties • PrinterName usually indicates the make and model of the printer, but the user can change the name during installation. • PrinterName is writable, but if it is changed, all the other properties are affected. • If PrinterName is not the name of a printer, IsValid is set to false. (no exception thrown)
Printer Setting Properties (con’t) • IsValid and IsDefaultPrinter are true when you create a new PrinterSettings object as long as there is an installed printer. • If no printers installed, PrinterName is <no default printer> and IsValid is false.
Static Property of Printer Settings • This gives you a list of all the installed printers.using PrinterSettings;StringCollection sc =InstalledPrinters();
Accessing the List of Printers • You can use the count to get the number of printers: • sc.Count • You can access them using []: • sc[0] or sc[1] …… sc[count-1] • You can access them directly: • PrinterSettings.InstalledPrinters[1]
Using the List of Printers • Code that puts all the installed printers in a combo box; • foreach(string str in PrinterSettings.InstalledPrinters) combo.Items.Add(str); • You can assign the PrinterName directly • prnset.PrinterName=sc[2]; • IsValid is true, IsDefaultPrinter is false even if you set the PrinterName to the default printer.
Printer Settings Properties • If IsPlotter = True then the printer should not be relied on to print bitmaps. • LandscapeAngle is either 90 or 270 degrees. If it is not an available feature, it is set to 0.
Printer Setting Properties • These can be accessed with either Count or an indexer. • Each property will be discussed in the next few slides.
Paper Source Properties • This is a PrinterSettings Property. It is a collection of all the paper sources on the printer, not the default. • An example of a SourceName is: • “Manual Paper Feed”
Paper Size Properties • Width and Height indicate the size of the paper in hundredths of an inch. • PaperSize is a property of PrinterSettings.
Paper Kind Enumeration • Some of the more useful members (117 members total).
Printer Resolution Properties • This is a PrinterSettings property. • X and Y indicate the actual dots per inch.
PrinterResolutionKind Enumeration • Every printer has at least one of each of these. Each of the resolutions the printer can provide gives you a Custom value. The others have a X and Y value of –1.
Printer Setting Properties • If CanDuplex is True, you can print on both sides of the page. Only can use enum values if True. • Simplex is one-sided printing. • Vertical and Horizontal show how the pages are connected.
Printer Setting Properties • Copies is 1 by default, can be set up to max copies. • If collate is True, the pages are printed 1,2,3,1,2,3. Default depends on the printer.
Printer Settings for Print Dialog • These are normally used with the PrintDialog class that will be discussed later.
Page Settings • This class describes the characteristics of a printed page. • This property indicates the default page settings. • Will be discussed more later.
Page Settings • PageSettings class describes those printer characteristics that can change with each page. • Each object is associated with a particular printer. • You can create a PageSettings object or use a pre-created one (from PrinterSettings). • PageSettings() • PageSettings(PrinterSettings prnset)
Intro to Page Settings Properties • Indicates printer these settings are associated with. • More on the next slide.
Page Settings Properties • A Windows Form program can change default settings when printing a document, but these changes DO NOT affect other applications. • prnset == prnset.DefaultPageSettings.PrinterSettingsis True because the object is only a reference, changes affect both. • Pageset==pageset.PrinterSettings.DefaultPageSettings is False, only initially true.
Page Settings Properties • Landscape toggles between Landscape and Portrait. • Color tells if color will be used on the page. • Bounds defines a rectangle object that indicates the size of the page units in hundredths of an inch. • According to paper size and landscape settings.
Page Settings Properties - Margins • This indicates the default margins for a page, initially set to 1 inch on all sides. • Constructors: • Margins() or Margins (int Right, Left, Top, Bottom) • Properties: • Right, Left, Top, Bottom
Page Settings Properties • Similar to PrinterSettings class properties • The PaperSource property of PageSettings is one of the items from PaperSources collection in PrinterSettings.
Notes on the Previous Slide • If you change one of these three properties in your program, be sure to set the property from a member of the corresponding collection. • PaperSize property is not affected by the Landscape Property.
Defining a Document • A print job is represented by the PrintDocument Class. • It only has a default constructor. • A program begins the printing process by creating an object of type PrintDocument. • If this is a field, you only need to create it once, and it retains all settings.
Print Document Properties • When you create a new PrintDocument object, it uses the default printer. • The DefaultPageSettings property is set from the DefaultPageSetting property of PrinterSettings.
Notes on the Properties • DocumentName is set to text string “document”…it should be changed. • See PrintController, Slide 40. • prndoc.PrinterSettings ==prndoc.DefaultPageSettings.PrinterSettings • Is True. • prndoc.DefaultPageSettings==prndoc.PrinterSettings.DefaultPageSettings • Is False so you can change default settings for the document, not for the printer.
Print Document Events • Begin and end are triggered once for each print job. • QueryPageSetting and PrintPage are triggered for every page.
Notes on the Events • PrintPage event handlers indicate if there are more pages to be printed. • If you want to have different settings for each page, install a handler for QueryPageSettings. • For initialization or cleanup, install handlers for BeginPrint or EndPrint.
Print • You initiate printing by calling this method: • Void Print() • The print method doesn’t return until the program is finished printing the document. • The application can’t respond to any input during this time.
Setting Print Page Event Args • BeginPrint event handler can set Cancel to true to abort the print job. • QueryPageSetting can change PageSettings properties in preparation for the corresponding PrintPage event.
Print Page Event Args • The graphics object is created new for each page. If you set PageUnit or PageScale, it will not stay.
Notes on Print Page Event Args • HasMorePages is always false; must be set to true to continue. • Cancel is usually set to false. • PageSetting will reflect any changes made in QueryPageSettings.
Printer Selection Dialog Program • The simple dialog box shown above is used in the PrintThreePage and PrintWithMargin programs.
Print Three Pages Program • Most of the work is going on in the MenuFilePrintOnClick method. • PrintDocument and PrintSelectionDialog objects are created. • Selected printer, resolution and other properties are set. • OnQueryPageSetting alternates between Landscape and Portrait. • Code on 1015-1017
Page Dimensions • In the Windows Form interface, you can never tell exactly what the margins are. • This is because you can’t tell what the printer can physically print on. The margins aren’t the same between left and right or top and bottom. • You can approximate the margins as in the PrintwithMargins program on pages 1020 and 1021. • The OnPrintPage method makes an approximate rectangle.
PrinterUnitConvert Static Convert Method • This method has 6 versions, each with different first argument types: int, double, Point, Size, Rectangle, Margins; the general form is: Type Convert(type tValue, PrinterUnit puFrom, PrinterUnit puTo)
The Print Controller • PrintController class is abstract. • Derived classes are: • StandardPrintController. • PrintControllerWithStatusDialog (default). • PreviewPrintController.
PrintController Class Methods • Four methods of PrintController are: OnStartPrint, OnStartPage, OnEndPage and OnEndPrint. • These methods are called by the PrintDocument between BeginPrint.. EndPrint. • PreviewPrintController uses OnStartPage to get Graphics object to display in print preview. Otherwise, the return object goes to PrintPage event handler.
An Example • The program on pp. 1024-1026 shows how to display current printing document and page with a status bar instead of a dialog box. • This task is easily accomplished using StandardPrintController and overriding the four PrintController methods.
Using the Standard Print Dialog Box • PrintDialog makes a dialog box that lets user select and change settings for a printer and specify what portion of the document to print. • When creating a new PrintDialog object using default constructor, you must initialize one (but not both) of: • Document is preferred as it also sets PrinterSettings • to the same property of the PrintDocument.
More PrintDialog Properties • These properties are options the dialog gives the user.
Simple Print Dialog • This program only allows you to bring up a print dialog box and print the numbers 1-3 thru the dialog box. • The only option that is allowed is the AllowSomePages. • If you move the first three lines of MenuFilePrintOnClick, you keep the settings every time. • Code on 1029-1031.
Setting Up the Page • PageSetupDialog is a dialog box for specifying margins, page orientation, page sources and paper sizes as well as selecting default printer and options. • When creating a new PageSetupDialog with default constructor, set one (only one) of: • Document property is preferred as this will set others.
More PageSetupDialog Properties • All bools are default true except ShowHelp. MinMargins default is all zeros.
ImagePrint Program • This program lets the user load, save, and print bitmaps using a menu and the 2 dialog boxes. • ImagePrint class is derived from class ImageIO from the program in Chapter 16. • OnPrintPage method calculates a display rectangle and makes the image as large as possible within the rectangle.
Print Preview • To perform a preview, the PrintPage event handler is used to display output to the surfaces of bitmaps. • When the print method returns, the Bitmaps are then presented to the user.