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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
Public Class Form3
Private shp1 As Region
Private shp2 As Region
Private rect1 As New Rectangle(10, 10, 80, 40)
Private rect2 As New Rectangle(25, 25, 100, 70)
Private msgHit As String = "Ok! intersection"
Private msg As String = "Pas d'intersection"
Public Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
Me.Text = msg
Label1.Size = New Size(150, 150)
Me.Label1.BackColor = Color.FromArgb(50, Color.LightSkyBlue)
Me.Label1.BorderStyle = BorderStyle.Fixed3D
GroupBox1.Size = New Size(400, 300)
StatusLabel1.Text = msg
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CreateTwoRegion()
End Sub
Private Sub CreateTwoRegion()
shp1 = New Region(rect1)
shp2 = New Region(rect2)
Label1.Invalidate()
GroupBox1.Invalidate()
End Sub
Private ptDown As New Point()
Private isDown As Boolean
Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
ptDown = e.Location
CreateTwoRegion()
If e.Button = MouseButtons.Left Then
If Label1.ClientRectangle.Contains(ptDown) Then
isDown = True
End If
End If
End Sub
Private shpTest As Region
Private hit As Boolean
Private Sub Label1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
Dim ptMove As Point = e.Location
If isDown Then
'------deplace label1
Label1.Left += ptMove.X - ptDown.X
Label1.Top += ptMove.Y - ptDown.Y
' -----Recupere une copie du rectangle du shp1
Dim rc As Rectangle = rect1
'------Ramene ses coords clients par rapport au groupbox
rc.X += Label1.Left
rc.Y += Label1.Top
StatusLabel1.Text = rect1.Location.ToString + " - " + rc.Location.ToString
'------Cree une region en coords clients groupbox
shpTest = New Region(rc)
'-------L'intersecte avec le shp2 du groupbox
'-------nb:le resulat de cet appel genere un
'-------region shpTest qui contient l'intersection ou vide
shpTest.Intersect(shp2)
'------Invalidate du groupbox pour effectuer le hit test sur le region shpTest
'------le test Region.IsEmpty(graphics) renvoie un boolean Hit
GroupBox1.Invalidate()
If hit Then
Me.Text = msgHit
Else
Me.Text = msg
End If
End If
End Sub
Private Sub Label1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp
If e.Button = MouseButtons.Right Then
ptDown = New Point()
isDown = False
If hit Then
Me.Text = msgHit
Else
Me.Text = msg
End If
End If
End Sub
' brush region label original: sera un peu transparent pour
' "visualiser la partie intersecte du region groupbox "
Private brush1 As SolidBrush = New SolidBrush(Color.FromArgb(50, Color.Yellow))
Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
Dim gr As Graphics = e.Graphics
If shp1 IsNot Nothing Then
gr.FillRegion(brush1, shp1)
End If
End Sub
'brush region groupbox
Private brush2 As SolidBrush = Brushes.NavajoWhite
'brush du "copie region label"
Private brushTest As SolidBrush = Brushes.DarkBlue
Private Sub GroupBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupBox1.Paint
Dim gr As Graphics = e.Graphics
If shp2 IsNot Nothing Then
gr.FillRegion(brush2, shp2)
End If
' region test est - il vide?
If shpTest IsNot Nothing Then
gr.FillRegion(brushTest, shpTest)
'si intersection non vide
If Not shpTest.IsEmpty(gr) Then
hit = True
Else
hit = False
End If
End If
End Sub
End Class |
Partager