Appunti di Programmazione

Creative Commons License

Rendere cliccabile una figura geometrica

Volete che un'area dello schermo o di un disegno di una PictureBox sia sensibile al click del mouse? Potete fare qualcosa di simile:

Public Class Form1

    Dim myPath As New Drawing2D.GraphicsPath

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        myPath.StartFigure()
        myPath.AddEllipse(20, 30, 50, 35)
        myPath.CloseFigure()

    End Sub

    Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As  _
                                       System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick

        If myPath.IsVisible(PictureBox1.PointToClient(MousePosition)) Then MessageBox.Show("Click")

    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As  _
                                      System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        If myPath.IsVisible(PictureBox1.PointToClient(MousePosition)) Then MessageBox.Show("Click")

    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
    Handles PictureBox1.Paint

        e.Graphics.FillEllipse(Brushes.Blue, 20, 30, 50, 35)

    End Sub

End Class