Home

Introduction to Brushes

 

Fundamentals of Brushes

 

Introduction

In the previous lesson, we drew two types of figures: line-based and closed shapes. These figures required a pen to show their shape. The particularity with closed shapes is that they can be filled, with a color, a picture, or a pattern.

A brush is an object that holds a color, a picture, or a drawing pattern and that is used to fill the interior of a closed shape. This definition also means that there are various types of brushes with different goals. To meet these goals, the .NET Framework provides support for brushes in various namespaces with different classes. The parent of all brushes is the Brush class defined in the System.Drawing namespace.

 

Using a Brush

Because the main job of a brush is to fill a closed shape, the Graphics class provides a method that corresponds to each of the closed shapes we reviewed to draw in the previous lesson in order to fill it. The methods are:

  • FillRectangle: Used to fill the interior of a rectangle or a square
  • FillRectangle: Used to fill the interior of a series of rectangles
  • FillEllipse: Used to fill the interior of an ellipse or a circle
  • FillPolygon: Used to fill the interior of a polygon
  • FillClosedCurve: Used to fill the interior of a closed curve
  • FillPie: Used to fill the interior of a pie
  • FillPath: Used to fill the interior of a graphic path

To fill out a shape, call one of these methods, pass it a brush value, followed by the location and dimensions of the shape. For example, if you want to draw a rectangle and fill it with a brush, you would use code similar to:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            e.Graphics.FillRectangle(SomeBrush, 20, 20, 200, 160)

        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

Over all, there are four types of brushes.

Solid Brushes

 

Simple Colored Brushes

Like a pen, the primary characteristic of a brush is its color. To help you create a simple brush, the System.Drawing namespace provides the static sealed Brushes class. The only feature this class provides is the ability to specify a color to use on a brush. As a static class, you never have to instantiate it.

To create a simple brush whose only information is provided by its color, call the Brushes class and access a color by qualifying it with the name of the class. Each color is provided by its name as a property. Here is an example of using the class:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim pt As Point() = { _
 		New Point(10, 22), _
 		New Point(188, 246), _
                          	New Point(280, 192), _
 		New Point(250, 48)}

            e.Graphics.FillClosedCurve(Brushes.BlanchedAlmond, pt)
            e.Graphics.DrawClosedCurve(Pens.Blue, pt)

        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

This would produce:

Brush

 

