|
Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application Authors: Patrick T Published year: 2006 Pages: 143-144/247 |
Selecting a CanvasMost drawing in .NET occurs in the context of a Graphics object. (For those familiar with pre-.NET development in Windows, this is similar to a device context .) Graphics objects provide a canvas on which you draw lines, shapes , bitmap images, and pre-recorded drawing macros. Graphics objects do not contain the graphics surface itself; they are simply generic conduits to the actual canvas. There is always some surface behind the Graphics object, whether it is a portion of the screen, a Bitmap object, or the simulated surface of a printed page. Any drawing that is done to the Graphics object immediately impacts the underlying surface. The Graphics object includes dozens of methods that let you draw shapes and images on the graphics surface, and perform other magical 2-D activities. We'll cover many of them in this chapter. Obtaining and Creating Graphics ObjectsGetting a Graphics object for an on-screen form or control is as easy as calling the form or control's CreateGraphics method.
Dim wholeFormGraphics As Graphics = _ Form1.CreateGraphics() Dim buttonOnlyGraphics As Graphics = _ Button1.CreateGraphics() Some events, most notably the Paint event for forms and controls, provide access to a Graphics object through the event arguments.
Private Sub PictureBox1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
Dim holdGraphics As Graphics =
e.Graphics
End Sub
You can also create a Graphics object that is unrelated to any existing display area by associating it to a bitmap.
Dim workBitmap As New Bitmap(50, 50) Dim workGraphics = Graphics.FromImage(workBitmap) Remember, all changes made to the workGraphics instance will impact the workBitmap image. Disposing of Graphics Objects ProperlyWhen you are finished with a Graphics object that you create , you must dispose of it by calling its Dispose method. (This rule is true for many different GDI+ objects.) Don't keep it around for a rainy day because it won't be valid later. You must, must, must dispose of it when you are finished with it. If you don't, it could result in image corruption, memory usage issues, or worse yet, international armed conflict. So, please dispose of all Graphics objects properly.
workGraphics.Dispose() If you create a Graphics object within an event, you really need to dispose of it before exiting that event handler. There is no guarantee that the Graphics object will still be valid in a later event. Besides, it's easy to re-create another Graphics object at any time. If you use a Graphics object that is passed to you from another part of the program (like that e.Graphics reference in the preceding Paint event handler), you should not dispose of it. Each creator is responsible for disposing of its own objects. |
Choosing Pens and BrushesA lot of graphics work involves drawing primitives: using lines, ellipses, rectangles, and other regular and irregular shapes to build up a final display. As in real life, you draw these primitives using a Pen object. For those primitives that result in a fillable or semi-fillable shape, a Brush object specifies the color or pattern to use in that filled area. GDI+ includes many predefined pens and brushes, or you can create your own. PensPens are line-drawing tools used with the drawing commands of a Graphics object. A basic pen has a solid color and a thickness .
' ----- A red pen five units wide. Dim redPen As New Pen(Color.Red, 5) As with Graphics objects, any Pen you create using the New keyword must be disposed of properly when you are finished with it.
redPen.Dispose() There are several predefined pens made available through the System.Drawing.Pens class, all named by their color, as in Pens.Red . If you use one of these pens, you don't have to dispose of it. You can create a lot of interesting pens that vary by line style, end decoration, and color variations. The following code generates the image displayed in Figure 17-2.
Private Sub PictureBox1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
' ----- Draw some fancy lines.
Dim usePen As Pen
' ----- Blank out the background.
e.Graphics.Clear(Color.White)
' ----- Draw a basic 1-pixel line using the title
' bar color.
usePen = New Pen(SystemColors.ActiveCaption, 1)
e.Graphics.DrawLine(usePen, 10, 10, 200, 10)
usePen.Dispose()
' ----- Draw a thicker dashed line with arrow and ball
' end caps. Each dashed segment has a triangle end.
usePen = New Pen(Color.FromName("Red"), 5)
usePen.DashCap = Drawing2D.DashCap.Triangle
usePen.StartCap = Drawing2D.LineCap.ArrowAnchor
usePen.EndCap = Drawing2D.LineCap.RoundAnchor
usePen.DashStyle = Drawing2D.DashStyle.Dash
e.Graphics.DrawLine(usePen, 10, 30, 200, 30)
usePen.Dispose()
' ----- A semi-transparent black pen with three line
' parts, two thin and one thick.
usePen = New Pen(Color.FromArgb(128, 0, 0, 0), 10)
usePen.CompoundArray = _
New Single() {0.0, 0.1, 0.4, 0.5, 0.8, 1.0}
e.Graphics.DrawLine(usePen, 10, 55, 200, 55)
usePen.Dispose()
End Sub
Figure 17-2. Yes sir, yes sir, three lines full
The code shows that there are a few different ways to specify a color, either by its predefined name ( Color.White and SystemColors.ActiveCaption ), a string name (using Color.FromName ), or its Alpha-Red-Green-Blue value ( Color.FromArgb ). That last version lets you supply distinct values for the "alpha blend" (which sets the transparency level, from 0 for fully transparent, to 255 for fully opaque ), red, green, and blue components of the full color. Most of the pen-specific properties I demonstrated here are somewhat self-explanatory. As with most of GDI+, the mind-numbing amount of available features makes it impossible to completely document in a small chapter, let alone provide a good night's sleep for authors designing such chapters. I will simply refer you to the online documentation for the Pen class to get all of the luscious details. BrushesBrushes are used for filling in spaces between drawn lines, even if you make those lines fully invisible. GDI+ includes a variety of brush types, including solid brushes (your basic single-color brush), hatch brushes (pattern brushes that are pleasant but general), texture brushes (where a custom bitmap is used for the brush), and gradient brushes (which slowly fade from one color to another across the brush). The System.Drawing.Brushes class includes some predefined solid brushes based on color name. As with pens, you must dispose of brushes that you create, but not the solid system-defined brushes. The following block of code draws some simple rectangles with a variety of brush styles. The results appear in Figure 17-3.
Private Sub PictureBox1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
' ----- Draw some fancy rectangles.
Dim useBrush As Brush
e.Graphics.Clear(Color.White)
' ---- Draw a filled rectangle with a solid color.
e.Graphics.FillRectangle(Brushes.Cyan, 10, 10, 150, 50)
' ----- Draw a hatched rectangle. Use black for the
' background, and white for the pattern foreground.
useBrush = New Drawing2D.HatchBrush( _
Drawing2D.HatchStyle.LargeConfetti, _
Color.White, Color.Black)
e.Graphics.FillRectangle(useBrush, 10, 70, 150, 50)
useBrush.Dispose()
' ----- Draw a left-to-right linear gradient rectangle.
' The gradient's own rectangle determines the
' starting offset, based on the Graphics surface
' origin.
useBrush = New Drawing2D.LinearGradientBrush( _
New Rectangle(200, 10, 75, 25), Color.Blue, _
Color.Yellow, Drawing2D.LinearGradientMode.Horizontal)
e.Graphics.FillRectangle(useBrush, 200, 10, 150, 50)
useBrush.Dispose()
' ----- Use an image for the brush. I'm using the
' "LookupItem.bmp" graphic used in the Library
' Project.
useBrush = New TextureBrush(Image.FromFile( _
"LookupItem.bmp"))
e.Graphics.FillRectangle(useBrush, 200, 70, 150, 50)
useBrush.Dispose()
End Sub
Figure 17-3. Kind of square if you ask me
|
|
Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application Authors: Patrick T Published year: 2006 Pages: 143-144/247 |