vb.net add Aero Snap property in my borderless form

jagath pathirana 1 Reputation point
2022-08-19T20:15:12.5+00:00

i am try to make border less form' it's work fine. but i cant add "Aero Snap" property for it. i am try different method for make borderless form. but they are not resize smoothly. that method is only best way for smooth resize. i want it maximize when it drag upper side of screen. how i did that here is my whole code.
Imports System.Runtime.InteropServices

Public Class Form2
<DllImport("user32.dll&")>
Public Shared Function ReleaseCapture() As Boolean
End Function
<DllImport("user32.dll&")>
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function

Private Const HTCAPTION As Integer = 2

Private Const WM_NCLBUTTONDOWN As Integer = &HA1
Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.Style = cp.Style Or &H20000 '<;--- Minimize borderless form from taskbar
Return cp
End Get
End Property

Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown

If locked = True Then  
    locked = False  
    Exit Sub  
End If  

If direction_ = ResizeDirection.None Then  
ElseIf direction_ = ResizeDirection.title Then  
    MoveForm()  
Else  
    ResizeForm()  
End If  

End Sub
Private Sub MoveForm()
ReleaseCapture()
SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
Invalidate()
End Sub
Private Sub ResizeForm()
' mouse_down = True
ReleaseCapture()
SendMessage(Me.Handle, WM_NCLBUTTONDOWN, direction_, 0)
End Sub

Dim frame_width As Integer = 3
Dim title_width As Integer = 31
Public Enum ResizeDirection
None = 0
title = 1
Left = 10
TopLeft = 13
Top = 12
TopRight = 14
Right = 11
BottomRight = 17
Bottom = 15
BottomLeft = 16
End Enum
Dim direction_ As ResizeDirection
Dim locked As Boolean = False
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If Me.WindowState = FormWindowState.Maximized Then
If e.Y < title_width Then
direction_ = ResizeDirection.title
Else
direction_ = ResizeDirection.None
End If
Cursor = Cursors.Default
Exit Sub
End If

If e.Y &lt; frame_width Then  
    If e.X &lt; frame_width Then  
        Cursor = Cursors.SizeNWSE  
        direction_ = ResizeDirection.TopLeft  
    ElseIf e.X &gt; Me.Width - frame_width Then  
        Cursor = Cursors.SizeNESW  
        direction_ = ResizeDirection.TopRight  
    Else  
        Cursor = Cursors.SizeNS  
        direction_ = ResizeDirection.Top  
    End If  
ElseIf e.Y &gt; Me.Height - frame_width Then  
    If e.X &lt; frame_width Then  
        Cursor = Cursors.SizeNESW  
        direction_ = ResizeDirection.BottomLeft  
    ElseIf e.X &gt; Me.Width - frame_width Then  
        Cursor = Cursors.SizeNWSE  
        direction_ = ResizeDirection.BottomRight  
    Else  
        Cursor = Cursors.SizeNS  
        direction_ = ResizeDirection.Bottom  
    End If  
ElseIf e.X &lt; frame_width Then  
    Cursor = Cursors.SizeWE  
    direction_ = ResizeDirection.Left  
ElseIf e.X &gt; Me.Width - frame_width Then  
    Cursor = Cursors.SizeWE  
    direction_ = ResizeDirection.Right  
Else  
    If e.Y &lt; title_width Then  
        direction_ = ResizeDirection.title  
    Else  
        direction_ = ResizeDirection.None  
    End If  
    Cursor = Cursors.Default  
End If  

End Sub
Private Sub BTN_MINIMIZE_Click(sender As Object, e As EventArgs) Handles BTN_MINIMIZE.Click
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub BTN_MAXIMIZE_Click(sender As Object, e As EventArgs) Handles BTN_MAXIMIZE.Click
' Dim Hd As Integer = 6
If Me.WindowState = FormWindowState.Maximized Then

    Me.WindowState = FormWindowState.Normal  
    &#39;  Panel_TITLE.Height -= Hd  
    &#39;  BTN_MINIMIZE.Top -= Hd  
    &#39;  BTN_MAXIMIZE.Top -= Hd  
    &#39; BTN_CLOSE.Top -= Hd  

    &#39; SplitContainer1.Top -= Hd  
    &#39;  SplitContainer1.Height -= Hd  
Else  
      
    &#39; Me.ControlBox = True  
    Me.WindowState = FormWindowState.Maximized  
    &#39;  Me.ControlBox = False  
    &#39;   Panel_TITLE.Height += Hd  
    &#39;   BTN_MINIMIZE.Top += Hd  
    &#39;  BTN_MAXIMIZE.Top += Hd  
    &#39; BTN_CLOSE.Top += Hd  

    &#39;  SplitContainer1.Top += Hd  
    &#39;  SplitContainer1.Height += Hd  
End If  

End Sub
Private Sub BTN_CLOSE_Click(sender As Object, e As EventArgs) Handles BTN_CLOSE.Click
Application.Exit()
End Sub

'===============================================================================================================

<DllImport("user32.dll", EntryPoint:="FindWindowEx")>
Public Shared Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
End Function
<DllImport("User32.dll")>
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal uMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
End Function

Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
' TextBox1.Text = Cursor.Position.ToString + "%%%"
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.FormBorderStyle = FormBorderStyle.None
Me.ControlBox = False
Me.BackColor = SystemColors.ControlDark

Me.ResizeRedraw = True  
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)  

Dim rrr As Rectangle = Screen.FromRectangle(Me.Bounds).WorkingArea  
rrr.Width += 18  
rrr.Height -= 1  
Me.MaximumSize = rrr.Size  

End Sub

Private Sub Form1_Move(sender As Object, e As EventArgs) Handles Me.Move
If Me.Top = 0 Then

    ReleaseCapture()  
    SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)  
    &#39; Invalidate()  

    locked = True  

Else  
    locked = False  
End If  

End Sub
End Class

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

1 answer

Sort by: Most helpful
  1. Castorix31 81,461 Reputation points
    2022-08-20T07:54:20.66+00:00

    You can add Aero Snap by adding the WS_THICKFRAME style =>

    Protected Overrides ReadOnly Property CreateParams As CreateParams  
        Get  
            Dim cp As CreateParams = MyBase.CreateParams  
            If True Then  
                cp.Style = cp.Style Or WS_THICKFRAME Or WS_CAPTION  
            End If  
            Return cp  
        End Get  
    End Property  
      
    Public Const WS_THICKFRAME = &H40000  
    Public Const WS_CAPTION = &HC00000  
    
    Protected Overrides Sub WndProc(ByRef m As Message)  
        If m.Msg = WM_NCHITTEST Then  
            m.Result = New IntPtr(HTCAPTION)          
        ElseIf m.Msg = WM_NCCALCSIZE Then            
            m.Result = CType(0, IntPtr)  
        Else  
            MyBase.WndProc(m)  
        End If  
    End Sub  
    

    233008-aerosnap.gif

    1 person found this answer helpful.