Practical LearningPractical Learning: Drawing a Circle

  1. Start a new Windows Application named RotatingCircles1
  2. Double-click middle of the form and change the file as follows:
     
    Public Class Form1
    
        Private graphDrawingArea As Graphics
        Private bmpDrawingArea As Bitmap
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
            bmpDrawingArea = New Bitmap(Width, Height)
            graphDrawingArea = Graphics.FromImage(bmpDrawingArea)
        End Sub
    End Class
  3. From the Components section of the Toolbox, click Timer and click the form
  4. In the Properties window, change the following values:
    Enabled: True
    Interval: 20
  5. Under the form, double-click the timer1 object and make the following changes:
     
    Public Class Form1
    
        Private GraphDrawingArea As Graphics
        Private BmpDrawingArea As Bitmap
        Private MainRadius As Integer = 10
        Private SmallRadius As Integer = 5
        Private IsMax As Integer
        Private SmallRadiusMax As Integer
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
            BmpDrawingArea = New Bitmap(Width, Height)
            GraphDrawingArea = Graphics.FromImage(BmpDrawingArea)
    
            MainRadius = 10
            SmallRadius = 5
        End Sub
    
        Private Sub DrawCentralCircle(ByVal CenterX As Integer, _
                                      ByVal CenterY As Integer, _
                                      ByVal Radius As Integer)
            Dim Starting As Integer = CenterX - Radius
            Dim Ending As Integer = CenterY - Radius
            Dim Diameter As Integer = Radius * 2
    
            GraphDrawingArea.DrawEllipse(New Pen(Color.Blue), _
                              Starting, Ending, Diameter, Diameter)
        End Sub
    
        Private Sub DrawCornerCircle(ByVal CenterX As Integer, _
                                     ByVal CenterY As Integer, _
                                     ByVal Radius As Integer)
            Dim Starting As Integer = CenterX - Radius
            Dim Ending As Integer = CenterY - Radius
            Dim Diameter As Integer = Radius * 2
    
            GraphDrawingArea.DrawEllipse(New Pen(Color.Red), _
                              Starting, Ending, Diameter, Diameter)
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) _
                                Handles Timer1.Tick
            Dim graph As Graphics = Graphics.FromHwnd(Handle)
    
            Dim CenterX As Integer = ClientRectangle.Width / 2
            Dim CenterY As Integer = ClientRectangle.Height / 2
    
            If IsMax = True Then
                MainRadius = MainRadius - 1
            Else
                MainRadius = MainRadius + 1
            End If
    
            If MainRadius > (ClientRectangle.Height / 2) Then
                IsMax = True
            End If
    
            If MainRadius < 10 Then IsMax = False
    
            If SmallRadiusMax = True Then
                SmallRadius = SmallRadius - 1
            Else
                SmallRadius = SmallRadius + 1
            End If
    
            If SmallRadius > 240 Then SmallRadiusMax = True
            If SmallRadius < 5 Then SmallRadiusMax = False
    
            GraphDrawingArea.FillRectangle(Brushes.Black, _
                                           0, 0, Width, Height)
    
            ' Central
            DrawCentralCircle(CenterX, CenterY, MainRadius)
            ' Top-Left
            DrawCornerCircle(CenterX / 2, CenterY / 2, SmallRadius)
            ' Top-Right
            DrawCornerCircle(CenterX + (CenterX / 2), CenterY / 2, SmallRadius)
            ' Bottom-Left
            DrawCornerCircle(CenterX / 2, CenterY + (CenterY / 2), SmallRadius)
            ' BottomRight
            DrawCornerCircle(CenterX + (CenterX / 2), _
                         CenterY + (CenterY / 2), SmallRadius)
    
            graph.DrawImage(BmpDrawingArea, 0, 0)
        End Sub
    End Class
  6. Execute the application to see the result
     
    Rotating Circles
  7. Close the form

Creating a Solid Brush

The simplest type of brush is referred to as solid. This type of brush is simply equipped with a color and it is used to fill a shape with it. To get a solid brush, you use the SolidBrush class defined in the System.Drawing namespace. It has only one constructor declared with the following syntax:

Public Sub New(color As Color)

The color passed as argument must be a valid definition of a Color. Here is an example:

Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim brushBlue As SolidBrush = New SolidBrush(Color.Blue)

            e.Graphics.FillRectangle(brushBlue, 20, 20, 200, 160)

End Sub

This would produce:

Brush

If you plan to use different colors to fill different shapes, you don't have to create a new brush for each shape. At any time, before re-using the same brush previously defined, you can simply change its Color. For this reason, the SolidBrush class is equipped with the Color property. Here is an example of using it:

Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim colorizer As SolidBrush = New SolidBrush(Color.Lime)
            e.Graphics.FillRectangle(colorizer, 10, 10, 120, 120)

            colorizer.Color = Color.Salmon
            e.Graphics.FillRectangle(colorizer, 140, 10, 120, 120)

            colorizer.Color = Color.Aqua
            e.Graphics.FillRectangle(colorizer, 10, 140, 120, 120)

            colorizer.Color = Color.Navy
            e.Graphics.FillRectangle(colorizer, 140, 140, 120, 120)

End Sub

This would produce:

Solid Brushes

Like most objects used in graphics programming, a brush consumes the computer resources. Therefore, after using it, you can free the resources it was using by calling the Dispose() method. Here is an example:

Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim colorizer As SolidBrush = New SolidBrush(Color.Lime)
            e.Graphics.FillRectangle(colorizer, 10, 10, 120, 120)

            colorizer.Color = Color.Salmon
            e.Graphics.FillRectangle(colorizer, 140, 10, 120, 120)

            colorizer.Color = Color.Aqua
            e.Graphics.FillRectangle(colorizer, 10, 140, 120, 120)

            colorizer.Color = Color.Navy
            e.Graphics.FillRectangle(colorizer, 140, 140, 120, 120)

            colorizer.Dispose()
