Public Class ResizeableForm
Public Event ResizingDone(ByRef ftr As Form)
Private WithEvents FormToResize As Form
Private _Edge As EdgeEnum = EdgeEnum.None
Private _AllowEdges As EdgeEnum = EdgeEnum.All
Private _OutlineWidth As Integer = 4
Private _OutlineDrawn As Boolean = False
Private _OutlineColor As Drawing.Color = Color.Yellow
Private _MouseDown As Boolean = False
Private _MousePointerLastLocation As Point = New Point(0, 0)
Private _OriginalCursor As Cursor = Nothing
Private g As Graphics = Nothing
Public Enum EdgeEnum
None = 0
Right = 1
Left = 2
Top = 4
Bottom = 8
All = Left Or Right Or Top Or Bottom
End Enum
Public Property AllowEdges() As EdgeEnum
Get
Return _AllowEdges
End Get
Set(ByVal value As EdgeEnum)
_AllowEdges = value
End Set
End Property
Public Property OutlineColor() As Drawing.Color
Get
Return _OutlineColor
End Get
Set(ByVal value As Drawing.Color)
_OutlineColor = value
End Set
End Property
Public Property MouseDown As Boolean
Get
Return _MouseDown
End Get
Set(value As Boolean)
_MouseDown = value
End Set
End Property
Public Property OriginalCursor As Cursor
Get
Return _OriginalCursor
End Get
Set(value As Cursor)
_OriginalCursor = value
End Set
End Property
Public Sub New(ByVal FormToResize As Form)
Me.FormToResize = FormToResize
End Sub
Private Sub FormToResize_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FormToResize.MouseDown
If e.Button = MouseButtons.Left Then _MouseDown = True
End Sub
Private Sub FormToResize_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FormToResize.MouseUp
_MouseDown = False
End Sub
Private Sub FormToResize_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FormToResize.MouseMove
With FormToResize
g = .CreateGraphics
Dim b As New SolidBrush(OutlineColor)
If _OutlineDrawn Then
.Refresh()
_OutlineDrawn = False
End If
Select Case _Edge
Case EdgeEnum.Left
g.FillRectangle(b, 0, 0, _OutlineWidth, .Height)
_OutlineDrawn = True
Case EdgeEnum.Right
g.FillRectangle(b, .Width - _OutlineWidth, 0, .Width, .Height)
_OutlineDrawn = True
Case EdgeEnum.Top
g.FillRectangle(b, 0, 0, .Width, _OutlineWidth)
_OutlineDrawn = True
Case EdgeEnum.Bottom
g.FillRectangle(b, 0, .Height - _OutlineWidth, .Width, _OutlineWidth)
_OutlineDrawn = True
End Select
If _MouseDown AndAlso _Edge <> EdgeEnum.None Then
.SuspendLayout()
Select Case _Edge
Case EdgeEnum.Left
.SetBounds(.Left + e.X, .Top, .Width - e.X, .Height)
Case EdgeEnum.Right
.SetBounds(.Left, .Top, .Width - (.Width - e.X), .Height)
Case EdgeEnum.Top
.SetBounds(.Left, .Top + e.Y, .Width, .Height - e.Y)
Case EdgeEnum.Bottom
.SetBounds(.Left, .Top, .Width, .Height - (.Height - e.Y))
End Select
RaiseEvent ResizingDone(FormToResize)
.ResumeLayout()
Else
OriginalCursor = Cursor.Current
Select Case True
Case e.X <= _OutlineWidth
.Cursor = Cursors.SizeWE
_Edge = EdgeEnum.Left
Case e.X > .Width - (_OutlineWidth + 1)
.Cursor = Cursors.SizeWE
_Edge = EdgeEnum.Right
Case e.Y <= _OutlineWidth
.Cursor = Cursors.SizeNS
_Edge = EdgeEnum.Top
Case e.Y > .Height - (_OutlineWidth + 1)
.Cursor = Cursors.SizeNS
_Edge = EdgeEnum.Bottom
Case Else
.Cursor = OrininalCursor
_Edge = EdgeEnum.None
End Select
End If
End With
End Sub
Private Sub FormToResize_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormToResize.MouseLeave
FormToResize.Cursor = OriginalCursor
_Edge = EdgeEnum.None
FormToResize.Refresh()
g = Nothing
End Sub
End Class