Important alert: (current site time 7/30/2014 6:50:30 AM EDT)
 

VB icon

Change Form Styles at Runtime

Email
Submitted on: 12/24/2001 8:02:21 AM
By: Stephen Kent  
Level: Beginner
User Rating: By 6 Users
Compatibility: VB 6.0
Views: 11529
(About the author)
 
     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.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Change Form Styles at Runtime
'**************************************
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
'Get/Set WindowLong Constants (only those used)
Private Const GWL_STYLE = (-16)
Private Const GWL_EXSTYLE = (-20)
'SetWindowPos Constants (only those used)
Private Const SWP_FRAMECHANGED = &H20 'The frame changed: send WM_NCCALCSIZE
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
'Dialog Styles (also present in the GWL_STYLE area)
Private Const DS_ABSALIGN As Long = &H1
Private Const DS_SYSMODAL As Long = &H2
Private Const DS_3DLOOKAs Long = &H4
Private Const DS_FIXEDSYS As Long = &H8
Private Const DS_NOFAILCREATEAs Long = &H10
Private Const DS_LOCALEDIT As Long = &H20 'Edit items get Local storage.
Private Const DS_SETFONT As Long = &H40 'User specified font for Dlg controls
Private Const DS_MODALFRAME As Long = &H80 'Can be combined with WS_CAPTION
Private Const DS_NOIDLEMSG As Long = &H100 'WM_ENTERIDLE message will not be sent
Private Const DS_SETFOREGROUNDAs Long = &H200 'not in win3.1
Private Const DS_CONTROL As Long = &H400
Private Const DS_CENTERAs Long = &H800
Private Const DS_CENTERMOUSEAs Long = &H1000
Private Const DS_CONTEXTHELPAs Long = &H2000
'Window Styles (GWL_STYLE area)
Private Const WS_OVERLAPPED As Long = &H0
Private Const WS_POPUPAs Long = &H80000000
Private Const WS_CHILDAs Long = &H40000000
Private Const WS_MINIMIZE As Long = &H20000000
Private Const WS_VISIBLE As Long = &H10000000
Private Const WS_DISABLED As Long = &H8000000
Private Const WS_CLIPSIBLINGSAs Long = &H4000000
Private Const WS_CLIPCHILDRENAs Long = &H2000000
Private Const WS_MAXIMIZE As Long = &H1000000
Private Const WS_CAPTION As Long = &HC00000 'WS_BORDER | WS_DLGFRAME
Private Const WS_BORDERAs Long = &H800000
Private Const WS_DLGFRAME As Long = &H400000
Private Const WS_VSCROLL As Long = &H200000
Private Const WS_HSCROLL As Long = &H100000
Private Const WS_SYSMENU As Long = &H80000
Private Const WS_THICKFRAME As Long = &H40000
Private Const WS_GROUPAs Long = &H20000
Private Const WS_TABSTOP As Long = &H10000
Private Const WS_MINIMIZEBOXAs Long = &H20000
Private Const WS_MAXIMIZEBOXAs Long = &H10000
Private Const WS_TILEDAs Long = WS_OVERLAPPED
Private Const WS_ICONICAs Long = WS_MINIMIZE
Private Const WS_SIZEBOX As Long = WS_THICKFRAME
'Extended Window Styles (GWL_EXSTYLE area)
Private Const WS_EX_DLGMODALFRAME As Long = &H1
Private Const WS_EX_NOPARENTNOTIFY As Long = &H4
Private Const WS_EX_TOPMOST As Long = &H8
Private Const WS_EX_ACCEPTFILESAs Long = &H10
Private Const WS_EX_TRANSPARENTAs Long = &H20
Private Const WS_EX_MDICHILDAs Long = &H40
Private Const WS_EX_TOOLWINDOWAs Long = &H80
Private Const WS_EX_WINDOWEDGEAs Long = &H100
Private Const WS_EX_CLIENTEDGEAs Long = &H200
Private Const WS_EX_CONTEXTHELPAs Long = &H400
Private Const WS_EX_RIGHT As Long = &H1000
Private Const WS_EX_LEFT As Long = &H0
Private Const WS_EX_RTLREADINGAs Long = &H2000
Private Const WS_EX_LTRREADINGAs Long = &H0
Private Const WS_EX_LEFTSCROLLBAR As Long = &H4000
Private Const WS_EX_RIGHTSCROLLBAR As Long = &H0
Private Const WS_EX_CONTROLPARENT As Long = &H10000
Private Const WS_EX_STATICEDGEAs Long = &H20000
Private Const WS_EX_APPWINDOWAs Long = &H40000
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Change Form Styles at Runtime
' Description: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.
' By: Stephen Kent
'
' Assumes:For certain buttons to work such as those in the control box they need to be enabled in design time (even if they are then hidden at runtime) otherwise there will be no handlers linked to those buttons and they will be useless. This applies to the What's This Button, Min Button, Max Button, and the Control Box. (What's this button has same restrictions on it as it does when used normally)
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=30084&lngWId=1'for details.'**************************************

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


Other 10 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/11/2002 3:24:48 PMFred_cpp

:) It Works Excellent! ! :)
(If this comment was disrespectful, please report it.)

 
12/30/2002 7:17:48 AMTechni Rei Myoko

some of your constant declarations need a space before the word as.
other than that, this code works absolutely wonderful
(If this comment was disrespectful, please report it.)

 
2/10/2003 4:09:03 AMKaliana

can you email me the project please ? I need a value of some constants that not in "API Viewer". Or can you write it down here all the constant's value ?
Thanks a lot...
(If this comment was disrespectful, please report it.)

 
8/20/2004 1:03:26 PM

Hello. The code works well, but I have problems with "Fixed toolbar" mode. This mode doesn't work, and changes me the form as fixed form. ¿whats happend?
(If this comment was disrespectful, please report it.)

 
11/20/2004 2:03:47 PMHamed Oveisi

Great! I need it so much :)
(If this comment was disrespectful, please report it.)

 
5/28/2006 7:06:39 AMvic

how to use ?
(If this comment was disrespectful, please report it.)

 
2/20/2013 4:03:44 AMCoolCurrent

you don't need api for this, jus use

Me.BorderStyle = 0 ' none
' Forces VB to redraw titlebar, system menu and window
Me.Caption = Me.Caption
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.