End Sub

Practical LearningPractical Learning: Using a Solid Brush

  1. Start a new Windows Forms Application named WeeklySales2
  2. Design the form as follows:
     
    Company Weekly Sales
    Control Name Text Other Properties
    Label Label   Monday  
    Label Label   Tuesday  
    Label Label   Wednesday  
    Label Label   Thursday  
    Label Label   Friday  
    TextBox TextBox txtMonday 12000 TextAlign: Right
    TextBox TextBox txtTuesday 11000 TextAlign: Right
    TextBox TextBox txtWednesday 8500 TextAlign: Right
    TextBox TextBox txtThursday 16800 TextAlign: Right
    TextBox TextBox txtFriday 17500 TextAlign: Right
    Button Button Generate btnGenerate  
  3. Double-click an unoccupied area of the form and change the file as follows:
     
    Public Class Form1
    
        Private GraphDrawingArea As Graphics
        Private BmpDrawingArea As Bitmap
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
            BmpDrawingArea = New Bitmap(Width, Height)
            GraphDrawingArea = Graphics.FromImage(bmpDrawingArea)
        End Sub
    End Class
  4. In the Method Name combo box, select Paint and implement its event as follows:
     
    Private Sub Form1_Paint(ByVal sender As Object, _
                            ByVal e As System.Windows.Forms.PaintEventArgs) _
                            Handles Me.Paint
            e.Graphics.DrawImage(BmpDrawingArea, 0, 0)
    End Sub
  5. In the Class Name combo box, select btnGenerate
  6. In the Method Name combo box, select Click and implement its Click event as follows:
     
    Private Sub btnGenerate_Click(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) _
                                      Handles btnGenerate.Click
            Dim Monday As Integer
            Dim Tuesday As Integer
            Dim Wednesday As Integer
            Dim Thursday As Integer
            Dim Friday As Integer
    
            Try
                Monday = CInt(txtMonday.Text) / 100
            Catch
                MsgBox("Invalid value")
            End Try
    
            Try
                Tuesday = CInt(txtTuesday.Text) / 100
            Catch
                MsgBox("Invalid value")
            End Try
    
            Try
                Wednesday = CInt(txtWednesday.Text) / 100
            Catch
                MsgBox("Invalid value")
            End Try
    
            Try
                Thursday = CInt(txtThursday.Text) / 100
            Catch
                MsgBox("Invalid value")
            End Try
    
            Try
                Friday = CInt(txtFriday.Text) / 100
            Catch
                MsgBox("Invalid value")
            End Try
    
            GraphDrawingArea.Clear(BackColor)
    
            GraphDrawingArea.FillRectangle(New SolidBrush(Color.Red), _
                    txtMonday.Left + 5, _
                    280 - monday, 40, monday)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), _
                    txtMonday.Left + 5, _
                        280 - monday, 40, monday)
            GraphDrawingArea.FillRectangle(New SolidBrush(Color.Blue), _
                    txtTuesday.Left + 5, _
                    280 - tuesday, 40, tuesday)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), _
                    txtTuesday.Left + 5, _
                        280 - tuesday, 40, tuesday)
            GraphDrawingArea.FillRectangle(New SolidBrush(Color.Fuchsia), _
                    txtWednesday.Left + 5, _
                    280 - wednesday, 40, wednesday)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), _
                    txtWednesday.Left + 5, _
                    280 - wednesday, 40, wednesday)
            GraphDrawingArea.FillRectangle(New SolidBrush(Color.Brown), _
                    txtThursday.Left + 5, _
                    280 - thursday, 40, thursday)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), _
                    txtThursday.Left + 5, _
                    280 - thursday, 40, thursday)
            GraphDrawingArea.FillRectangle(New SolidBrush(Color.Turquoise), _
                    txtFriday.Left + 5, _
                    280 - friday, 40, friday)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), _
                    txtFriday.Left + 5, _
                        280 - friday, 40, friday)
    
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), _
                        10, 280, Width - 30, 1)
            Invalidate()
    End Sub
  7. Execute the application and test the form
     
     Weekly Sales
  8. After using it, close the form

