Draw a circle with Paint event

nicnicman

New member
Joined
Feb 5, 2011
Messages
1
Programming Experience
Beginner
I'd like to draw a circle using after a mouse click using the Paint event. I'm able to do it by calling CreateGraphics but I was told this is not a good practice.

Here is what I have:

VB.NET:
Private Sub Form1_Paint1(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If Control.MouseButtons = Windows.Forms.MouseButtons.Left Then
            Dim g As Graphics = e.Graphics
            Dim pn As New Pen(Color.Blue, 3)
            g.DrawEllipse(pn, 50, 50, 200, 100)
        End If
    End Sub
Honestly I'm pretty lost. Any help would be greatly appreciated.
 
You might want to create a collection class to hold a bunch of shapes that you'll be creating in your MouseDown Event. Something Like:

VB.NET:
Public Class Shapes
    Implements IEnumerable
    Private mShapes As Generic.List(Of Shape) = New Generic.List(Of Shape)
    Public Function Add(ByVal pShape As Shape) As Integer

    End Function
    Public ReadOnly Property Count() As Integer
        Get

        End Get
    End Property
    Public Sub Draw(ByVal pGraphics As System.Drawing.Graphics)
        For Each vShape As DrawingShapes.Shape In mShapes
            vShape.Draw(pGraphics, New System.Drawing.Pen(Drawing.Color.Black))
        Next
    End Sub
    Public ReadOnly Property Item(ByVal pIndex As Integer) As Shape
        Get

        End Get
    End Property
    Public Sub Remove(ByVal pShape As Shape)

    End Sub
    Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        GetEnumerator = mShapes.GetEnumerator()
    End Function
End Class
Where a Shape might look like:

VB.NET:
Public Class Shape
    Public Overridable Sub Draw(ByVal pGraphics As System.Drawing.Graphics, Optional ByVal pPen As System.Drawing.Pen = Nothing)

    End Sub
End Class
With this structure in place you can create a circle class and potentially other types of shapes that you can create through your MouseDown and render in your Paint Event code. Circle would look something like:

VB.NET:
Public Class Circle
    Inherits Shape
    Private mCenter As System.Drawing.PointF
    Private mRadius As Single
    Public Sub New(ByVal pCenter As System.Drawing.PointF, ByVal pRadius As Single)

    End Sub
    Public ReadOnly Property Diameter() As Single
        Get

        End Get
    End Property
    Public Overrides Sub Draw(ByVal pGraphics As System.Drawing.Graphics, Optional ByVal pPen As System.Drawing.Pen = Nothing)
        pGraphics.DrawEllipse(pPen, mCenter.X - Me.Radius, mCenter.Y - Me.Radius, Me.Diameter, Me.Diameter)
    End Sub
    Public ReadOnly Property Radius() As Single
        Get

        End Get
    End Property
End Class
then your form could do something like:
VB.NET:
Public Class Form1
    Private mShapes As DrawingShapes.Shapes = New DrawingShapes.Shapes
    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        Static sCenter As Object = Nothing
        If sCenter Is Nothing Then
            sCenter = New System.Drawing.PointF(e.X, e.Y)
        Else
            Dim vRadius As Single = Math.Sqrt((sCenter.x - e.X) ^ 2 + (sCenter.y - e.Y) ^ 2)
            mShapes.Add(New DrawingShapes.Circle(sCenter, vRadius))
            sCenter = Nothing
            Me.Refresh()
        End If
    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        mShapes.Draw(e.Graphics)
    End Sub
End Class
Carlos
 
Back
Top