The Brush Class 2-D Graphics Programming with GDI+

167

4.6.3 The Brush Class

Brush objects hold the settings used when filling graphics areas. All of the Graphics classs Fill...methods FillClosedCurve, FillEllipse, etc. require that the caller supply a Brush object. The supplied Brush object determines how the interior of the figure will be painted. Ex am ple 4- 11 shows an OnPaint method that can be used to draw an ellipse on a user control or a form. It is similar to Ex am ple 4- 9 , but draws a filled ellipse rather than an outline. The lines that differ from Ex am ple 4- 9 are shown in bold. The resulting display is shown in Figur e 4- 12 . Example 4-11. Drawing a filled ellipse on a form Protected Overrides Sub OnPaintByVal e As PaintEventArgs Dim br As New SolidBrushMe.ForeColor Dim rect As Rectangle rect.X = Me.ClientRectangle.X + Me.ClientRectangle.Width \ 4 rect.Y = Me.ClientRectangle.Y + Me.ClientRectangle.Height \ 4 rect.Width = Me.ClientRectangle.Width \ 2 rect.Height = Me.ClientRectangle.Height \ 2 e.Graphics.FillEllipsebr, rect br.Dispose End Sub Figure 4-12. The ellipse drawn by the code in Ex a m ple 4 - 1 1 Note that Ex am ple 4- 11 is not entirely parallel to Ex am ple 4- 9 . Specifically, Ex am ple 4- 9 instantiated a Pen object directly, but Ex am ple 4- 11 instantiates an object from a class that derives from the Brush class rather than directly from the Brush class. Objects cant be directly instantiated from the Brush class. The classes that derive from Brush are: HatchBrush Fills an area with a hatch pattern. Hatch patterns are patterns of lines and spaces. The HatchBrush classs HatchStyle property determines the exact pattern of the hatch. It is defined in the System.Drawing.Drawing2D namespace. LinearGradientBrush Fills an area with a gradient blend of two or more colors. It is defined in the System.Drawing.Drawing2D namespace. 168 PathGradientBrush Fills the internal area of a GraphicsPath object with a gradient blend of two or more colors. It is defined in the System.Drawing.Drawing2D namespace. SolidBrush Fills an area with a solid color. It is defined in the System.Drawing namespace. TextureBrush Fills an area with an image. It is defined in the System.Drawing namespace. The .NET Framework provides a number of predefined brushes through the properties of the Brushes and SystemBrushes classes defined in the System.Drawing namespace. For example, the Blue property of the Brushes class returns a Brush object that fills areas with solid blue. Thus, the following line of code draws a solid blue ellipse: e.Graphics.FillEllipseBrushes.Blue, rect Similarly, the SystemBrushes classs Window property returns a Brush object whose color is set to the background color of the systems window client area. Using the standard brushes provided by the Brushes and SystemBrushes classes can be more efficient than instantiating new Brush objects. However, their properties cannot be altered. See Table 4- 1 for the list of Brush objects available through the Brushes class. See Sect ion 4.6.4.1 for the list of Brush objects available through the SystemBrushes class.

4.6.4 The Color Structure