10.2 Drawing a Rectangle

 <  Day Day Up  >  

You want to draw a rectangle on a Windows Form and optionally fill it with a defined color .


Technique

After obtaining a Graphics object, call the DrawRectangle or FillRectangle . The DrawRectangle method uses a Pen object to draw a rectangle. Create a Pen object passing a SolidBrush object to draw a solid border. The SolidBrush object uses a Color value that you can specify using a predefined static value defined in the Color structure, such as Color.Black or a custom color created with a call to the static method FromArgb defined in the Color structure. This method uses three parameters denoting the red, blue, and green mixtures to use. Each parameter must be a value in the 0 “255 range. You can optionally use a predefined brush by calling one of the static methods defined in the System.Drawing.Brushes class, as shown in the following code:

 
 Brush myBrush = System.Drawing.Brushes.DeepSkyBlue; 

The FillRectangle is similar to the DrawRectangle method, but it only needs a Brush object instead of a Pen . Both methods also need X, Y, width, and height values. The X and Y values are relative to the container the rendering is occurring on. In other words, if rendering is occurring on a Windows Form, then the X and Y values are relative to the upper-left corner of the form. The upper-left corner coordinates are (0,0) with subsequent viewable coordinates increasing in the positive direction, as shown in Listing 10.1.

Listing 10.1 Drawing Randomly Sized Rectangles with Random Colors
 private void Form1_Click(object sender, System.EventArgs e) {     Random randNum = new Random( DateTime.Now.Millisecond );     int width = randNum.Next( this.Width-mouseHit.X );     int height = randNum.Next( this.Height-mouseHit.Y );     int r = randNum.Next(255);     int g = randNum.Next(255);     int b = randNum.Next(255);     Graphics surface = this.CreateGraphics();     if( randNum.Next(2) == 0 )     {         surface.FillRectangle( new SolidBrush(Color.FromArgb(r,g,b)),             mouseHit.X, mouseHit.Y, width, height );     }     else     {         surface.DrawRectangle( new Pen( new SolidBrush(Color.FromArgb(r,g,b))),             mouseHit.X, mouseHit.Y, width, height );     }     surface.Dispose(); } private void Form1_MouseDown(object sender,     System.Windows.Forms.MouseEventArgs e) {     mouseHit = new Point( e.X, e.Y ); } 

Comments

When creating custom rendering algorithms, you might find it beneficial to visualize a piece of paper sitting on a desk. The top-left corner of the paper has a X coordinate of 0 and a Y coordinate of 0. As you move to the right across the paper, the X coordinate increases . If you move to the left, which is off the paper and onto the desk, the X coordinate is a negative number. The same can be said with the Y coordinate; the down direction is positive and the up direction is negative. If you were to take a pen and begin drawing a line going to the right of the paper, you would eventually reach the end. What would happen if you then continued drawing in the same direction? You would begin drawing on the desk itself, and in GDI+, this type of behavior is completely legal. You won't see the rendered output, but the rendering surface itself is there. In fact, if you were to move the actual piece of paper to the right ”the rendering surface, in the case of GDI+ ”you would eventually see the output drawn beyond the initial position of the surface. This method of moving is known as translation and is covered in Recipe 10.8.

Once you decide on the location to render a shape, you need to create a Pen or Brush object. A Brush object is for shapes whose interiors are filled. In the following recipes, you'll see how you can use different types of brushes to perform the filling method. To fill the interior of a shape with a solid color, create a SolidBrush object and specify a predefined color from the static values defined in the Color class, or define your own color using three values representing the red, green, and blue components of the color.

For shapes whose interiors are not filled, you need to use a Pen object. You create a Pen by associating it with a Brush object or Color value and an optional width. For instance, to create a Pen that renders a 2 pixel wide solid magenta color, you can use the following three methods:

 
 Pen p = new Pen( new SolidBrush( Color.Magenta ), 2.0f ); Pen p = new Pen( Color.Magenta, 2.0f ); Pen p = new Pen( Color.FromArgb( 255, 0, 255 ), 2.0f ); 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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