Custom Title Bar

This code and description provide a way of creating a custom window title bar, just in case you need to get rid of the normal title bar of any form. It will even provide means to move the form using the mouse like you normally would using the standard title bar of the form.

Ok, open a new vb project. Remove the form's caption and controls boxes.

Add a label to the form and set a caption for it. Make the label appear flat and give it a border. Put the label where you want the title bar to be.

Now go to the code window for the form and add the following:

Option Explicit

' API functions
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long

' Constants for above API calls
Private Const HTCAPTION = 2

Private Sub Form_Load()
  Dim retVal As Long
  retVal = SetWindowText(Me.hwnd, Label1)
End Sub

Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
End Sub

This will put a caption in the task bar (ensure that the property for the form to display in taskbar is set to true) and will allow you to move the form around when the mouse is down on the label. You can add extra labels (or buttons) and code for minimize and maximize.



You might also like...




Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.” - Jon Ribbens