日志文章

2007年11月08日 13:39:23

在winform中实现控件的拖动

Copy code
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Dim dragging As Boolean
Dim mousex As Integer
Dim mousey As Integer
Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
  dragging = True
  mousex = -e.X
  mousey = -e.Y
  Dim clipleft As Integer = Me.PointToClient(MousePosition).X - Panel1.Location.X
  Dim cliptop As Integer = Me.PointToClient(MousePosition).Y - Panel1.Location.Y
  Dim clipwidth As Integer = Me.ClientSize.Width - (Panel1.Width - clipleft)
  Dim clipheight As Integer = Me.ClientSize.Height - (Panel1.Height - cliptop)
  Windows.Forms.Cursor.Clip = Me.RectangleToScreen(New Rectangle(clipleft, cliptop, clipwidth, clipheight))
  Panel1.Invalidate()
End If
End Sub
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If dragging Then
  Dim MPosition As New Point()
  MPosition = Me.PointToClient(MousePosition)
  MPosition.Offset(mousex, mousey)
  Panel1.Location = MPosition
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
If dragging Then
  dragging = False
  Windows.Forms.Cursor.Clip = Nothing
  Panel1.Invalidate()
End If
End Sub
Private Sub Button1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
  dragging = True
  mousex = -e.X
  mousey = -e.Y
  Dim clipleft As Integer = Me.PointToClient(MousePosition).X - Button1.Location.X
  Dim cliptop As Integer = Me.PointToClient(MousePosition).Y - Button1.Location.Y
  Dim clipwidth As Integer = Me.ClientSize.Width - (Button1.Width - clipleft)
  Dim clipheight As Integer = Me.ClientSize.Height - (Button1.Height - cliptop)
  Windows.Forms.Cursor.Clip = Me.RectangleToScreen(New Rectangle(clipleft, cliptop, clipwidth, clipheight))
  Button1.Invalidate()
End If
End Sub
Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
If dragging Then
  Dim MPosition As New Point()
  MPosition = Me.PointToClient(MousePosition)
  MPosition.Offset(mousex, mousey)
  Button1.Location = MPosition
End If
End Sub
Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
If dragging Then
  dragging = False
  Windows.Forms.Cursor.Clip = Nothing
  Button1.Invalidate()
End If
End Sub
End Class

Tags: drag  

类别: vb.net |  评论(0) |  浏览(1115) |  收藏
发表评论