Imports System.Drawing.Point PublicClass MoveClass Move PrivateWithEvents control As System.Windows.Forms.Control Private mouse_offset As Point Private frm As Form PublicSub New()SubNew(ByVal theControl As System.Windows.Forms.Control, ByVal theForm As Form) Me.control = theControl '鼠标拖动的控件 Me.frm = theForm '要移动的窗口 End Sub PrivateSub GroupControl1_MouseDown()Sub GroupControl1_MouseDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.MouseEventArgs) Handles control.MouseDown mouse_offset =New Point(e.X, e.Y) End Sub PrivateSub GroupControl1_MouseMove()Sub GroupControl1_MouseMove(ByVal sender AsObject, ByVal e As System.Windows.Forms.MouseEventArgs) Handles control.MouseMove If (e.Button = MouseButtons.Left Or e.Button = MouseButtons.Right) Then Dim mousePos As Point = frm.MousePosition '获得鼠标偏移量 mousePos.Offset(-mouse_offset.X, -mouse_offset.Y) '设置窗体随鼠标一起移动 frm.Location = mousePos EndIf End Sub End Class
使用:
在窗口加载时:
Move move = new Move(this.label1,this) 这样鼠标按住 label1 拖动时,窗口就会移动。