© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics and Printing Outline 26.1Test-Driving the CheckWriter Application 26.2GDI+ Introduction 26.3Constructing the CheckWriter Application 26.4 PrintPreviewDialog s and PrintDocument s 26.5Creating an Event Handler for the CheckWriter Application 26.6 Graphics Objects: Colors, Lines and Shapes 26.7Printing Each Control of the CheckWriter Application 26.8 Font Class 26.9Previewing and Printing the Check 26.10Wrap-Up 1
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Draw two-dimensional shapes. –Control the colors and patterns of filled shapes. –Use Graphics objects. –Draw shapes on an object. –Create an application to write checks. 2
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Introduction API : interface used by a program to access the operating system and various services on the computer GDI+: graphics API that provides classes for creating and manipulating two-dimensional vector graphics, fonts, and images Vector graphic : not represented by a grid of pixels, but by a set of mathematical properties called vectors, which describe a graphic’s dimensions, attributes, and position 3
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.1Test-Driving the CheckWriter Application 4
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.1Test-Driving the CheckWriter Application Figure 26.1 CheckWriter application displaying an empty check. Company information Written check amount Memo text Check numberDateAmountRecipient 5
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.1Test-Driving the CheckWriter Application Figure 26.2 CheckWriter application displaying a completed check. Where MICR numbers are located on a check 6 Magnetic Ink Character Recognition (MICR) numbers – Bank’s routing number (9 digits), followed by account number and check number. – Special machines read MICR number and route check to appropriate account
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.1Test-Driving the CheckWriter Application Figure 26.3 Preview of the completed check. Page to view Close Print Zoom Displays three pages 7
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.2GDI+ Introduction Pen : used to draw lines in various styles Brush: used to fill a shape (solid color or pattern) Color : contains pre-defined colors and allows you to create custom colors Font : contains font properties like Bold, Italic, and Size –FontFamily : contains methods for obtaining font information Coordinate System: used to identify all points on the screen – x-coordinate: horizontal position – y-coordinate: vertical position 8
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.2GDI+ Introduction Figure 26.4 GDI+ coordinate system. 9
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.3Constructing the CheckWriter Application Figure 26.5 ACE table for the CheckWriter application. 10
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.3Constructing the CheckWriter Application Figure 26.6 CheckWriter application in Design view with PrintPreviewDialog. Added PrintPreviewDialog 11
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.3Constructing the CheckWriter Application Figure 26.7 Running template application. 12
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved PrintPreviewDialog s and PrintDocument s Document property: allows you to specify the document to preview PrintPreviewDialog : contains the UseAntiAlias property, which can smooth the text in the dialog – Set UseAntiAlias to true to enable smooth text 13
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure 26.8 Use a namespace in class FrmCheckWriter. Using namespace System.Drawing.Printing System.Drawing.Printing allows access to all services related to printing 14
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure 26.9 Rearranging and commenting the control declaration. 15
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure m_objFont created. Declaring a font object 16
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure objPrintDocument_PrintPage event handler. Empty PrintPage event handler 17
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application float data type: stores floating-point values; similar to double but less precise and requires less memory Use float to specify the coordinates for the left and top margins of the page to be printed. Margin values are determined using MarginBounds.Left and MarginBounds.Top 18
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure Variables created for objPrintDocument_PrintPage. Declare coordinatesSet left margin of pageSet top margin of page 19
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure foreach statement used for iteration. Declaring a foreach statement 20
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure Event handler objPrintDocument_PrintPage modified to draw border of check. Draw border of check 21
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application DrawRectangle takes five arguments: – Pens. Black object – x-coordinate of left edge of check – y-coordinate of top of check – this.Width ( this refers to the current object) – this.Height 22
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure Event handler objPrintDocument_PrintPage modified to indicate that there are no more pages to print. Indicate no more pages to print 23
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics Objects: Colors, Lines and Shapes Graphics : To draw, invoke a control’s CreateGraphics method – Example: Graphics objGraphics = pnlDisplay.CreateGraphics(); Colors : Created from a combination of alpha, red, green, and blue. Alpha determines opacity and ranges from 0 to 255 – Opacity : amount of transparency Blending of the color with any background color Dithering : Using small dots of existing colors to form a pattern that simulates the desired color. 24
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics Objects: Colors, Lines and Shapes 25 Creating your own color: Color colorSilver; colorSilver = Color.FromArgb( 192, 192, 192 ); This version of the FromArgb method sets alpha to 255
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics Objects: Colors, Lines and Shapes 26
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics Objects: Colors, Lines and Shapes Pen : used to specify characteristics, such as the color and thickness of a line Brush : used to fill interior of a shape with a color or pattern. – Example, SolidBrush objBrush = new SolidBrush( Color.Orange ); 27
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics Objects: Colors, Lines and Shapes 28
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.7Printing Each Control of the CheckWriter Application Figure Code to determine whether the current control is a Button. Ensure current control is not a Button 29
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.7Printing Each Control of the CheckWriter Application Figure switch statement to print controls. Determine current controlSet strLine to control’s textUnderline the text if displaying date from DateTimePicker Draw box around dollar amountSet the default font 30
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.7Printing Each Control of the CheckWriter Application DrawString : draws the specified string of text in the Graphics object – First argument: string to draw – Second argument: Font, specified by m_objFont – Third argument: specifies a Brush – Fourth and fifth arguments: x- and y-coordinates where the first character of the string prints 31
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.7Printing Each Control of the CheckWriter Application Figure Code to set string positions of the controls. Set horizontal location of current control Set vertical location of current control Print current control’s text 32
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Font Class 33 Note: Once a Font object has been created, its properties cannot be modified – You must create a new object to change the Font
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Font Class 34 Size returns the size of the font in design units SizeInPoints returns the size of the font in points Design units include Point, Display, Document, Inch, and Pixel Font class has several constructors Most require a font name such as SansSerif or Serif, a font size, and font style such as bold, italic, or underline
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure Code that creates the PrintDocument object. Declaring a PrintDocument object 35
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure Code that adds an event handler to the PrintDocument object. Adding an event handler for the PrintDocument object 36 Use class PrintPageEventHandler to associate the PrintPage event of the objPrintDocument object with the event handler specified
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure Exiting the event handler if no printers are installed. Take no action if there are no printers installed 37
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure Event handler btnPrint_Click modified to print the document. Printing the check 38
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure Event handler btnPreview_Click modified to create PrintDocument and add PrintPage event handler. Creating a PrintDocument object Adding an event handler for the PrintDocument 39
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure Exiting the print preview event handler if no printers are installed. 40
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure Event handler btnPreview_Click modified to set the PrintPreviewDialog object’s Document property. Setting the document to preview 41
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure Event handler btnPreview_Click modified to show preview dialog. Displaying the preview dialogue 42
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure Displaying an error message when no printers are installed. Method to display error message 43
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 44 CheckWriter.cs Part (1 of 10)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 45 CheckWriter.cs Part (2 of 10)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 46 CheckWriter.cs Part (3 of 10) Instance variable to store font
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 47 CheckWriter.cs Part (4 of 10)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 48 CheckWriter.cs Part (5 of 10) Event handler indicating what to print Variable to store left margin value Variable to store top margin value Looping through each control on the Form
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 49 CheckWriter.cs Part (6 of 10) Underlining text Drawing a box around text Using the control’s font
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 50 CheckWriter.cs Part (7 of 10) Printing textDrawing a box around the check Indicate that there are no more pages
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 51 CheckWriter.cs Part (8 of 10) Create a PrintDocument object Add an event handler for the PrintDocument object Display an error message if no printers are installed Print the Check
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 52 CheckWriter.cs Part (9 of 10) Create a PrintDocument object Add an event handler for the PrintDocument object Display error message if no printers are installed Show preview in dialog
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 53 CheckWriter.cs Part (10 of 10)