Recipe 9.7. Drawing Lines, Ellipses, and Rectangles


Problem

You need to draw some basic shapes on a graphics surface. What choices are available?

Solution

Sample code folder: Chapter 09\ DrawingBasicShapes

The System.Drawing.Graphics object includes several methods that draw filled and unfilled shapes, including methods for lines, rectangles, and ellipses. This recipe's code implements a simple drawing program using these basic shapes.

Discussion

Create a new Windows Forms application, and add the following controls to Form1:

  • A RadioButton control named DrawLine. Set its Text property to Line and its Checked property to true.

  • A RadioButton control named DrawRectangle. Set its Text property to Rectangle.

  • A RadioButton control named DrawEllipse. Set its Text property to Ellipse.

  • A ComboBox control named LineColor. Set its DropDownStyle property to DropDownList.

  • A ComboBox control named FillColor. Set its DropDownStyle property to DropDownList.

  • A PictureBox control named DrawingArea. Set its BackColor property to White (or 255, 255, 255) and its BorderStyle property to Fixed3D. Make it somewhat large.

Add informational labels if desired. The form should look like the one in Figure 9-10.

Figure 9-10. The controls on the shape drawing sample


Now add the following source code to the form's code template:

 Private FirstPoint As Point = New Point(-1, -1) Private Sub Form1_Load(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles MyBase.Load    ' ----- Fill in the list of colors.    For Each colorName As String In New String() _          {"Black", "Red", "Orange", "Yellow", "Green", _          "Blue", "Indigo", "Violet", "White"}       LineColor.Items.Add(colorName)       FillColor.Items.Add(colorName)    Next colorName    LineColor.SelectedIndex = LineColor.Items.IndexOf("Black")    FillColor.SelectedIndex = LineColor.Items.IndexOf("White") End Sub Private Sub DrawingArea_MouseDown(ByVal sender As Object, _       ByVal e As System.Windows.Forms.MouseEventArgs) _       Handles DrawingArea.MouseDown    ' ----- Time to do some drawing.    Dim useLine As Pen    Dim useFill As Brush    Dim canvas As Graphics    Dim drawBounds As Rectangle    ' ----- Is this the first or second click?    If (FirstPoint.Equals(New Point(-1, -1))) Then       ' ----- This is the first click. Record the location.       FirstPoint = e.Location       ' ----- Draw a marker at this point.       DrawMarker(FirstPoint)    Else       ' ----- Get the two colors to use.       useLine = New Pen(Color.FromName(LineColor.Text))       useFill = New SolidBrush(Color.FromName(FillColor.Text))       ' ----- Get the   drawing surface.       canvas = DrawingArea.CreateGraphics()       ' ----- Remove the first-point marker.       DrawMarker(FirstPoint)       ' ----- For   rectangles and   ellipses, get the       '       bounding area.       drawBounds = New Rectangle( _          Math.Min(FirstPoint.X, e.Location.X), _          Math.Min(FirstPoint.Y, e.Location.Y), _          Math.Abs(FirstPoint.X - e.Location.X), _          Math.Abs(FirstPoint.Y - e.Location.Y))       ' ----- Time to draw.       If (DrawLine.Checked = True) Then          ' ----- Draw a line.          canvas.DrawLine(useLine, FirstPoint, e.Location)       ElseIf (DrawRectangle.Checked = True) Then          ' ----- Draw a rectangle.          canvas.FillRectangle(useFill, drawBounds)          canvas.DrawRectangle(useLine, drawBounds)       Else          ' ----- Draw an ellipse.          canvas.FillEllipse(useFill, drawBounds)          canvas.DrawEllipse(useLine, drawBounds)       End If       ' ----- Clean up.       canvas.Dispose()       useFill.Dispose()       useLine.Dispose()       FirstPoint = New Point(-1, -1)    End If End Sub Private Sub DrawMarker(ByVal centerPoint As Point)    ' ----- Given a point, draw a small square at    '       that location.    Dim screenPoint As Point    Dim fillArea As Rectangle    ' ----- Determine the fill area.    screenPoint = DrawingArea.PointToScreen(centerPoint)    fillArea = New Rectangle(screenPoint.X - 2, _       screenPoint.Y - 2, 5, 5)    ' ----- Draw a red rectangle. Cyan is the RBG    '       inverse of red.    ControlPaint.FillReversibleRectangle(fillArea, Color.Cyan) End Sub 

Run the program, and use the RadioButton and ComboBox controls to select the object style and colors. Click on the DrawingArea controls twice to specify the two endpoints of each line, rectangle, or ellipse. Figure 9-11 shows the program in use.

Figure 9-11. Drawing basic shapes


Drawing shapes is so easy in .NET as to make it somewhat humdrum. Back in the early days of computer drawing, drawing a line or circle required a basic understanding of the geometric equations needed to produce such shapes on a Cartesian coordinate system. But no more! The Graphics object includes a set of methods designed to make drawing simple. Most of them are used throughout the recipes in this chapter.

This recipe's code spends some time watching for the locations of mouse clicks on the drawing surface. Once it has these locations and the user-selected colors, it draws the basic shapes in just a few quick statements:

 If (DrawLine.Checked = True) Then    canvas.DrawLine(useLine, FirstPoint, e.Location) ElseIf (DrawRectangle.Checked = True) Then    canvas.FillRectangle(useFill, drawBounds)    canvas.DrawRectangle(useLine, drawBounds) Else    canvas.FillEllipse(useFill, drawBounds)    canvas.DrawEllipse(useLine, drawBounds) End If 

See Also

Recipe 9.26 discusses the FillReversibleRectangle() method used in this recipe's code.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

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