Ahmad.Tr Ahmad.Tr - 1 month ago 12
Vb.net Question

How to handle click event on shapes of System.Drawing

i've been trying to reach the following result in a winforms vb.net application

Desired Result

where each arc or circle in this image is clickable ,
clickable arc is colored in pink.

i managed to write the following code

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

'Create pen objects
Dim p As New Pen(Color.Green, 30)
Dim p1 As New Pen(Color.Yellow, 30)
Dim p2 As New Pen(Color.Red, 30)
Dim p3 As New Pen(Color.Blue, 30)

'Create rectangle objects
Dim rt As New Rectangle(160, 150, 80, 100)
Dim rt1 As New Rectangle(100, 150, 80, 100)
Dim rt2 As New Rectangle(130, 120, 80, 100)
Dim rt3 As New Rectangle(130, 180, 80, 100)

'Draw arcs
e.Graphics.DrawArc(p, rt, 45, -90)
e.Graphics.DrawArc(p1, rt1, -135, -90)
e.Graphics.DrawArc(p2, rt2, -45, -90)
e.Graphics.DrawArc(p3, rt3, 135, -90)

End Sub


that resulted the following output .

Output

what i didn't figure out is :

1- how to make a border for each of the arcs.

2- how to handle clicks on each of the arcs.

is there any better way than the way i'am trying to pull this off.

any help would be appreciated .

Answer

Special Thanks for Reza Aghaei for the help , i used in my solution the following code

Private pGraphicsPath As New GraphicsPath

Private Sub usercontrol1_Click(sender As Object, e As MouseEventArgs) Handles Me.Click
    If pGraphicsPath.IsVisible(e.Location) Then
        MsgBox("Surface Hit")
    Else
        MsgBox("Missed")
    End If
End Sub

Private Sub usercontrol1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

    CreateGraphicsPathArcs(55, e)
    CreateGraphicsPathArcs(145, e)
    CreateGraphicsPathArcs(235, e)
    CreateGraphicsPathArcs(325, e)

End Sub

    Private Sub CreateGraphicsPathArcs(ByVal v_intStartAngle As Integer, ByVal e As System.Windows.Forms.PaintEventArgs)

    Dim center = New Point(100, 100)
    Dim innerR = 70
    Dim thickness = 20
    Dim startAngle = v_intStartAngle
    Dim arcLength = 70
    Dim outerR = innerR + thickness
    Dim outerRect = New Rectangle(center.X - outerR, center.Y - outerR, 2 * outerR, 2 * outerR)
    Dim innerRect = New Rectangle(center.X - innerR, center.Y - innerR, 2 * innerR, 2 * innerR)


        pGraphicsPath.AddArc(outerRect, startAngle, arcLength)
        pGraphicsPath.AddArc(innerRect, startAngle + arcLength, -arcLength)
        pGraphicsPath.CloseFigure()
        e.Graphics.FillPath(Brushes.Green, pGraphicsPath)
        e.Graphics.DrawPath(Pens.Black, pGraphicsPath)

End Sub

i used Reza's answer in the following stackoverflow question :

How to draw a circular progressbar pie using GraphicsPath in WinForm?

how can i treat the circle as a control after drawing it?