1

I try to create a borderless form with this code

Public Partial Class Form1
    Inherits Form

    Public Sub New()
        InitializeComponent()
        Me.FormBorderStyle = FormBorderStyle.None
        Me.DoubleBuffered = True
        Me.SetStyle(ControlStyles.ResizeRedraw, True)
    End Sub

    Private Const cGrip As Integer = 16
    Private Const cCaption As Integer = 32

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim rc As Rectangle = New Rectangle(Me.ClientSize.Width - cGrip, Me.ClientSize.Height - cGrip, cGrip, cGrip)
        ControlPaint.DrawSizeGrip(e.Graphics, Me.BackColor, rc)
        rc = New Rectangle(0, 0, Me.ClientSize.Width, cCaption)
        e.Graphics.FillRectangle(Brushes.DarkBlue, rc)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = &H84 Then
            Dim pos As Point = New Point(m.LParam.ToInt32())
            pos = Me.PointToClient(pos)

            If pos.Y < cCaption Then
                m.Result = CType(2, IntPtr)
                Return
            End If

            If pos.X >= Me.ClientSize.Width - cGrip AndAlso pos.Y >= Me.ClientSize.Height - cGrip Then
                m.Result = CType(17, IntPtr)
                Return
            End If
        End If

        MyBase.WndProc(m)
    End Sub
End Class

the problem is that if I should add a control like Panel and dock it to the top, it stays on top of the Blue rectangle that was painted. I want every docking to start below the painted rectangle

  • You could use a `Panel` as your caption (Dock = Top), instead of just drawing in onto the form. This way the LayoutEngine will work as expected, when you add another `Panel` with Dock = Top. – keco Aug 01 '20 at 09:10
  • the reason I prefer drawing is because I have other projects from which I want to inherit it to. Hence, if I use a panel, (1) the name of the panel may contradict a name in another project (2) it adds a symbol on the panel showing that it is an inherited control – yinkajewole Aug 01 '20 at 09:30
  • 1
    [How to set the client area (ClientRectangle) in a borderless form?](https://stackoverflow.com/a/29788300/7444103) – Jimi Aug 01 '20 at 09:51
  • yes, something like this... but that was written in C# and it also gives error when resizing – yinkajewole Aug 01 '20 at 10:23
  • 2
    Looks familiar, note that attribution is required here. Select the form in the designer and set its Padding property to (0, 32, 0, 0) – Hans Passant Aug 01 '20 at 12:27
  • Exactly what I had always wanted. Thanks. By the way, what you mean by _note that attribution is required here_ – yinkajewole Aug 01 '20 at 16:11

0 Answers0