[VB.net][WinForm]Panel控制元件移動\滑鼠拖動

kerenseaman發表於2016-11-30

“`
‘控制控制元件移動
Public Shared whetherSelected As Boolean
Public Shared p As Point

‘窗體首次顯示
Private Sub frm_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
whetherSelected = False
p = New Point()“
End Sub

Region “Panel控制元件移動程式碼”

'滑鼠按下
Private Sub pnlPopUp01_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlPopUp01.MouseDown, lblPopup01Head.MouseDown
    whetherSelected = True
    p.X = Cursor.Position.X
    p.Y = Cursor.Position.Y
End Sub

'滑鼠抬起
Private Sub pnlPopUp01_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlPopUp01.MouseUp, lblPopup01Head.MouseUp
    whetherSelected = False
End Sub

'滑鼠移動
Private Sub pnlPopUp01_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlPopUp01.MouseMove, lblPopup01Head.MouseMove
    If whetherSelected = True Then

        pnlPopUp01.Left = pnlPopUp01.Left + (Cursor.Position.X - p.X)
        pnlPopUp01.Top = pnlPopUp01.Top + (Cursor.Position.Y - p.Y)

        p.X = Cursor.Position.X
        p.Y = Cursor.Position.Y

    End If

End Sub

End Region

相關文章