Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / Visual Basic

gTitleBar - Custom TitleBar for Borderless Forms (VB.NET)

Rate me:
Please Sign up or sign in to vote.
4.81/5 (31 votes)
4 Oct 2011CPOL5 min read 89.2K   5.4K   49  
Simulates standard titlebar on a borderless form for easy placement of additional controls on the titlebar, freeing up space on the form. It can also be put on a panel to simulate an MDI Form.
Public Class gTitleBarMDI
    Inherits Form

#Region "Borderless Form Helper"

    Private Const HTLEFT As Integer = 10
    Private Const HTRIGHT As Integer = 11
    Private Const HTTOP As Integer = 12
    Private Const HTTOPLEFT As Integer = 13
    Private Const HTTOPRIGHT As Integer = 14
    Private Const HTBOTTOM As Integer = 15
    Private Const HTBOTTOMLEFT As Integer = 16
    Private Const HTBOTTOMRIGHT As Integer = 17
    Private Const WM_NCHITTEST As Integer = &H84

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_NCHITTEST Then
            Dim pt As New Point(m.LParam.ToInt32)
            pt = PointToClient(pt)
            If pt.X < GTitleBar1.FrameWidth AndAlso _
                pt.Y < GTitleBar1.FrameWidth Then
                m.Result = New IntPtr(HTTOPLEFT)
            ElseIf pt.X > (Width - GTitleBar1.FrameWidth) AndAlso _
                pt.Y < GTitleBar1.FrameWidth Then
                m.Result = New IntPtr(HTTOPRIGHT)
            ElseIf pt.Y < GTitleBar1.FrameWidth Then
                m.Result = New IntPtr(HTTOP)
            ElseIf pt.X < GTitleBar1.FrameWidth AndAlso _
                pt.Y > (Height - GTitleBar1.FrameWidth - _
                GTitleBar1.FrameHeightAdj) Then
                m.Result = New IntPtr(HTBOTTOMLEFT)
            ElseIf pt.X > (Width - GTitleBar1.FrameWidth) AndAlso _
                pt.Y > (Height - GTitleBar1.FrameWidth - _
                GTitleBar1.FrameHeightAdj) Then
                m.Result = New IntPtr(HTBOTTOMRIGHT)
            ElseIf pt.Y > (Height - GTitleBar1.FrameWidth - _
                GTitleBar1.FrameHeightAdj) Then
                m.Result = New IntPtr(HTBOTTOM)
            ElseIf pt.X < GTitleBar1.FrameWidth Then
                m.Result = New IntPtr(HTLEFT)
            ElseIf pt.X > (Width - GTitleBar1.FrameWidth) Then
                m.Result = New IntPtr(HTRIGHT)
            Else
                MyBase.WndProc(m)
            End If
        Else
            MyBase.WndProc(m)
        End If
    End Sub

    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs)
        GTitleBar1.IsFormActive = True
        GTitleBar1.BackColor = Parent.BackColor

    End Sub

    Private Sub Form1_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs)
        GTitleBar1.IsFormActive = False
    End Sub

#End Region

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        'Add your custom paint code here
    End Sub
    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        MyBase.OnResize(e)

        Refresh()

    End Sub

    Private Sub Button_Press_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Press.Click
        lblMDI.Text = "The Button Was Pressed"
    End Sub

    Private Sub gTitleBarMDI_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GTitleBar1.BackColor = Parent.BackColor
    End Sub

    Private Sub gTitleBarMDI_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Parent.Refresh()
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I first got hooked on programing with the TI994A. After it finally lost all support I reluctantly moved to the Apple IIe. Thank You BeagleBros for getting me through. I wrote programs for my Scuba buisness during this time. Currently I am a Database manager and software developer. I started with VBA and VB6 and now having fun with VB.NET/WPF/C#...

Comments and Discussions