21. Graphics Methods – Code Example Private Sub graphicsForm_Paint(ByVal sender As Object, _ ByVal e As System.Windows . Forms . PaintEventArgs) _ Handles MyBase.Paint Dim gr As Graphics = e .Graphics ' Create a graphics object. Dim redPen As New Pen (Color . Red) gr . DrawRectangle(redPen, 10, 10, 30, 30) ' Draw a red rectangle. gr . DrawLine(redPen, 50, 0, 50, 300) ' Draw a red line. Dim blueBrush As New SolidBrush (Color . Blue) gr . FillEllipse(blueBrush, 100, 100, 50, 50) ' Draw a blue filled circle. Dim widePen As New Pen (Color . Blue, 15) gr . DrawLine(widePen, 300, 0, 300, 300) ' Draw a fat blue line. End Sub
22.
23.
24.
25.
26.
27.
28.
29. Creating Animation Each of the graphics is placed into the upper picture box when the user clicks the Change button
30.
31.
32.
33.
34.
35.
36. Scroll Bar Properties (2 of 3) Gray Area ( LargeChange property ) Scroll Box ( Value property ) Scroll Arrow ( SmallChange property ) Maximum value ( Maximum property ) Minimum value ( Minimum property )
37.
38.
39.
40.
41.
42.
43. Drag-and-Drop Programming (2 of 2) The Source object is dragged to the Target object in a drag-and-drop operation
44.
45.
46.
47.
Notes de l'éditeur
Brief description on how to navigate within this presentation (ppt) The first time a Key Term from the chapter is used in the ppt it will display in blue Gold colored text boxes display coding examples Slides will be numbered (# of #) when multiple slides on same topic (Slide title) Speaker notes are included where appropriate for slides (*)Denotes either a comment for page reference to textbook or slide reference in ppt
GDI+ is more advanced and an improvement over the previous Graphics Device Interface (GDI) used in previous version of VB Being device dependent, the programmer doesn’t have to be concerned about the physical characteristics of the output device
*The next few slides describe the steps in more detail
A graphics object can also be created by calling the CreateGraphics method of a form or control—it is used when wanting to display a graphic from a procedure other than the Paint event Lines and shapes are drawn on forms and controls by drawing on a Graphics object
*The Pen/Brush properties are described on the next slide
*The next slide displays a graphic shape created by drawing with Pen and Brush objects
*The next slides show examples of code for Pen and Brush classes
There may be times that several different pens are used—for each different color or line width another Pen object can be created or redefine an already dimensioned Pen variable
Use the Color constants to assign a color to Brush objects
*The next slide displays the coordinates for graphic
The coordinates for graphics begin with 0,0 in the upper-left corner of a form or container
An example of a Point in the design of a form can easily be seen-examine the Location property of any control; the Location is assigned a Point object with x and y properties
Examine the design of any form to see an example of a Size structure-each of the controls has a Size property which has width and height properties
The first example defines a rectangular region, specified by its upper left corner and its size The second example using the overloaded constructor declare a new Rectangle by specifying its location in x and y coordinates and its width and height Point, Size, and rectangle structures can be created for single-precision floating-point values—specify the PointF, SizeF, and RectangleF structures
*The next slide displays the Graphics Method-General Forms
Rather than declaring a pen or brush-type Pens.color or Brushes.color directly in the Graphics method; the pen or brush object must be declared if wanting to change the width
The code example draws the outline of rectangle in red using the DrawRectangle method (key term) and draws a line with the DrawLine method ; (keyterm) the FillEllipse method (key term) is used to draw a filled circle
A Random object is popular for use in games, as well as problems in probability and queuing theory
*Refer to textbook p. 509 for an example of output produced by Chapter13RandomNumbers example program—the program draws the figure and generates random snowflakes in the form’s Pain event handler
You can use a control’s SetBounds method to move it to a new location and/or to change its size *The next slide displays examples of the SetBounds Method General Form and usage examples
Generally events occur when the user takes an action-using the Timer component and its Tick event When a timer is added, it goes into the component tray-the tool for the timer is represented by the little stopwatch in the toolbox
*Each of these properties has a default value-see Table 13.2 p.516 in textbook
It’s fun to add sound to an application—computers play sounds as they are turned on and off or receiving email
If it is desired to have a sound play as the application opens, set the SoundLocation property of a SoundPlayer component to the desired file at design time
*The Source and Target Objects are described on the following slides