|
Draggable / Resizable Rectangle Control
Published: 11/6/2008 |
|
I wrote this starter control the other day to illustrate how to build a control that can be dragged around and resized by the user at runtime. It uses the MouseDown, MouseMove, and MouseUp events to determine where the cursor is located when the user presses and holds the mouse button and initiates a resize or drag operation accordingly. Enjoy!
VB:
' TODO: add a property for each of these settings...
Private m_ptMinSize As Point = New Point(25, 25)
Private m_boolAllowDrag As Boolean = True
Private m_boolAllowResize As Boolean = True
Private m_clrBorderColor As Color = Color.Black
' internal variables...
Private m_boolFocused As Boolean = False
Private m_boolDragging As Boolean = False
Private m_boolSizing As Boolean = False
Private m_crSizeType As Cursor = Cursors.Default
Private m_ptDragOffset As Point = New Point(0, 0)
Private Sub DraggableRectangle_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If (m_boolAllowDrag) Then m_boolDragging = (Cursor = Cursors.Hand)
If m_boolAllowResize Then m_boolSizing = Not m_boolDragging
m_ptDragOffset = New Point(e.X, e.Y)
End Sub
Private Sub DraggableRectangle_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
' perform drag...
If m_boolDragging Then
Me.Top = Me.Top + (e.Y - m_ptDragOffset.Y)
Me.Left = Me.Left + (e.X - m_ptDragOffset.X)
Exit Sub
End If
' perform size...
If m_boolSizing Then
Select Case Cursor
Case Cursors.SizeNS
If e.Y >= m_ptMinSize.Y Then Me.Height = e.Y
Case Cursors.SizeWE
If e.X >= m_ptMinSize.X Then Me.Width = e.X
Case Else
If e.Y >= m_ptMinSize.Y Then Me.Height = e.Y
If e.X >= m_ptMinSize.X Then Me.Width = e.X
End Select
'redraw...
Me.Invalidate()
Exit Sub
End If
' alter cursor if not dragging or sizing...
If (e.X = Me.Width - 2 Or e.X = Me.Width - 3) And _
(e.Y = Me.Height - 2 Or e.Y = Me.Height - 3) Then
' bottom right corner...
Me.Cursor = Cursors.SizeNWSE
ElseIf (e.X = Me.Width - 2 Or e.X = Me.Width - 3) Then
' right side...
Me.Cursor = Cursors.SizeWE
ElseIf (e.Y = Me.Height - 2 Or e.Y = Me.Height - 3) Then
' bottom side...
Me.Cursor = Cursors.SizeNS
Else
Me.Cursor = Cursors.Hand
End If
End Sub
Private Sub DraggableRectangle_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
m_boolDragging = False
m_boolSizing = False
End Sub
Private Sub DraggableRectangle_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
m_boolFocused = True
Me.Invalidate()
End Sub
Private Sub DraggableRectangle_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
m_boolFocused = False
Me.Invalidate()
End Sub
Private Sub DraggableRectangle_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' draw borders...
' TODO: set pen color according to a property on the control...
Dim clsPen As Pen = New Pen(m_clrBorderColor, 1)
' set up pen appearance...
If m_boolFocused Then
clsPen.DashStyle = Drawing2D.DashStyle.Dash
Else
clsPen.DashStyle = Drawing2D.DashStyle.Solid
End If
' draw border...
e.Graphics.DrawRectangle(clsPen, New Rectangle(New Point(1, 1), New Size(Me.Size.Width - 2, Me.Size.Height - 2)))
End Sub
Questions or Comments? .
VB to C# and C# to VB translation provided by Instant C# and Instant VB.