by Stephen Kent (10 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)
This code allows a user to Toggle the caption bar (toolbox and all) on a form at runtime. Normally you cannot visually change some of the form's settings at runtime this solves one of those instances.
Code can be fairly easily changed so as to accept a parameter for if you want the caption or not. This is just a simple example.
Assumes
This simply changes the window style of the form and then does a couple of extremely quick re-sizes so that the caption area is forced to redraw. This will only work as is if the code is placed in the form you want to affect.
Sub must be called for changes to take effect.
API DeclarationsPrivate Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
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 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 Const GWL_STYLE = (-16)
Private Const SWP_NOMOVE = &H2
Private Const WS_CAPTION = &HC00000
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub ToggleFormCaption()
Dim rcWindow As RECT
Dim lRet As Long
lRet = GetWindowRect(Me.hwnd, rcWindow)
lRet = SetWindowLong(Me.hwnd, GWL_STYLE, GetWindowLong(Me.hwnd, GWL_STYLE) Xor WS_CAPTION)
lRet = SetWindowPos(Me.hwnd, 0, 0, 0, rcWindow.Right - rcWindow.Left + 1, rcWindow.Bottom - rcWindow.Top, SWP_NOMOVE)
lRet = SetWindowPos(Me.hwnd, 0, 0, 0, rcWindow.Right - rcWindow.Left, rcWindow.Bottom - rcWindow.Top, SWP_NOMOVE)
End Sub