Change Form Styles at Runtime
This sub-procedure will allow the developer to fairly easily switch between a form's border styles during runtime. Normally this isn't really possible because several of the attributes are read-only at runtime. This code overcomes those limitations. I have only tested this with VB6, but since it is basically just API calls it should be able to work with any version that supports API calls. Thanks to Fred_CPP for the tip on using SWP_FRAMECHANGED instead of resizing the form.
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
Public Sub ChangeFormBorder(frmForm As Form, _
ByVal eNewBorder As FormBorderStyleConstants, _
Optional ByVal bClipControls As Boolean = True, _
Optional ByVal bControlBox As Boolean = True, _
Optional ByVal bMaxButton As Boolean = True, _
Optional ByVal bMinButton As Boolean = True, _
Optional ByVal bShowInTaskBar As Boolean = True, _
Optional ByVal bWhatsThisButton As Boolean = False)
Dim lRet As Long
Dim lStyleFlags As Long
Dim lStyleExFlags As Long
'Initialize our flags
lStyleFlags = 0
lStyleExFlags = 0
'If we want ClipControls then add that flag and change the form property
If bClipControls Then
lStyleFlags = lStyleFlags Or WS_CLIPCHILDREN
frmForm.ClipControls = True
Else
frmForm.ClipControls = False
End If
'If we want the control box then add the flag (property is read-only)
If bControlBox Then lStyleFlags = lStyleFlags Or WS_SYSMENU
'If we want the max button then add the flag (property is read-only)
If bMaxButton Then lStyleFlags = lStyleFlags Or WS_MAXIMIZEBOX
'If we want the min button then add the flag (property is read-only)
If bMinButton Then lStyleFlags = lStyleFlags Or WS_MINIMIZEBOX
'If we want the form to show in taskbar then add the flag (property is read-only)
If bShowInTaskBar Then lStyleExFlags = lStyleExFlags Or WS_EX_APPWINDOW
'If we want the what's this button then add the flag (property is read-only)
If bWhatsThisButton Then lStyleExFlags = lStyleExFlags Or WS_EX_CONTEXTHELP
'If the form is an MDI Child form then add the flag (Don't want to screw up the form)
If frmForm.MDIChild Then lStyleExFlags = lStyleExFlags Or WS_EX_MDICHILD
'Now we need to set the flags for the border we are changing to
Select Case eNewBorder
Case vbBSNone
lStyleFlags = lStyleFlags Or (WS_VISIBLE Or WS_CLIPSIBLINGS)
'No change to extended style flags.
Case vbFixedSingle
lStyleFlags = lStyleFlags Or (WS_VISIBLE Or WS_CLIPSIBLINGS Or WS_CAPTION)
lStyleExFlags = lStyleExFlags Or WS_EX_WINDOWEDGE
Case vbSizable
lStyleFlags = lStyleFlags Or (WS_VISIBLE Or WS_CLIPSIBLINGS Or WS_CAPTION Or WS_THICKFRAME)
lStyleExFlags = lStyleExFlags Or WS_EX_WINDOWEDGE
Case vbFixedDialog
lStyleFlags = lStyleFlags Or (WS_VISIBLE Or WS_CLIPSIBLINGS Or WS_CAPTION Or DS_MODALFRAME)
lStyleExFlags = lStyleExFlags Or (WS_EX_WINDOWEDGE Or WS_EX_DLGMODALFRAME)
Case vbFixedToolWindow
lStyleFlags = lStyleFlags Or (WS_VISIBLE Or WS_CLIPSIBLINGS Or WS_CAPTION)
lStyleExFlags = lStyleExFlags Or (WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW)
Case vbSizableToolWindow
lStyleFlags = lStyleFlags Or (WS_VISIBLE Or WS_CLIPSIBLINGS Or WS_CAPTION Or WS_THICKFRAME)
lStyleExFlags = lStyleExFlags Or (WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW)
End Select
'WS_VISIBLE makes sure the form is visible
'WS_CLIPSIBLINGS makes sure that when there are other windows with the same relative family that they do not draw over each other.
'WS_CAPTION provides the form's caption
'WS_THICKFRAME makes the form sizable
'DS_MODALFRAME allows dialog forms to have 3d effect
'WS_EX_WINDOWEDGE is for the border around the form
'WS_EX_DLGMODALFRAME says the window has a double border and may or may not have a caption
'WS_EX_TOOLWINDOW says we need a shorter caption and smaller font
'Change our styles
lRet = SetWindowLong(frmForm.hwnd, GWL_STYLE, lStyleFlags)
lRet = SetWindowLong(frmForm.hwnd, GWL_EXSTYLE, lStyleExFlags)
'Signal that the frame has changed
lRet = SetWindowPos(frmForm.hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_FRAMECHANGED)
'Make that we've changed the border in the form's property
frmForm.BorderStyle = eNewBorder
End Sub
Original Comments (3)
Recovered from Wayback Machine