Brushes

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); }

Brushes are used to fill basic geometric shapes such as rectangles, ellipses, and polygons. The System.Drawing.Brush class is an abstract base class and cannot be instantiated directly. The Brush class serves as a base class for various brush styles. Figure 4.1 shows four of the common brush styles in action.

Figure 4.1. Basic brushes.

figure 4.1. basic brushes.

The following sections describe the Brush class styles.

SolidBrush

A SolidBrush is just as its name implies: it creates a solid color that fills a graphics primitive such as a triangle, circle, or rectangle. All graphics primitives are constructed from the most basic primitive, the point. Points can be used to construct lines, circles, and polygons. From these basic shapes, more complex shapes can be constructed. Creating a SolidBrush is a simple matter of knowing the color of the brush to create and passing the color to the constructor of the SolidBrush class. In the case of Figure 4.1, the SolidBrush was created with the following code:

 SolidBrush solidBrush = new SolidBrush( System.Drawing.Color.Blue ); 

The SolidBrush object is created by specifying the color of the brush. The color can be specified either by using a predefined color found in the Color class or by using the FromArgb method of the Color class.

With the brush created, a call to the FillRectangle method of the graphics object creates a solid blue rectangle. Listing 4.1 shows the code snippet from the Brush Demo used to create and render the SolidBrush.

Listing 4.1 Creating a SolidBrush
 1: protected override void OnPaint( PaintEventArgs e ) { 2:  SolidBrush solidBrush = new SolidBrush( System.Drawing.Color.Blue ); 3:  e.Graphics.FillRectangle( solidBrush, rcDisplayRect ); 4:  solidBrush.Dispose( ); 5: } 

Notice the call to the Dispose method of the solidBrush object. GDI+ objects tend to be resource intensive, and it's recommended that the Dispose method should be invoked to free the associated resources when the GDI+ object is no longer needed.

HatchBrush

A HatchBrush defines a brush with a foreground and background color, along with a HatchSytle enum or pattern for the brush. The HatchBrush displayed in Figure 4.2 uses a foreground color of white, a background color of black, and the Cross hatch style. At last count 56 hatch styles were defined within the HatchSytle enum. If you're interested in seeing all of them, use the code snippet in Listing 4.2 and change the HatchStyle accordingly. A complete listing of the various hatch styles can be found by searching the online help for System.Drawing.Drawing2D.HatchStyle enum value.

Figure 4.2. The HatchBrush.

figure 4.2. the hatchbrush.

Listing 4.2 Creating a HatchBrush
 1: protected override void OnPaint( PaintEventArgs e ) { 2: HatchBrush hatchBrush = new HatchBrush( HatchStyle.Cross, System.Drawing.Color.White,  graphics/ccc.gifSystem.Drawing.Color.Black ); 3: e.Graphics.FillRectangle( hatchBrush, rcDisplayRect ); 4:  hatchBrush.Dispose( ); 5:} 

Providing a sample of every style of HatchStyle available would consume far too much space, but you should experiment with different HatchSytles to see the result. The following code snippet shows how to create a Weave hatch style with a blue foreground color and a gray background:

 HatchBrush hatchBrush = new HatchBrush( HatchStyle.Weave, Color.Blue, Color.Grey ); 

LinearGradientBrush

A linear gradient is a transition from one color to another. The LinearGradientBrush class defines a starting and ending color and creates a smooth transition from one color to another. In addition, it is also possible to define an angle for the transition. Figure 4.3 shows a gradient from Black to White with a 45-degree slope for the transition.

Figure 4.3. The LinearGradientBrush.

figure 4.3. the lineargradientbrush.

As with the other brushes, creating a gradient style brush is a simple matter of providing the desired parameters during construction. Listing 4.3 shows the code used to create the image shown in Figure 4.3.

Listing 4.3 The LinearGradientBrush
 1: protected virtual void OnPaint( PaintEventArgs e ) { 2: Rectangle rcDisplayRect = new Rectangle( 0, 0, 100, 100 ); 3: LinearGradientBrush lgb = new LinearGradientBrush( rcDisplayRect, 4:                                                    Color.Black, 5:                                                    Color.White, 6:                                                    45f ); 7:   e.Graphics.FillRectangle( lgb, rcDisplayRect ); 8:   lgb.Dispose( ); 9: } 

The last parameter for the LinearGradientBrush constructor is the angle or rotation for the transition. In C# it is necessary to post-fix a float constant with the lowercase letter f in order for proper type safety. Such post-fixing of constants is found in languages such as C, C++, and even Visual Basic.

Also, the angle parameter is based on a left-handed coordinate system, meaning that the rotation is counterclockwise. To produce a downward slope, you can use a negative value or provide angle values greater than 90 degrees. Only extermination will help to make this clear.

GDI+ uses both 2D and 3D graphics concepts to accomplish rotation, scaling, and translation. Before panic sets in, be aware that the most 2D or 3D graphics fundamentals require nothing more than basic geometry, trigonometry, and matrix manipulation. Books solely devoted to 2D and 3D graphics are widely available in just about every bookstore.

TexturedBrush

The last brush covered is the TexturedBrush. The TexturedBrush allows for an image to be used as a repeating pattern for the brush. The image can be any image format such as a bitmap, jpeg, or gif, to name a few. Figure 4.4 shows the TexturedBrush using a jpeg as the repeating image.

Figure 4.4. The TexturedBrush.

figure 4.4. the texturedbrush.

As with all the brushes presented so far, no real skill is needed to create the TexturedBrush. Listing 4.4 shows the code to create the TexturedBrush shown in Figure 4.4.

