-
Feb 10th, 2016, 12:45 PM
#1
Thread Starter
New Member
How to Resize form without border
How to Resize form without border
add four picturebox to your borderless form
rename them upR, downR, rightR and leftR
then paste the code
Code:
Public Class Form1
Const wm_nchittest As Integer = &H84
Const htclient As Integer = &H1
Const htcaption As Integer = &H2
Protected Overrides Sub wndproc(ByRef m As System.Windows.Forms.Message)
Select Case m.Msg
Case wm_nchittest
MyBase.WndProc(m)
If m.Result = htclient Then m.Result = htcaption
Case Else
MyBase.WndProc(m)
End Select
End Sub
Private Sub UpR_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles UPR.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Me.Size = New Size(Me.Size.Width, Me.Size.Height + (Me.Location.Y - MousePosition.Y))
Me.Location = New Point(Me.Location.X, MousePosition.Y)
End If
End Sub
Private Sub DownR_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles downR.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Me.Size = New Size(Me.Size.Width, MousePosition.Y - Me.Location.Y)
End If
End Sub
Private Sub LeftR_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles leftR.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Me.Size = New Size(Me.Size.Width + (Me.Location.X - MousePosition.X), Me.Size.Height)
Me.Location = New Point(MousePosition.X, Me.Location.Y)
End If
End Sub
Private Sub RightR_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rightR.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Me.Size = New Size(MousePosition.X - Me.Location.X, Me.Size.Height)
End If
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
downR.Location = New Point(0, Me.Size.Height - 2)
rightR.Location = New Point(Me.Size.Width - 2, 0)
UPR.Size = New Size(Me.Size.Width, 2)
downR.Size = New Size(Me.Size.Width, 2)
leftR.Size = New Size(2, Me.Size.Height)
rightR.Size = New Size(2, Me.Size.Height)
End Sub
End Class
Last edited by Shaggy Hiker; Feb 10th, 2016 at 02:09 PM.
-
Feb 10th, 2016, 01:13 PM
#2
Re: How to Resize form without border
Tutorials and samples belongs to the CodeBank forum. You should also use the [CODE] tag around your code for better readability.
-
Feb 10th, 2016, 01:44 PM
#3
Re: How to Resize form without border
You don't have Option Strict On, which should be, so you don't get the two errors on the following line.
If m.Result = htclient Then m.Result = htcaption
m.Result is an IntPtr and Option Strict On doesn't allow comparing to an Integer Constant.
I guess you could cast m.Result to an Integer, but I would normally create preset IntPtr variables to compare against.
Perhaps someone else has a better or preferable way to handle this situation.
It would probably be good to set the mouse pointer in the pictureboxes to various arrows to indicate when you can click and the direction of resizing.
You don't preset the upR and leftR pictureboxes to location (0,0). The user should be told to do that, or perhaps set them in the Form Load.
The code I use for some of my sizing and moving of controls is not standard, but I find it convenient.
I use the left button to move and the right button to resize. The code looks like this, for the borderless form in this example.
Code:
Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Static lpos As Point
If e.Button = Windows.Forms.MouseButtons.Left Then 'move form
Me.Location += New Size(e.X - lpos.X, e.Y - lpos.Y)
ElseIf e.Button = Windows.Forms.MouseButtons.Right Then ' resize form (but no less than 10x10 pixels)
Dim newWidth As Integer = Math.Max(10, Me.Width + (e.X - lpos.X))
Dim newHeight As Integer = Math.Max(10, Me.Height + (e.Y - lpos.Y))
lpos = e.Location
Me.Size = New Size(newWidth, newHeight)
Else
lpos = e.Location
End If
End Sub
But the formMove code could be used in picture move events instead of the form, with the pictureboxes appropriately anchored to the edges of the form to remove the necessity of resizing code for the pictureboxes.
But to be traditional, there should probably be corner boxes to allow diagonal resizing if going the picturebox (or other edge controls) route.
p.s. I requested the thread be moved to the code bank, so don't start a new thread for this in the code bank.
Last edited by passel; Feb 10th, 2016 at 01:49 PM.
-
Feb 12th, 2016, 11:11 AM
#4
Re: How to Resize form without border
Have you seen this thread?
Move borderless form
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|