1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
Imports System.Drawing.Drawing2D
Public Class Form3
Private g As Graphics
Private ArrayChamp As Point() = Nothing
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
End Sub
Private Sub btnCreePolygonChamp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreePolygonChamp.Click
' Cree un tableau de points du champ.
ArrayChamp = New Point() {New Point(33, 30), New Point(50, 20), _
New Point(67, 30), New Point(60, 50), New Point(40, 50)}
' Create un Graphics du picturebox
g = Graphics.FromHwnd(Me.PictureBox1.Handle)
End Sub
Private Sub PictureBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
'Cursor en croix
Cursor = Cursors.Cross
'Un point arbitraire
Dim ptArbitraire As Point = e.Location
'Test
Dim dansChamp As Boolean = False
If g IsNot Nothing And ArrayChamp IsNot Nothing Then
dansChamp = IsVisiblePointDansChamp(g, ArrayChamp, ptArbitraire)
If dansChamp Then
MessageBox.Show(dansChamp.ToString())
Else
MessageBox.Show("choisissez un autre point....")
End If
End If
End Sub
Public Function IsVisiblePointDansChamp(ByVal g As Graphics, ByVal ArrayChamp As Point(), ByVal ptTest As Point) As Boolean
'1-Cree un GraphicsPath & lui ajoute polygone Champ
Dim myPath As New GraphicsPath
myPath.AddPolygon(ArrayChamp)
'ATTENTION OHJOE: IL N'EST PAS NECESSAIRE DE DESSINER
'LE POLYGONE SUR PICTUREBOX.
'TU PEUX ENLEVER CETTE LIGNE.C'EST JUSTE POUR ILLUSTRER LA METHODE
'Dessine le path à l'ecran sur picturebox.
Dim myPen As New Pen(Color.Black, 2)
g.DrawPath(myPen, myPath)
'2-Tester ptTest est "dans" polygone.
Dim visible As Boolean = myPath.IsVisible(ptTest.X, ptTest.Y, g)
MessageBox.Show(visible.ToString())
Return visible
End Function
End Class |
Partager