Private blnMoving As Boolean = False Private MouseDownX As Integer Private MouseDownY As Integer Private MovingRect As Rectangle Private oldRect As Rectangle Private blnClick As Boolean = False Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown If e.Button = MouseButtons.Left Then blnMoving = True MouseDownX = e.X MouseDownY = e.Y MovingRect = New Rectangle(Button1.Location, Button1.Size) End If End Sub Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp If e.Button = MouseButtons.Left And Not blnClick Then Button1.Location = Me.PointToClient(New Point(MovingRect.X, MovingRect.Y)) Me.Refresh() oldRect = oldRect.Empty blnMoving = False Else blnClick = False blnMoving = False End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click blnClick = True End Sub Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove If blnMoving Then If Not oldRect.IsEmpty Then ControlPaint.DrawReversibleFrame(MovingRect, Me.BackColor, FrameStyle.Thick) Button1.Refresh() End If Dim temp As Point = Me.PointToScreen(New Point(Button1.Location.X + (e.X - MouseDownX), Button1.Location.Y + (e.Y - MouseDownY))) MovingRect.X = temp.X MovingRect.Y = temp.Y If Me.ClientRectangle.Contains(Me.RectangleToClient(MovingRect)) Then oldRect = MovingRect Else MovingRect = oldRect End If ControlPaint.DrawReversibleFrame(MovingRect, SystemColors.Control, FrameStyle.Thick) End If End Sub
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)