A 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 semifillable 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.
Pens 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)
Graphics objects, any
Pen you create using the
New keyword must be disposed of properly when you are finished with it.
Several predefined pens are 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 styles, end decorations, and color variations. The following code generates the image displayed in Figure 18-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, ...