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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
| Imports Microsoft.VisualBasic.PowerPacks
Public Class Form1
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2
Private idxSelectBox As Integer
Private idxSelectLine As Integer
Private idxSelectType As MyTyp
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, _
ByVal wParam As Integer, ByVal lParam As String) As Integer
Private Declare Sub ReleaseCapture Lib "user32" ()
'Déplacement de la ligne
Private dragging As Boolean = False
Private oldShapePosition1 As Point
Private oldShapePosition2 As Point
Private mouseDownX1 As Integer 'Mouse position based on ShapeContainer
Private mouseDownY1 As Integer
Private mouseDownX2 As Integer
Private mouseDownY2 As Integer
Private idxLineMove As Integer
Private SelectBox As Boolean
Private selectline As Boolean
'Déplacement de la PictureBox
Private Sub pictBox_MouseMove(ByVal sender As PictureBox, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim lHwnd As Integer
lHwnd = sender.Handle
If lHwnd = 0 Then Exit Sub
ReleaseCapture()
SendMessage(lHwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
MouveBox(sender)
End Sub
Private Sub MouveBox(ByVal Pict As PictureBox)
idxSelectBox = Pict.Tag
SelectBox = True
selectline = False
idxSelectType = CollBox(Pict.Tag).Tp
CollBox(Pict.Tag).X1 = Pict.Left
CollBox(Pict.Tag).Y1 = Pict.Top
txtX1.Text = CStr(Pict.Left)
txtY1.Text = CStr(Pict.Top)
txtX2.Text = ""
txtY2.Text = ""
End Sub
Private Sub GroupBox2_Resize(sender As System.Object, e As System.EventArgs) Handles GroupBox2.Resize
End Sub
Private Sub PictureBox_Click(sender As System.Object, e As System.EventArgs) Handles pictBox1.Click, pictBox9.Click, pictBox8.Click, pictBox7.Click, pictBox6.Click, pictBox5.Click, pictBox4.Click, pictBox3.Click, pictBox2.Click, pictBox13.Click, pictBox12.Click, pictBox11.Click, pictBox10.Click
Dim tpBox As Integer = Val(sender.tag)
Dim param As New frmParam(tpBox)
If param.ShowDialog() = Windows.Forms.DialogResult.OK Then
If CollBox.Count > 0 Then
createBOX()
End If
End If
End Sub
Private Sub lblline_Click(sender As System.Object, e As System.EventArgs) Handles lblline.Click
Dim tpBox As Integer = Val(sender.tag)
Dim param As New frmLineAdd()
If param.ShowDialog() = Windows.Forms.DialogResult.OK Then
createLine()
End If
End Sub
Private Sub createBOX()
Dim idx As Integer = CollBox.Count - 1
CollBox(idx).Box = New PictureBox
CollBox(idx).Box.Image = ImageList1.Images(CollBox(idx).Tp)
CollBox(idx).Box.Name = "Pict" & CStr(idx)
CollBox(idx).Box.Size = New Size(40, 40)
CollBox(idx).Box.Location = New Point(0, 0)
CollBox(idx).Box.ContextMenuStrip = ContextMenuStrip1
CollBox(idx).Box.Tag = idx
Me.Panel1.Controls.Add(CollBox(idx).Box)
AddHandler CollBox(idx).Box.MouseMove, AddressOf pictBox_MouseMove
End Sub
Private Sub SupprimerToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SupprimerToolStripMenuItem.Click
Me.Panel1.Controls.Remove(CollBox(idxSelectBox).Box)
CollBox.Remove(idxSelectBox)
End Sub
Private Sub cmdValidMove_Click(sender As System.Object, e As System.EventArgs) Handles cmdValidMove.Click
Dim X1 As Integer = Val(txtX1.Text)
Dim X2 As Integer = Val(txtX2.Text)
Dim Y1 As Integer = Val(txtY1.Text)
Dim Y2 As Integer = Val(txtY2.Text)
Dim blackPen As New Pen(Color.Black, 3)
If CollBox.Count > 0 Then
If SelectBox And CollBox(idxSelectBox).Tp <> MyTyp.line Then
CollBox(idxSelectBox).Box.Top = Y1
CollBox(idxSelectBox).Box.Left = X1
Else
If selectline Then
CollLine(idxSelectLine).line.StartPoint = New System.Drawing.Point(X1, Y1)
CollLine(idxSelectLine).line.EndPoint = New System.Drawing.Point(X2, Y2)
End If
End If
End If
End Sub
#Region "Line"
Const HitTestDelta As Integer = 10
' The mouse position when mouse down
Dim oldMouseX As Integer
Dim oldMouseY As Integer
' The line position when mouse down.
Dim oldStartPoint As Point
Dim oldEndPoint As Point
Dim dragStartPoint As Boolean = False
Dim dragEndPoint As Boolean = False
Private Sub createLine(Optional ByVal line As LineShape = Nothing)
Dim valLine As Boolean = False
If line IsNot Nothing Then
valLine = True
End If
Dim idx As Integer = CollLine.Count - 1
CollLine(idx).line = New LineShape
CollLine(idx).LineCont = New ShapeContainer
CollLine(idx).line.Name = "Line" & CStr(idx)
CollLine(idx).line.BorderColor = Color.Black
CollLine(idx).line.BorderWidth = CollLine(idx).Epaisseur
CollLine(idx).line.Tag = idx
CollLine(idx).LineCont.Parent = Me.Panel1
CollLine(idx).line.Parent = CollLine(idx).LineCont
If valLine Then
CollLine(idx).line.StartPoint = New System.Drawing.Point(line.X1 + 10, line.Y1 + 10)
CollLine(idx).line.EndPoint = New System.Drawing.Point(line.X2 + 10, line.Y2 + 10)
Else
CollLine(idx).line.StartPoint = New System.Drawing.Point(idx * 20, 60 + idx * 60)
CollLine(idx).line.EndPoint = New System.Drawing.Point(100 + idx * 20, 110 + idx * 60)
End If
CollLine(idx).line.BringToFront()
CollLine(idx).LineCont.BringToFront()
Me.Panel1.Controls.Add(CollLine(idx).LineCont)
CollLine(idx).line.ContextMenuStrip = ContextMenuStrip2
' add handlers
AddHandler CollLine(idx).LineCont.MouseDown, AddressOf ShapeContainerMouseDownEventHandler
AddHandler CollLine(idx).LineCont.MouseMove, AddressOf ShapeContainerMouseMoveEventHandler
AddHandler CollLine(idx).LineCont.MouseUp, AddressOf ShapeContainerMouseUpEventHandler
End Sub
Private Sub ShapeContainerMouseDownEventHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim siSCId As Integer
Dim myShapeContainer As ShapeContainer
myShapeContainer = CType(sender, ShapeContainer)
Dim myLineShape As LineShape
siSCId = sender.tag
idxSelectLine = siSCId
If siSCId > -1 Then
myLineShape = CollLine(siSCId).line
myShapeContainer.BringToFront()
If (myLineShape.HitTest(MousePosition.X, MousePosition.Y)) Then
oldMouseX = e.X
oldMouseY = e.Y
oldStartPoint = myLineShape.StartPoint
oldEndPoint = myLineShape.EndPoint
dragStartPoint = MouseIsNearBy(oldStartPoint)
dragEndPoint = MouseIsNearBy(oldEndPoint)
If (Not dragStartPoint AndAlso Not dragEndPoint) Then
'If not drag either end, then drag both.
dragStartPoint = True
dragEndPoint = True
End If
myLineShape.SelectionColor = Color.Transparent
End If
End If
End Sub
Private Sub ShapeContainerMouseMoveEventHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim siSCId As Integer
Dim myShapeContainer As ShapeContainer
myShapeContainer = CType(sender, ShapeContainer)
Dim myLineShape As LineShape
siSCId = sender.tag()
If siSCId > -1 Then
SelectBox = False
selectline = True
myLineShape = CollLine(siSCId).line
If (dragStartPoint) Then
txtX1.Text = CStr(oldStartPoint.X + e.X - oldMouseX)
txtY1.Text = CStr(oldStartPoint.Y + e.Y - oldMouseY)
myLineShape.StartPoint = New Point(oldStartPoint.X + e.X - oldMouseX, oldStartPoint.Y + e.Y - oldMouseY)
End If
If (dragEndPoint) Then
txtX2.Text = CStr(oldEndPoint.X + e.X - oldMouseX)
txtY2.Text = CStr(oldEndPoint.Y + e.Y - oldMouseY)
myLineShape.EndPoint = New Point(oldEndPoint.X + e.X - oldMouseX, oldEndPoint.Y + e.Y - oldMouseY)
End If
End If
End Sub
Private Sub ShapeContainerMouseUpEventHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim siSCId As Integer
Dim myShapeContainer As ShapeContainer
myShapeContainer = CType(sender, ShapeContainer)
Dim myLineShape As LineShape
siSCId = sender.tag
If siSCId > -1 Then
myLineShape = CollLine(siSCId).line
dragStartPoint = False
dragEndPoint = False
myLineShape.SelectionColor = Color.Transparent
End If
myShapeContainer.Parent.Refresh()
End Sub
Private Function MouseIsNearBy(ByVal testPoint As Point) As Boolean
Dim X = MousePosition.X ' - 5
Dim Y = MousePosition.Y '- 10
testPoint = Panel1.PointToScreen(testPoint)
Return Math.Abs(testPoint.X - X) <= HitTestDelta _
AndAlso Math.Abs(testPoint.Y - Y) <= HitTestDelta
End Function
Private Sub CouleurToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CouleurToolStripMenuItem.Click
Dim colorPicker As New ColorDialog()
Try
colorPicker.ShowHelp = True
Dim siClickedShapeContainerId As Integer = -1
siClickedShapeContainerId = sender.tag
colorPicker.Color = CollLine(siClickedShapeContainerId).line.BorderColor
If (colorPicker.ShowDialog() = Windows.Forms.DialogResult.OK) Then
CollLine(siClickedShapeContainerId).line.BorderColor = colorPicker.Color
End If
Catch ex As Exception
MessageBox.Show("Sub ITM1_Click: " & ex.Message, Me.Text)
End Try
colorPicker.Dispose()
End Sub
Private Sub EpaisseurToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EpaisseurToolStripMenuItem.Click
Dim myDlgLineWidth As New DlgLineWidth
Try
Dim siClickedShapeContainerId As Integer = -1
siClickedShapeContainerId = sender.tag
myDlgLineWidth.LineWidth = CollLine(siClickedShapeContainerId).line.BorderWidth
If myDlgLineWidth.ShowDialog = Windows.Forms.DialogResult.OK Then
CollLine(siClickedShapeContainerId).line.BorderWidth = myDlgLineWidth.LineWidth
End If
Catch ex As Exception
MessageBox.Show("Sub ITM2_Click: " & ex.Message, Me.Text)
End Try
myDlgLineWidth.Dispose()
End Sub
Private Sub SuppressionToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SuppressionToolStripMenuItem.Click
Try
Dim siClickedShapeContainerId As Integer = -1
siClickedShapeContainerId = sender.tag
Me.Panel1.Controls.Remove(CollLine(siClickedShapeContainerId).LineCont)
CollLine.Remove(siClickedShapeContainerId)
Catch ex As Exception
MessageBox.Show("Sub ITM2_Click: " & ex.Message, Me.Text)
End Try
End Sub
Private Sub CopierToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CopierToolStripMenuItem.Click
createLine(CollLine(idxSelectLine).line)
End Sub
#End Region
End Class |
Partager