学习笔记 winForm move功能 与 drag 功能

 

vb.Net WinForm move button function

#Region "Move vb.net button"
    Private isDrag As Boolean = False
    Private downPoint As Point
    Private Sub Button2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
       Handles Button2.MouseDown
        isDrag = True
        downPoint = e.Location
    End Sub

    Private Sub Button2_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
      Handles Button2.MouseMove
        If isDrag Then
            Button2.Location = e.Location + Button2.Location - downPoint
        End If
    End Sub

    Private Sub Button2_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
      Handles Button2.MouseUp
        isDrag = False
    End Sub
#End Region

 

vb.Net WinForm drag button function

Set control's AllowDrag = True

Set groupBox's AllowDrag = True

#Region "drag vb.Net Button to GroupBox"
    Private Sub Button1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
        Handles Button1.MouseDown
        Button1.DoDragDrop(Button1, DragDropEffects.All)
    End Sub

    Private Sub GroupBox1_dragdrop(sender As Object, e As DragEventArgs) _
        Handles GroupBox1.DragDrop
        Button1.Left = e.X
        Button1.Top = e.Y

    End Sub

    Private Sub GroupBox1_DragOver(sender As Object, e As DragEventArgs) _
        Handles GroupBox1.DragOver
        e.Effect = DragDropEffects.All
    End Sub
#End Region

 

vb6 CodeArchitects.VB6Library.VB6CommandButton drag

Set control's AllowDrag = True

Set form's AllowDrag = True

 Dim CustomEditMode As Boolean = True
    Dim MouseY As Integer
    Dim MouseX As Integer
    Private Sub Button1_MouseDown(ByRef Button As Short, ByRef Shift As Short, ByRef x As Single, ByRef Y As Single) Handles Button1.MouseDown
        If CustomEditMode Then
            MouseY = Y
            MouseX = x
            Button1.Drag(1)
        End If
    End Sub

    Private Sub Button1_MouseUp(ByRef Button As Short, ByRef Shift As Short, ByRef x As Single, ByRef Y As Single) Handles Button1.MouseUp
        Button1.Drag(2)
    End Sub

    Private Sub Button1_DragDrop(ByRef Source As Control, ByRef x As Single, ByRef Y As Single) Handles Button1.DragDrop
        MouseY = (MouseY - Y)  
        MouseX = (MouseX - x) 

        Call Form_DragDrop(Source, Button1.Left, Button1.Top)

    End Sub

    Private Sub Form_DragDrop(ByRef Source As Control, ByRef x As Single, ByRef Y As Single) Handles MyBase.DragDrop
        If Not CustomEditMode Then 
            Exit Sub
        End If
        Source.Left = (x - MouseX) / 15
        Source.Top = (Y - MouseY) / 15
        Source.Enabled = True

    End Sub

 

posted @ 2013-04-03 11:17  xiaoyinxxy  阅读(595)  评论(0编辑  收藏  举报