假設已經動態產生以下物件,並指定事件
'設定當滑鼠指標移入 Ellipse 物件上方時所要執行的事件處理常式。
AddHandler Elp.MouseLeftButtonDown, AddressOf Elp_MouseDown
AddHandler Elp.MouseMove, AddressOf Elp_MouseMove
AddHandler Elp.MouseLeftButtonUp, AddressOf Elp_MouseUP
滑鼠事件: 按下
'-----------------------------
Private Sub Elp_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
gloOriPos = e.GetPosition(Me.MYCS) '對哪一個物件取座標位置!
'偵測來源 sender 物件 並進行轉換
Select Case sender.GetType.ToString
Case "System.Windows.Shapes.Ellipse"
Dim Selobj As Ellipse = TryCast(sender, Ellipse)
gloSelObjName = Selobj.Name
Dim idx As Integer = 2
'改變顏色
Selobj.Fill = New SolidColorBrush(Color.FromRgb(ToRGB(idx).R, ToRGB(idx).G, ToRGB(idx).B))
'********** 以目前選取的物件 補捉滑鼠 ********** 這裡很重要
Selobj.CaptureMouse()
Case "System.Windows.Controls.TextBlock"
End Select
End Sub
滑鼠事件: 移動
'-----------------------------
Private Sub Elp_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
'偵測滑鼠左鍵是否仍然被按下
If e.LeftButton = MouseButtonState.Pressed Then
Dim NowPos As Point ' = e.GetPosition(Me.MYCS) '對哪一個物件取座標位置!
NowPos = e.GetPosition(Me.MYCS)
'滑鼠 移動量()
Dim dx As Double = NowPos.X - gloOriPos.X
Dim dy As Double = NowPos.Y - gloOriPos.Y
'Me.Title = e.GetPosition(Me.MYCS).X & "," & e.GetPosition(Me.MYCS).Y
'偵測來源物件並轉型
Select Case sender.GetType.ToString
Case "System.Windows.Shapes.Ellipse"
Dim Selobj As Ellipse = TryCast(sender, Ellipse)
Canvas.SetLeft(Selobj, NowPos.X - (gloEllipseWidth / 2))
Canvas.SetTop(Selobj, NowPos.Y - (gloEllipseWidth / 2))
gloSelObjName = Selobj.Name
'呼叫移動線段的程式碼(假設該目標有好幾個線段連結)
Call LineMove("EL", Selobj)
Case "System.Windows.Controls.TextBlock"
End Select
'紀錄上次的位置
gloOriPos.X = NowPos.X
gloOriPos.Y = NowPos.Y
End If
End Sub
滑鼠事件:放開
'-----------------------------
Private Sub Elp_MouseUP(ByVal sender As Object, ByVal e As MouseEventArgs)
'偵測來源物件並進行轉型
Select Case sender.GetType.ToString
Case "System.Windows.Shapes.Ellipse"
Dim Selobj As Ellipse = TryCast(sender, Ellipse)
gloSelObjName = Selobj.Name
'釋放滑鼠事件
Selobj.ReleaseMouseCapture()
'恢復原來的顏色
Dim idx As Integer = 1
Selobj.Fill = New SolidColorBrush(Color.FromRgb(ToRGB(idx).R, ToRGB(idx).G, ToRGB(idx).B))
Case "System.Windows.Controls.TextBlock"
End Select
End Sub
沒有留言:
張貼留言