1

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

5
  • You need to also make it flicker when dragging the top/left. Set ResizeRedraw = True in the constructor. May 25, 2020 at 9:25
  • I want to know why it is flickering when resizing from the top or left not to reproduce the flickering. The dragging is perfect. I can drag it with no problem even if it's a borderless form.
    – lelouch
    May 25, 2020 at 9:53
  • BTW, you can remove the WM_LBUTTONDOWN and WM_MOUSEMOVE: not needed. Add instead If loc.Y < SystemInformation.CaptionHeight + 2 Then m.Result = new IntPtr(2). This: Dim loc As New Point(m.LParam.ToInt32 And &HFFFF, m.LParam.ToInt32 >> 16) loc = PointToClient(loc) can be simplified in dim loc = PointToClient(new Point(m.LParam.ToInt32()))
    – Jimi
    May 25, 2020 at 10:53
  • Thank you for the links @jimi especially the second one, why it doesn't appear in google search when I search for this problem? Anyway thanks a lot. Like what Saeed Sayyadipour did, I think the flickering is machine specific. I don't have another machine aside from what I'm using right now which is a win 10 and 64bit laptop but Saeed Sayyadipour reproduce the problem and the flickering is not present.
    – lelouch
    May 26, 2020 at 6:52

1 Answer 1

1

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.

4
  • Thank you so much for the explanation @Saeed Sayyadipour. I want to know if you reproduce the problem? did you try the above code in your machine?
    – lelouch
    May 25, 2020 at 9:45
  • Yes, I copied your code into a new form and tested it completely. All works fine and the flickering problem does not happen for me. Could you test it on another system? For example a different machine with different windows version. I tested it on my laptop with a 64-bit version of Windows 10. In general, some problems may resolve in the release version of an application. For another try, you can check whether it happens in the release version of your application. May 25, 2020 at 19:59
  • Thanks, I guess this is a machine-specific problem. I better stick with resizable window to avoid this flickering problem and try to skin the form for better customization.
    – lelouch
    May 26, 2020 at 6:54
  • Good. Finally, if you found this answer useful, please accept and vote it dear @lelouch. Thanks! :) May 26, 2020 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.