Hatch Brushes

 

Introduction

A hatch brush relies on a drawn or designed pattern to set its filling type. To support hatch brushes, the .NET Framework provides the patterns you can use as part of the brush. These pre-designed patterns are referred to as hatch styles. This means that when you use a hatch brush, you must specify the type of pattern you want to use, through one of the available hatch styles. To make the filled area more interesting, you also specify the color to use when drawing the pattern.

To get a hatch brush, you use the HatchBrush class. One of its constructors has the following syntaxes:

Public Sub New(hatchstyle As HatchStyle, foreColor As Color)

The Style of a Hatch Brush

The foreColor argument is the color that will be used to draw the pattern. The style argument is the hatch style you want to apply. Some of the available styles are:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim brushBackDiag As HatchBrush = _
    		New HatchBrush(HatchStyle.BackwardDiagonal, _
             		Color.FromArgb(0, 0, 255))
            Dim brushCross As HatchBrush = _
        		New HatchBrush(HatchStyle.Cross, _
                 	Color.FromArgb(200, 0, 0))
            Dim brushDarkDown As HatchBrush = _
        		New HatchBrush(HatchStyle.DarkDownwardDiagonal, _
                 	Color.Salmon)
            Dim brushDarkHorz As HatchBrush = _
        		New HatchBrush(HatchStyle.DarkHorizontal, _
                 	Color.Navy)
            Dim brushDarkUpDiag As HatchBrush = _
       		New HatchBrush(HatchStyle.DarkUpwardDiagonal, _
                 	Color.Pink)
            Dim brushVertical As HatchBrush = _
        		New HatchBrush(HatchStyle.DarkVertical, _
                 	Color.FromArgb(255, 0, 255))
            Dim brushDashDnDiag As HatchBrush = _
        		New HatchBrush(HatchStyle.DashedDownwardDiagonal, _
                 	Color.FromArgb(255, 128, 0))
            Dim brushDashHorz As HatchBrush = _
        		New HatchBrush(HatchStyle.DashedHorizontal, _
                 	Color.FromArgb(0, 128, 192))
            Dim brushDashUpDiag As HatchBrush = _
        		New HatchBrush(HatchStyle.DashedUpwardDiagonal, _
                 	Color.Green)
            Dim brushDashVert As HatchBrush = _
        		New HatchBrush(HatchStyle.DashedVertical, _
                 	Color.Firebrick)
            Dim brushDiagBrisk As HatchBrush = _
        		New HatchBrush(HatchStyle.DiagonalBrick, _
                 	Color.Fuchsia)
            Dim brushDiagCross As HatchBrush = _
        		New HatchBrush(HatchStyle.DiagonalCross, _
                 	Color.Moccasin)
            Dim brushDivot As HatchBrush = _
        		New HatchBrush(HatchStyle.Divot, _
                 	Color.Goldenrod)
            Dim brushDotDiamond As HatchBrush = _
        		New HatchBrush(HatchStyle.DottedDiamond, _
                 	Color.Gainsboro)
            Dim brushDottedGrid As HatchBrush = _
        		New HatchBrush(HatchStyle.DottedGrid, _
                 	Color.Khaki)
            Dim brushForDiag As HatchBrush = _
        		New HatchBrush(HatchStyle.ForwardDiagonal, _
                 	Color.Maroon)
            Dim brushHorz As HatchBrush = _
        		New HatchBrush(HatchStyle.Horizontal, _
                 	Color.Red)
            Dim brushHorzBrick As HatchBrush = _
        		New HatchBrush(HatchStyle.HorizontalBrick, _
                 	Color.SaddleBrown)
            Dim brushLgChkBoard As HatchBrush = _
        		New HatchBrush(HatchStyle.LargeCheckerBoard, _
                 	Color.RoyalBlue)
            Dim brushLgConfetti As HatchBrush = _
        		New HatchBrush(HatchStyle.LargeConfetti, _
                 	Color.MistyRose)
            Dim brushLgGrid As HatchBrush = _
        		New HatchBrush(HatchStyle.LargeGrid, _
                 	Color.Purple)
            Dim brushLtDnDiag As HatchBrush = _
        		New HatchBrush(HatchStyle.LightDownwardDiagonal, _
                 	Color.DarkCyan)
            Dim brushLtHorz As HatchBrush = _
        		New HatchBrush(HatchStyle.LightHorizontal, _
                 	Color.PowderBlue)
            Dim brushUpDiag As HatchBrush = _
        		New HatchBrush(HatchStyle.LightUpwardDiagonal, _
                 	Color.SeaGreen)
            Dim brushLtVert As HatchBrush = _
        		New HatchBrush(HatchStyle.LightVertical, _
                 	Color.Olive)

            e.Graphics.FillRectangle(brushBackDiag, _
                           20, 20, 80, 60)
            e.Graphics.FillRectangle(brushCross, _
                           120, 20, 80, 60)
            e.Graphics.FillRectangle(brushDarkDown, _
                           220, 20, 80, 60)
            e.Graphics.FillRectangle(brushDarkHorz, _
                           320, 20, 80, 60)
            e.Graphics.FillRectangle(brushDarkUpDiag, _
                           420, 20, 80, 60)

            e.Graphics.FillRectangle(brushVertical, _
                               20, 100, 80, 60)
            e.Graphics.FillRectangle(brushDashDnDiag, _
                           120, 100, 80, 60)
            e.Graphics.FillRectangle(brushDashHorz, _
                           220, 100, 80, 60)
            e.Graphics.FillRectangle(brushDashUpDiag, _
                           320, 100, 80, 60)
            e.Graphics.FillRectangle(brushDashVert, _
                           420, 100, 80, 60)

            e.Graphics.FillRectangle(brushDashVert, _
                           20, 180, 80, 60)
            e.Graphics.FillRectangle(brushDiagBrisk, _
                           120, 180, 80, 60)
            e.Graphics.FillRectangle(brushDiagCross, _
                           220, 180, 80, 60)
            e.Graphics.FillRectangle(brushDivot, _
                           320, 180, 80, 60)
            e.Graphics.FillRectangle(brushDotDiamond, _
                           420, 180, 80, 60)

            e.Graphics.FillRectangle(brushDottedGrid, _
                           20, 260, 80, 60)
            e.Graphics.FillRectangle(brushForDiag, _
                           120, 260, 80, 60)
            e.Graphics.FillRectangle(brushHorz, _
                               220, 260, 80, 60)
            e.Graphics.FillRectangle(brushHorzBrick, _
                           320, 260, 80, 60)
            e.Graphics.FillRectangle(brushLgChkBoard, _
                           420, 260, 80, 60)

            e.Graphics.FillRectangle(brushLgGrid, _
                           20, 340, 80, 60)
            e.Graphics.FillRectangle(brushLtDnDiag, _
                           120, 340, 80, 60)
            e.Graphics.FillRectangle(brushLtHorz, _
                           220, 340, 80, 60)
            e.Graphics.FillRectangle(brushUpDiag, _
                           320, 340, 80, 60)
            e.Graphics.FillRectangle(brushLtVert, _
                           420, 340, 80, 60)
        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

