You've almost finished the first part of the chapter, and you've seen all the basic classes and objects required in order to draw specified shapes and so on to the screen. This section starts off by reviewing some of the drawing methods the Graphics class makes available and presents a short example that illustrates the use of several brushes and pens.
System.Drawing.Graphics has a large number of methods that allow you to draw various lines, outline shapes, and solid shapes. Once again there are too many to provide a comprehensive list here, but the following table lists the main ones and should give you some idea of the variety of shapes you can draw.
Method | Typical Parameters | What it Draws |
---|---|---|
DrawLine | Pen, start and end points | A single straight line |
DrawRectangle | Pen, position, and size | Outline of a rectangle |
DrawEllipse | Pen, position, and size | Outline of an ellipse |
FillRectangle | Brush, position, and size | Solid rectangle |
FillEllipse | Brush, position, and size | Solid ellipse |
DrawLines | Pen, array of points | Series of lines, connecting each point to the next one in the array |
DrawBezier | Pen, 4 points | A smooth curve through the two end points, with the remaining two points used to con- trol the shape of the curve |
DrawCurve | Pen, array of points | A smooth curve through the points |
DrawArc | Pen, rectangle, two angles | Portion of circle within the rectangle defined by the angles |
DrawClosedCurve | Pen, array of points | Like DrawCurve but also draws a straight line to close the curve |
DrawPie | Pen, rectangle, two angles | Wedge-shaped outline within the rectangle |
FillPie | Brush, rectangle, two angles | Solid wedge-shaped area within the rectangle |
DrawPolygon | Pen, array of points | Like DrawLines but also connects first and last points to close the figure drawn |
Before leaving the subject of drawing simple objects, this section rounds off with a simple example that demonstrates the kinds of visual effects you can achieve using brushes. The example is called ScrollMoreShapes, and it's essentially a revision of ScrollShapes. Besides the rectangle and ellipse, you'll add a thick line and fill in the shapes with various custom brushes. You've already learned the principles of drawing, so the code speaks for itself. First, because of your new brushes, you need to indicate you are using the System.Drawing.Drawing2D namespace:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms;
Next are some extra fields in your Form1 class, which contain details of the locations where the shapes are to be drawn, as well as various pens and brushes you will use:
private Rectangle rectangleBounds = new Rectangle(new Point(0,0), new Size(200,200)); private Rectangle ellipseBounds = new Rectangle(new Point(50,200), new Size(200,150)); private Pen bluePen = new Pen(Color.Blue, 3); private Pen redPen = new Pen(Color.Red, 2); private Brush solidAzureBrush = Brushes.Azure; private Brush solidYellowBrush = new SolidBrush(Color.Yellow); static private Brush brickBrush = new HatchBrush(HatchStyle.DiagonalBrick, Color.DarkGoldenrod, Color.Cyan); private Pen brickWidePen = new Pen(brickBrush, 10);
The brickBrush field has been declared as static, so that you can use its value to initialize the brick WidePen field. C# won't let you use one instance field to initialize another instance field, because it's not defined which one will be initialized first. However, declaring the field as static solves the problem. Because only one instance of the Form1 class will be instantiated, it is immaterial whether the fields are static or instance fields.
Here is the OnPaint() override:
protected override void OnPaint( PaintEventArgs e ) { base.OnPaint(e); Graphics dc = e.Graphics; Point scrollOffset = this.AutoScrollPosition; dc.TranslateTransform(scrollOffset.X, scrollOffset.Y); if (e.ClipRectangle.Top+scrollOffset.X < 350 || e.ClipRectangle.Left+scrollOffset.Y < 250) { dc.DrawRectangle(bluePen, rectangleBounds); dc.FillRectangle(solidYellowBrush, rectangleBounds); dc.DrawEllipse(redPen, ellipseBounds); dc.FillEllipse(solidAzureBrush, ellipseBounds); dc.DrawLine(brickWidePen, rectangleBounds.Location, ellipseBounds.Location+ellipseBounds.Size); } }
As before, you also set the AutoScrollMinSize to (250,350). Figure 25-13 shows the new results.
Figure 25-13
Notice that the thick diagonal line has been drawn on top of the rectangle and ellipse, because it was the last item to be painted.