Reputation: 35
I saw this code to resize and move my borderless form and I copied it to my project.
This is good I'm searching for something like this for a while now, all I can see are tutorials made for c#. May I know if there are drawbacks for using this? One more thing the form is flickering when I'm resizing it from the top or left but if I resized it from right or bottom it's good, just like a normal form. I don't what could be the problem since honestly, I don't understand what is written below. It seems that it uses some hex values.
I tried to set the form double buffered = true
it doesn't work.
Public Class Form2
Private Const WM_NCHITTEST As Integer = &H84
Private Const WM_MOUSEMOVE As Integer = &H200
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_LBUTTONUP As Integer = &H202
Private Const MK_LBUTTON As Integer = &H1
Private Const HTLEFT As Integer = &HA
Private Const HTRIGHT As Integer = &HB
Private Const HTTOP As Integer = &HC
Private Const HTTOPLEFT As Integer = &HD
Private Const HTTOPRIGHT As Integer = &HE
Private Const HTBOTTOM As Integer = &HF
Private Const HTBOTTOMLEFT As Integer = &H10
Private Const HTBOTTOMRIGHT As Integer = &H11
Private OffSet As Point = Point.Empty
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = FormBorderStyle.None
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_NCHITTEST Then
Dim loc As New Point(m.LParam.ToInt32 And &HFFFF, m.LParam.ToInt32 >> 16)
loc = PointToClient(loc)
Dim bTop As Boolean = (loc.Y < ClientRectangle.Y + 4)
Dim bLeft As Boolean = (loc.X < ClientRectangle.X + 4)
Dim bRight As Boolean = (loc.X > Width - 4)
Dim bBottom As Boolean = (loc.Y > Height - 4)
If bTop And bLeft Then
m.Result = CType(HTTOPLEFT, IntPtr)
Return
ElseIf bTop And bRight Then
m.Result = CType(HTTOPRIGHT, IntPtr)
Return
ElseIf bBottom And bLeft Then
m.Result = CType(HTBOTTOMLEFT, IntPtr)
Return
ElseIf bBottom And bRight Then
m.Result = CType(HTBOTTOMRIGHT, IntPtr)
Return
ElseIf bLeft Then
m.Result = CType(HTLEFT, IntPtr)
Return
ElseIf bTop Then
m.Result = CType(HTTOP, IntPtr)
Return
ElseIf bRight Then
m.Result = CType(HTRIGHT, IntPtr)
Return
ElseIf bBottom Then
m.Result = CType(HTBOTTOM, IntPtr)
Return
End If
ElseIf m.Msg = WM_LBUTTONDOWN Then
OffSet = New Point(MousePosition.X - Me.Location.X, MousePosition.Y - Me.Location.Y)
ElseIf m.Msg = WM_MOUSEMOVE AndAlso m.WParam.ToInt32 = MK_LBUTTON Then
Me.Location = New Point(MousePosition.X - OffSet.X, MousePosition.Y - OffSet.Y)
End If
MyBase.WndProc(m)
End Sub
End Class
Upvotes: 1
Views: 364
Reputation: 550
Generally, WndProc is an application-defined function that processes messages sent to a window. In your example, Form2 is considered to be a frameless window. In a raw frameless window, you cannot move or resize the window, unless you override its WndProc protected function. In your example, the m.Msg (which indicates to the ID number for the message) is compared with different operating system messages (WM_NCHITTEST, WM_LBUTTONDOWN, and so on) to determine what action should be carried out (resize, move, ...). You can find a complete list of system messages ids here. About the flickering problem. honestly this problem not happens on my system. It may depend on your operating system and your windows version.
Upvotes: 1