This would produce:

Hatch Brushes

Practical LearningPractical Learning: Using a Hatch Brush

  1. Start a new Windows Forms Application named YearlySales2
  2. Design the form as follows:
     
    Company Yearly Sales
     
    Control Name Text
    GroupBox GroupBox   Current Year's Sales
    Label Label   1st Qtr
    Label Label   2nd Qtr
    Label Label   3rd Qtr
    Label Label   4th Qtr
    TextBox TextBox txtCurrentQtr1 0
    TextBox TextBox txtCurrentQtr2 0
    TextBox TextBox txtCurrentQtr3 0
    TextBox TextBox txtCurrentQtr4 0
    Button Button Close btnClose
    GroupBox GroupBox   Previous Year's Sales
    Label Label   1st Qtr
    Label Label   2nd Qtr
    Label Label   3rd Qtr
    Label Label   4th Qtr
    TextBox TextBox txtPreviousQtr1 0
    TextBox TextBox txtPreviousQtr2 0
    TextBox TextBox txtPreviousQtr3 0
    TextBox TextBox txtPreviousQtr4 0
    Button Button Generate btnGenerate
    Label Label   _________ Legend _________
    Label Label   This Year's Sales
    Label Label   Last Year's Sales
  3. Double-click an unoccupied area of the form and change the file as follows:
     
    Imports System.Drawing.Drawing2D
    
    Public Class Form1
    
        Private graphDrawingArea As Graphics
        Private bmpDrawingArea As Bitmap
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
            bmpDrawingArea = New Bitmap(Width, Height)
            graphDrawingArea = Graphics.FromImage(bmpDrawingArea)
        End Sub
    End Class
  4. In Method Name combo box, select Paint and implement its event as follows:
     
    Private Sub Form1_Paint(ByVal sender As Object, _
                            ByVal e As System.Windows.Forms.PaintEventArgs) _
                            Handles Me.Paint
            e.Graphics.DrawImage(bmpDrawingArea, 0, 0)
    End Sub
  5. In the Class Name combo box, select btnGenerate
  6. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnGenerate_Click(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) _
                                      Handles btnGenerate.Click
            ' Retrieve the values of the current year's sales
            Dim CurQtr1 As Integer
            Dim CurQtr2 As Integer
            Dim CurQtr3 As Integer
            Dim CurQtr4 As Integer
    
            Try
                CurQtr1 = CInt(txtCurrentQtr1.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                CurQtr2 = CInt(txtCurrentQtr2.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                CurQtr3 = CInt(txtCurrentQtr3.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                CurQtr4 = CInt(txtCurrentQtr4.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            ' Create an array of Rectangle objects for the current year
            Dim RectCurrentYear As Rectangle() = { _
                    New Rectangle(txtCurrentQtr1.Left + 20, _
                                380 - CurQtr1, 40, CurQtr1), _
                    New Rectangle(txtCurrentQtr2.Left + 20, _
                                 380 - CurQtr2, 40, CurQtr2), _
                    New Rectangle(txtCurrentQtr3.Left + 20, _
                                380 - CurQtr3, 40, CurQtr3), _
                    New Rectangle(txtCurrentQtr4.Left + 20, _
                                380 - CurQtr4, 40, CurQtr4)}
    
            ' Retrieve the values of last year's sales
            Dim PrvQtr1 As Integer
            Dim PrvQtr2 As Integer
            Dim PrvQtr3 As Integer
            Dim PrvQtr4 As Integer
    
            Try
                PrvQtr1 = CInt(txtPreviousQtr1.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                PrvQtr2 = CInt(txtPreviousQtr2.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                PrvQtr3 = CInt(txtPreviousQtr3.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                PrvQtr4 = CInt(txtPreviousQtr4.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            ' Create an array of Rectangle objects for the previous year
            Dim RectPreviousYear As Rectangle() = { _
                    New Rectangle(txtPreviousQtr1.Left + 30, _
                         380 - PrvQtr1, 40, PrvQtr1), _
                    New Rectangle(txtPreviousQtr2.Left + 30, _
                         380 - PrvQtr2, 40, PrvQtr2), _
                    New Rectangle(txtPreviousQtr3.Left + 30, _
                         380 - PrvQtr3, 40, PrvQtr3), _
                    New Rectangle(txtPreviousQtr4.Left + 30, _
                         380 - PrvQtr4, 40, PrvQtr4)}
    
            ' In case the user has changed the values, erase the previous chart
            GraphDrawingArea.Clear(BackColor)
    
            Dim brushDiagCross As HatchBrush = _
                 New HatchBrush(HatchStyle.DiagonalCross, _
                          Color.White, Color.Blue)
            Dim brushDotDiamond As HatchBrush = _
             New HatchBrush(HatchStyle.DottedDiamond, _
                      Color.Fuchsia, Color.Brown)
    
            ' Draw the chart for the previous year first to send it back
            GraphDrawingArea.FillRectangles(brushDiagCross, _
                                 RectPreviousYear)
            GraphDrawingArea.DrawRectangles(New Pen(Color.Blue), _
                                 RectPreviousYear)
            ' Draw the chart for the current year in front
            GraphDrawingArea.FillRectangles(brushDotDiamond, _
                                 RectCurrentYear)
            GraphDrawingArea.DrawRectangles(New Pen(Color.Red), _
                                 RectCurrentYear)
    
            ' Draw the small rectangles of the legend
                graphDrawingArea.FillRectangle(brushDotDiamond,
                                lblCurYear.Left - 30,
                                lblCurYear.Top, 20, 14)
                graphDrawingArea.DrawRectangle(new Pen(Color.Red),
                                lblCurYear.Left - 30,
                                lblCurYear.Top, 20, 14)
                graphDrawingArea.FillRectangle(brushDiagCross,
                                lblLastYear.Left - 30,
                                lblLastYear.Top, 20, 14)
                graphDrawingArea.DrawRectangle(new Pen(Color.Blue),
                                lblLastYear.Left - 30,
                                lblLastYear.Top, 20, 14)
    
                graphDrawingArea.DrawRectangle(new Pen(Color.Black),
                                25, 380, Width - 220, 1)
            Invalidate()
    End Sub
  7. Execute the application and test the form
     
     Yearly Sales
  8. After using it, close the form

The Background Color of a Hatch Brush

If you use the above constructor to fill out a shape, the selected pattern would be drawn on top of a black color used as the background. If you want to use a different background, use the following constructor to initialize the brush:

Public Sub New(hatchstyle As HatchStyle, foreColor As Color, backColor As Color)

The backColor argument passed as a Color value will be used as the background Color. Here are examples of specifying the back color:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim hsBrush As HatchStyle() = _
                    { _
                        HatchStyle.BackwardDiagonal, _
                        HatchStyle.Cross, _
                        HatchStyle.Divot, _
                        HatchStyle.DarkDownwardDiagonal, _
                        HatchStyle.DarkHorizontal, _
                        HatchStyle.ForwardDiagonal, _
                        HatchStyle.DarkUpwardDiagonal, _
                        HatchStyle.DarkVertical, _
                        HatchStyle.HorizontalBrick, _
                        HatchStyle.DashedDownwardDiagonal, _
                        HatchStyle.DashedHorizontal, _
                        HatchStyle.DashedVertical, _
                        HatchStyle.LargeCheckerBoard, _
                        HatchStyle.DiagonalBrick, _
                        HatchStyle.Horizontal, _
                        HatchStyle.DiagonalCross, _
                        HatchStyle.DottedGrid, _
                        HatchStyle.DottedDiamond, _
                        HatchStyle.LightUpwardDiagonal, _
                        HatchStyle.LargeConfetti, _
                        HatchStyle.LargeGrid, _
                        HatchStyle.LightDownwardDiagonal, _
                        HatchStyle.OutlinedDiamond, _
                        HatchStyle.LightHorizontal, _
                        HatchStyle.LightVertical _
                    }

            Dim ForeColors As Color() = _
                    { _
                        Color.FromArgb(0, 0, 255), Color.FromArgb(200, 0, 0), _
                        Color.Salmon, Color.Navy, Color.Pink, _
                        Color.FromArgb(255, 0, 255), Color.FromArgb(255, 128, 0), _
                        Color.FromArgb(0, 128, 192), Color.Green, _
                        Color.Firebrick, Color.Fuchsia, Color.Moccasin, _
                        Color.Goldenrod, Color.Gainsboro, Color.Khaki, _
                        Color.Maroon, Color.DarkCyan, Color.Purple, _
                        Color.MistyRose, Color.RoyalBlue, Color.Red, _
                        Color.SaddleBrown, Color.Olive, Color.SeaGreen, _
                        Color.PowderBlue _
                    }

            Dim BackColors As Color() = _
                   { _
                       Color.Azure, Color.DarkBlue, Color.AntiqueWhite, _
                       Color.Aqua, Color.DarkGray, Color.Aquamarine, _
                       Color.Azure, Color.Beige, Color.DarkGoldenrod, _
                       Color.Bisque, Color.DarkKhaki, Color.BlanchedAlmond, _
                       Color.Brown, Color.DarkCyan, Color.AliceBlue, _
                       Color.BurlyWood, Color.CadetBlue, Color.DarkMagenta, _
                       Color.Coral, Color.Chartreuse, Color.CornflowerBlue, _
                       Color.Cornsilk, Color.Crimson, Color.Cyan, _
                       Color.DarkGreen _
                   }

            Dim rnd As Random = New Random()

            Dim brushBackDiag As HatchBrush = _
    New HatchBrush(hsBrush(rnd.Next(25)), _
             ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushCross As HatchBrush = _
        New HatchBrush(hsBrush(rnd.Next(25)), _
                 ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDarkDown As HatchBrush = _
        New HatchBrush(hsBrush(rnd.Next(25)), _
                 ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDarkHorz As HatchBrush = _
        New HatchBrush(hsBrush(rnd.Next(25)), _
                 ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDarkUpDiag As HatchBrush = _
        New HatchBrush(hsBrush(rnd.Next(25)), _
                 ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushVertical As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDashDnDiag As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDashHorz As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDashUpDiag As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDashVert As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDiagBrisk As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDiagCross As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDivot As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDotDiamond As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushDottedGrid As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushForDiag As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushHorz As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushHorzBrick As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushLgChkBoard As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushLgConfetti As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushLgGrid As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushLtDnDiag As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushLtHorz As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushUpDiag As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
            Dim brushLtVert As HatchBrush = _
            New HatchBrush(hsBrush(rnd.Next(25)), _
                     ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))

            e.Graphics.FillRectangle(brushBackDiag, 20, 20, 80, 60)
            e.Graphics.FillRectangle(brushCross, 120, 20, 80, 60)
            e.Graphics.FillRectangle(brushDarkDown, 220, 20, 80, 60)
            e.Graphics.FillRectangle(brushDarkHorz, 320, 20, 80, 60)
            e.Graphics.FillRectangle(brushDarkUpDiag, 420, 20, 80, 60)

            e.Graphics.FillRectangle(brushVertical, 20, 100, 80, 60)
            e.Graphics.FillRectangle(brushDashDnDiag, 120, 100, 80, 60)
            e.Graphics.FillRectangle(brushDashHorz, 220, 100, 80, 60)
            e.Graphics.FillRectangle(brushDashUpDiag, 320, 100, 80, 60)
            e.Graphics.FillRectangle(brushDashVert, 420, 100, 80, 60)

            e.Graphics.FillRectangle(brushDashVert, 20, 180, 80, 60)
            e.Graphics.FillRectangle(brushDiagBrisk, 120, 180, 80, 60)
            e.Graphics.FillRectangle(brushDiagCross, 220, 180, 80, 60)
            e.Graphics.FillRectangle(brushDivot, 320, 180, 80, 60)
            e.Graphics.FillRectangle(brushDotDiamond, 420, 180, 80, 60)

            e.Graphics.FillRectangle(brushDottedGrid, 20, 260, 80, 60)
            e.Graphics.FillRectangle(brushForDiag, 120, 260, 80, 60)
            e.Graphics.FillRectangle(brushHorz, 220, 260, 80, 60)
            e.Graphics.FillRectangle(brushHorzBrick, 320, 260, 80, 60)
            e.Graphics.FillRectangle(brushLgChkBoard, 420, 260, 80, 60)

            e.Graphics.FillRectangle(brushLgGrid, 20, 340, 80, 60)
            e.Graphics.FillRectangle(brushLtDnDiag, 120, 340, 80, 60)
            e.Graphics.FillRectangle(brushLtHorz, 220, 340, 80, 60)
            e.Graphics.FillRectangle(brushUpDiag, 320, 340, 80, 60)
            e.Graphics.FillRectangle(brushLtVert, 420, 340, 80, 60)

        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

This would produce:

Hatch Style

At any time, to find out the color used to paint a pattern, you can access the brush's ForegroundColor property. To know the color used as background, you can access the brush's BackgroundColor property. To know the hatch style used on the current brush, you can access its HatchStyle property.

 
 

Previous Copyright 2008-2009, yevol.com Next