The new vbAccelerator Site - more VB and .NET Code and Controls

Show and Hide a Form's Titlebar at run-time

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,Windows

Updated:

09/08/98

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


This tip shows you how to show and hide the title bar of a window at run-time. To make a window's title bar disappear, you have to remove the control box, the maximise box and the minimise box as well as set the caption of the form to blank. Unfortunately, VB's ControlBox, MinButton and MaxButton properties of a form are read-only so you can normally only do this at design time. However, by manipulating the style of the window using API calls, you can get the same thing to happen at run-time.

Start a new project in VB. Add the following code to the project's form:

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_STYLE = (-16)
&nbsp &nbsp Private Const WS_CAPTION = &HC00000&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
' WS_BORDER Or WS_DLGFRAME
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_SYSMENU = &H80000

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Enum ESetWindowPosStyles
&nbsp &nbsp SWP_SHOWWINDOW = &H40
&nbsp &nbsp SWP_HIDEWINDOW = &H80
&nbsp &nbsp
SWP_FRAMECHANGED = &H20 ' The frame changed: send WM_NCCALCSIZE
&nbsp &nbsp SWP_NOACTIVATE = &H10
&nbsp &nbsp SWP_NOCOPYBITS = &H100
&nbsp &nbsp SWP_NOMOVE = &H2
&nbsp &nbsp
SWP_NOOWNERZORDER = &H200 ' Don't do owner Z ordering
&nbsp &nbsp SWP_NOREDRAW = &H8
&nbsp &nbsp SWP_NOREPOSITION = SWP_NOOWNERZORDER
&nbsp &nbsp SWP_NOSIZE = &H1
&nbsp &nbsp SWP_NOZORDER = &H4
&nbsp &nbsp SWP_DRAWFRAME = SWP_FRAMECHANGED
&nbsp &nbsp HWND_NOTOPMOST = -2
End Enum

Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
&nbsp &nbsp Left As Long
&nbsp &nbsp Top As Long
&nbsp &nbsp Right As Long
&nbsp &nbsp Bottom As Long
End Type

Private Function ShowTitleBar(ByVal bState As Boolean)
Dim lStyle As Long
Dim tR As RECT

&nbsp &nbsp
' Get the window's position:
&nbsp &nbsp GetWindowRect Me.hwnd, tR

&nbsp &nbsp
' Modify whether title bar will be visible:
&nbsp &nbsp lStyle = GetWindowLong(Me.hwnd, GWL_STYLE)
&nbsp &nbsp If (bState) Then
&nbsp &nbsp Me.Caption = Me.Tag
&nbsp &nbsp If Me.ControlBox Then
&nbsp &nbsp &nbsp &nbsp lStyle = lStyle Or WS_SYSMENU
&nbsp &nbsp End If
&nbsp &nbsp If Me.MaxButton Then
&nbsp &nbsp &nbsp &nbsp lStyle = lStyle Or WS_MAXIMIZEBOX
&nbsp &nbsp End If
&nbsp &nbsp If Me.MinButton Then
&nbsp &nbsp &nbsp &nbsp lStyle = lStyle Or WS_MINIMIZEBOX
&nbsp &nbsp End If
&nbsp &nbsp If Me.Caption "" Then
&nbsp &nbsp &nbsp &nbsp lStyle = lStyle Or WS_CAPTION
&nbsp &nbsp End If
&nbsp &nbsp Else
&nbsp &nbsp Me.Tag = Me.Caption
&nbsp &nbsp Me.Caption = ""
&nbsp &nbsp lStyle = lStyle And Not WS_SYSMENU
&nbsp &nbsp lStyle = lStyle And Not WS_MAXIMIZEBOX
&nbsp &nbsp lStyle = lStyle And Not WS_MINIMIZEBOX
&nbsp &nbsp lStyle = lStyle And Not WS_CAPTION
End If
SetWindowLong Me.hwnd, GWL_STYLE, lStyle

' Ensure the style takes and make the window the
' same size, regardless that the title bar etc
' is now a different size:
SetWindowPos Me.hwnd, 0, tR.Left, tR.Top, tR.Right - tR.Left, tR.Bottom - tR.Top, SWP_NOREPOSITION Or SWP_NOZORDER Or SWP_FRAMECHANGED
Me.Refresh

' Ensure that your resize code is fired, as the client area
' has changed:
Form_Resize

End Function

To try out the hiding and showing the title bar, add a CheckBox to the project's form. Set the check box's Value property to 1 (Checked). Then put the following code under the Check box's click event:

Private Sub Check1_Click()
&nbsp &nbsp If (Check1.Value = Checked) Then
&nbsp &nbsp ShowTitleBar True
&nbsp &nbsp Else
&nbsp &nbsp ShowTitleBar False
End If
End Sub

When you click on the check box, the form's titlebar will be alternately hidden and shown.


&nbsp

Related Tips and Articles:

None.

&nbsp
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 09/08/98