Listing 4.4 Creating a TexturedBrush
 1: protected override void OnPaint( PaintEventArgs e ) { 2:  System.Drawing.Image samsImage = Image.FromFile( "sams.jpg" ); 3:  TextureBrush tb = new TextureBrush( samsImage ); 4:  e.Graphics.FillRectangle( tb, new Rectangle( 0, 0, 500, 100 ) ); 5:  samsImage.Dispose( ); 6:  tb.Dispose( ); 7: } 

Listing 4.4 starts by loading a jpg image from a disk and then uses the newly created image to create a TextureBrush. This TextureBrush is then used to fill a rectangle much in the same manner as a solid brush or any other GDI+ brush.

This covers the basics of brushes. Listing 4.5 shows the complete source for the Brush Demo from Figure 4.1.

Listing 4.5 Brush Demo Source
   1: using System;   2: using System.Drawing;   3: using System.Drawing.Drawing2D; //TexturedBrush, HatchBrush, LinearGradientBrush   4: using System.Collections;   5: using System.ComponentModel;   6: using System.Windows.Forms;   7: using System.Data;   8:   9: namespace BrushDemo  10: {  11:     /// <summary>  12:     /// Summary description for Form1.  13:     /// </summary>  14:     public class Form1 : System.Windows.Forms.Form  15:     {  16:         /// <summary>  17:         /// Required designer variable.  18:         /// </summary>  19:         private System.ComponentModel.Container components = null;  20:  21:         public Form1()  22:         {  23:             //  24:             // Required for Windows Form Designer support  25:             //  26:             InitializeComponent();  27:             this.Paint += new PaintEventHandler( this.OnPaintEventHandler );  28:             this.SizeChanged += new EventHandler( this.OnSizeChangedEventHandler );  29:  30:             //Default the brushes  31:  32:         }  33:  34:         protected void OnSizeChangedEventHandler( object sender, EventArgs e ) {  35:             this.Invalidate( );  36:         }  37:  38:         protected void OnPaintEventHandler( object sender,   graphics/ccc.gifSystem.Windows.Forms.PaintEventArgs e ) {  39:             int width = this.ClientRectangle.Width - 20;  40:             int height = this.ClientRectangle.Height / 4;  41:             int top = 0;  42:             int left = 10;  43:             int step = height;  44:             Rectangle rcDisplayRect = new Rectangle( left, top, width, height );  45:             rcDisplayRect.Inflate(0,-10);  46:  47:             SolidBrush solidBrush = new SolidBrush( System.Drawing.Color.Blue );  48:             e.Graphics.FillRectangle( solidBrush, rcDisplayRect );  49:             solidBrush.Dispose( );  50:  51:   rcDisplayRect.Offset( 0, step );  52:             HatchBrush hatchBrush = new HatchBrush( HatchStyle.Weave,  graphics/ccc.gifSystem.Drawing.Color.Blue, System.Drawing.Color.Gray );  53:             e.Graphics.FillRectangle( hatchBrush, rcDisplayRect );  54:             hatchBrush.Dispose( );  55:  56:             rcDisplayRect.Offset( 0, step );  57:             LinearGradientBrush lgb = new LinearGradientBrush( rcDisplayRect,  58:                        System.Drawing.Color.Blue,  59:                        System.Drawing.Color.Red,  60:                        45f );  61:             e.Graphics.FillRectangle( lgb, rcDisplayRect );  62:             lgb.Dispose( );  63:  64:  65:             rcDisplayRect.Offset( 0, step );  66:             System.Drawing.Image samsImage = Image.FromFile("sams.jpg");  67:             TextureBrush tb = new TextureBrush( samsImage );  68:             e.Graphics.FillRectangle( tb, rcDisplayRect );  69:             samsImage.Dispose( );  70:             tb.Dispose( );  71:  72:         }  73:  74:         /// <summary>  75:         /// Clean up any resources being used.  76:         /// </summary>  77:         protected override void Dispose( bool disposing )  78:         {  79:             if( disposing )  80:             {  81:                 if (components != null)  82:                 {  83:                     components.Dispose();  84:                 }  85:             }  86:             base.Dispose( disposing );  87:         }  88:  89:      #region Windows Form Designer generated code  90:         /// <summary>  91:         /// Required method for Designer support - do not modify  92:         /// the contents of this method with the code editor.  93:         /// </summary>  94:         private void InitializeComponent()  95:         {  96:             //  97:             // Form1  98:             //  99:             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 100:             this.BackColor = System.Drawing.SystemColors.Window; 101:             this.ClientSize = new System.Drawing.Size(528, 517); 102:             this.Name = "Form1"; 103:             this.Text = "GDI+ Brush Demo"; 104: 105:         } 106:         #endregion 107: 108:         /// <summary> 109:         /// The main entry point for the application. 110:         /// </summary> 111:         [STAThread] 112:         static void Main() 113:         { 114:             Application.Run(new Form1()); 115:         } 116:  } 117: } 

Listing 4.5 reprents a fairly typical Windows Forms application as developed using the IDE. Note the comments on lines 91 and 92 which specifiy that the code within the InitializeComponent method should not be modified. This is due to the fact that the IDE and the Forms Designer use this method to persist code for creating controls placed on the form during design-time. This interaction between the designer and generated code is covered in Chapter 3, "Designer Basics."

All the painting code takes place within the OnPaintEventHandler method. Most of the code is derived from the previous code listings for creating and using GDI+ brushes. As with any new programming endeavor, you should modify the sample and experiment with the various brush properties and styles.



    .NET Windows Forms Custom Controls
    User Interfaces in VB .NET: Windows Forms and Custom Controls
    ISBN: 1590590449
    EAN: 2147483647
    Year: 2002
    Pages: 74

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net