Resizing a Windows Forms border if the border is set to None.

Twisty Nado 1 Reputation point
2021-10-29T10:50:39.403+00:00

Hello, I am trying to make a simple program that I made to test around with in Windows Forms. I am programming in Visual Basic and I am trying to resize the border of the program. Is this possible? Thanks!

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2021-10-29T14:54:12.96+00:00

    Try adding this Sub to your form class:

    Protected Overrides Sub WndProc(ByRef m As Message)
    
        Const WM_NCHITTEST = &H84
        Const HTCAPTION = 2
        Const HTLEFT = 10
        Const HTRIGHT = 11
        Const HTTOP = 12
        Const HTBOTTOM = 15
    
        Select Case m.Msg
            Case WM_NCHITTEST
                Dim p = New Point(m.LParam.ToInt32)
                Dim r = Me.Bounds
                Dim b = New Size(10, 10) ' or SystemInformation.FrameBorderSize '
    
                If p.X <= r.Left + b.Width Then
                    m.Result = New IntPtr(HTLEFT)
                ElseIf p.X >= r.Right - b.Width Then
                    m.Result = New IntPtr(HTRIGHT)
                ElseIf p.Y <= r.Top + b.Height Then
                    m.Result = New IntPtr(HTTOP)
                ElseIf p.Y >= r.Bottom - b.Height Then
                    m.Result = New IntPtr(HTBOTTOM)
                Else
                    m.Result = New IntPtr(HTCAPTION)
                End If
            Case Else
                MyBase.WndProc(m)
        End Select
    
    End Sub
    

    You can improve it to allow diagonal resizing.

    2 people found this answer helpful.
    0 comments No comments