Resize controls at RunTime...

AlexCode

Well-known member
Joined
Jul 7, 2003
Messages
931
Location
Portugal
Hi.

Im just posting this here... because I think is something theres a lot of people wondering around on how to do it... (at least a lot of them already asked me how it could be donne)!

On this example I demonstrate how to resize a System.Windows.Forms.Button at RunTime.
All the user have to do is to point the mouse on the edges of the button, PRESS AND HOLD THE RIGHT MOUSE BUTTON and normally resize the control...

Anyone can use this code.
I took about 20 min to do it so it has a lot of things that can be improved (you can add a move function too... or design the squares like the IDE resizing), but for naow I think it will help some people out there...

There it goes, Enjoy :D


[VB]
Public Class myButton
Inherits System.Windows.Forms.Button

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

This call is required by the Windows Form Designer.
InitializeComponent()

Add any initialization after the InitializeComponent() call

End Sub

UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

NOTE: The following procedure is required by the Windows Form Designer
It can be modified using the Windows Form Designer.
Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

#End Region

#Region "[ Mouse Events ]"

Enum resizePos
None = -1
Left = 0
Right = 1
Top = 2
Bottom = 3
End Enum

Dim RPos As resizePos
Dim MDown As Boolean = False Stores the state of the mouse button
Dim CursorPos As New Point Stores the Cursor position when the mouse buttom was pressed
Dim StartSize As New System.Drawing.Size Stores the start size of the button
Dim StartLocation As New System.Drawing.Point Stores the Start Location of the button

Private Sub myButton_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

If the control is in RunTime then executes this code
If Me.DesignMode = False Then

If (e.X >= 0) And (e.X <= 5) Then
Me.Cursor = System.Windows.Forms.Cursors.SizeWE
RPos = resizePos.Left
ElseIf (e.X <= Me.Width) And (e.X >= Me.Width - 5) Then
Me.Cursor = System.Windows.Forms.Cursors.SizeWE
RPos = resizePos.Right
ElseIf (e.Y >= 0) And (e.Y <= 5) Then
Me.Cursor = System.Windows.Forms.Cursors.SizeNS
RPos = resizePos.Top
ElseIf (e.Y <= Me.Height) And (e.Y >= Me.Height - 5) Then
Me.Cursor = System.Windows.Forms.Cursors.SizeNS
RPos = resizePos.Bottom
ElseIf Not MDown Then
Me.Cursor = System.Windows.Forms.Cursors.Default
RPos = resizePos.None
End If


If MDown Then

Select Case RPos
Case resizePos.Left
Me.Location = New System.Drawing.Point(StartLocation.X + (Cursor.Position.X - StartLocation.X) - Me.FindForm.Left, StartLocation.Y)
Me.Width = StartSize.Width - (Cursor.Position.X - StartLocation.X) + Me.FindForm.Left
Case resizePos.Right
Me.Width = StartSize.Width + (Cursor.Position.X - CursorPos.X)
Case resizePos.Top
Here we have a little problem...
The top border of the form doesnt count, and differ from each border style
Im gonna use this work around but who wishes to develop further this
will have to find a better way... (I cant give you all !! <img src="images/smilies/biggrin.gif" border="0" alt="">)
If Me.FindForm.FormBorderStyle <> FormBorderStyle.None Then
Me.Location = New System.Drawing.Point(StartLocation.X, StartLocation.Y + (Cursor.Position.Y - StartLocation.Y) - Me.FindForm.Top - 28)
Me.Height = StartSize.Height - (Cursor.Position.Y - StartLocation.Y) + Me.FindForm.Top + 28
Else
Me.Location = New System.Drawing.Point(StartLocation.X, StartLocation.Y + (Cursor.Position.Y - StartLocation.Y) - Me.FindForm.Top)
Me.Height = StartSize.Height - (Cursor.Position.Y - StartLocation.Y) + Me.FindForm.Top
End If

Case resizePos.Bottom
Me.Height = StartSize.Height + (Cursor.Position.Y - CursorPos.Y)
End Select

TODO Delete this line...
Me.Text = CursorPos.X & "," & CursorPos.Y & " ; " & _
Cursor.Position.X & "," & Cursor.Position.Y
End If

End If


End Sub


Private Sub myButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
If e.Button = MouseButtons.Right Then
CursorPos = Cursor.Position
StartSize = Me.Size
StartLocation = Me.Location
MDown = True
End If
End Sub

Private Sub myButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
If MDown Then
MDown = False
Me.Cursor = System.Windows.Forms.Cursors.Default
RPos = resizePos.None
End If
End Sub

#End Region

End Class
[/VB]
 
Back
Top