Download presentation
Presentation is loading. Please wait.
Published byHugh Fields Modified over 9 years ago
1
© Copyright 1992-2004 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
2
© Copyright 1992-2004 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
3
© Copyright 1992-2004 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
4
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.1Test-Driving the CheckWriter Application 4
5
© Copyright 1992-2004 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
6
© Copyright 1992-2004 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
7
© Copyright 1992-2004 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
8
© Copyright 1992-2004 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
9
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.2GDI+ Introduction Figure 26.4 GDI+ coordinate system. 9
10
© Copyright 1992-2004 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
11
© Copyright 1992-2004 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
12
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.3Constructing the CheckWriter Application Figure 26.7 Running template application. 12
13
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.4 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
14
© Copyright 1992-2004 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
15
© Copyright 1992-2004 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
16
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure 26.10 m_objFont created. Declaring a font object 16
17
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure 26.11 objPrintDocument_PrintPage event handler. Empty PrintPage event handler 17
18
© Copyright 1992-2004 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
19
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure 26.12 Variables created for objPrintDocument_PrintPage. Declare coordinatesSet left margin of pageSet top margin of page 19
20
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure 26.13 foreach statement used for iteration. Declaring a foreach statement 20
21
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure 26.14 Event handler objPrintDocument_PrintPage modified to draw border of check. Draw border of check 21
22
© Copyright 1992-2004 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
23
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.5Creating an Event Handler for the CheckWriter Application Figure 26.15 Event handler objPrintDocument_PrintPage modified to indicate that there are no more pages to print. Indicate no more pages to print 23
24
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.6 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
25
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.6 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
26
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.6 Graphics Objects: Colors, Lines and Shapes 26
27
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.6 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
28
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.6 Graphics Objects: Colors, Lines and Shapes 28
29
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.7Printing Each Control of the CheckWriter Application Figure 26.18 Code to determine whether the current control is a Button. Ensure current control is not a Button 29
30
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.7Printing Each Control of the CheckWriter Application Figure 26.19 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
31
© Copyright 1992-2004 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
32
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.7Printing Each Control of the CheckWriter Application Figure 26.20 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
33
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.8 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
34
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.8 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
35
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure 26.22 Code that creates the PrintDocument object. Declaring a PrintDocument object 35
36
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure 26.23 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
37
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure 26.24 Exiting the event handler if no printers are installed. Take no action if there are no printers installed 37
38
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure 26.25 Event handler btnPrint_Click modified to print the document. Printing the check 38
39
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure 26.26 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
40
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure 26.27 Exiting the print preview event handler if no printers are installed. 40
41
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure 26.28 Event handler btnPreview_Click modified to set the PrintPreviewDialog object’s Document property. Setting the document to preview 41
42
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure 26.29 Event handler btnPreview_Click modified to show preview dialog. Displaying the preview dialogue 42
43
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26.9Previewing and Printing the Check Figure 26.30 Displaying an error message when no printers are installed. Method to display error message 43
44
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 44 CheckWriter.cs Part (1 of 10)
45
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 45 CheckWriter.cs Part (2 of 10)
46
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 46 CheckWriter.cs Part (3 of 10) Instance variable to store font
47
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 47 CheckWriter.cs Part (4 of 10)
48
© Copyright 1992-2004 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
49
© Copyright 1992-2004 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
50
© Copyright 1992-2004 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
51
© Copyright 1992-2004 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
52
© Copyright 1992-2004 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
53
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 53 CheckWriter.cs Part (10 of 10)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.