Changing a forms border at runtime?
Resizable to None
Anyone?
Printable View
Changing a forms border at runtime?
Resizable to None
Anyone?
Check out the attached file. It should do what you want.
Cheers
Spot on :D
Cheers
Woka
Just figured out a way to do this without using the API. It seems that the reason changing BorderStyle at runtime doesn't work is because we need the form to redraw. Try this bit of code, it toggles between a normal border and no border.
The trick to this working is to reference the form's Caption property which forces the form to redraw. :cool:VB Code:
Private Sub Command1_Click() If Me.BorderStyle = 0 Then Me.BorderStyle = vbSizable Else Me.BorderStyle = vbBSNone End If Me.Caption = Me.Caption End Sub
Excellent tip!! Thanks for the input.
Just out of interest, did you write this code?Quote:
Originally posted by pnish
Check out the attached file. It should do what you want.
Cheers
the reason i am asking is that when I set the form so it is not resizable, and has no caption, ie No captionbar and no border at all, then when I do Me.Caption = "Woof", the caption bar appears again, which I don't want...BUT if I just have a normal form, that at design time has been set to have no border and no caption, when I do Me.Caption = "Woof", nothing happens???!
Do I have to add code into my app to check for the style of the form at runtime so that I don't change anyof the props acidentally, ie If Caption = "Woof", I don't want the caption bar to appear so when I go to change the caption I would have to write some code to check if it has a caption bar, and if it hasn't then don't set the caption...
Woka
Not guilty I'm afraid, and I can't remember where I got it, maybe the VB-Helper web site.Quote:
Just out of interest, did you write this code?
This is the only code I can own up to:I don't really know the answer to your question, but it sounds like it would be a good idea to check the style of the form before you change any properties that might do weird things.VB Code:
Private Sub Command1_Click() If Me.BorderStyle = vbBSNone Then Me.BorderStyle = vbSizable Else Me.BorderStyle = vbBSNone End If Me.Caption = Me.Caption End Sub
Good luck
Yea, it definately looks that way :(
Oh well...*sigh*
Cheers,
Woka
To change the border style at runtime use:
VB Code:
Option Explicit Public Enum enSetWindowPos SWP_FRAMECHANGED = &H20 ' The frame changed: send WM_NCCALCSIZE SWP_HIDEWINDOW = &H80 SWP_NOACTIVATE = &H10 SWP_NOCOPYBITS = &H100 SWP_NOMOVE = &H2 SWP_NOOWNERZORDER = &H200 ' Don't do owner Z ordering SWP_NOREDRAW = &H8 SWP_NOSIZE = &H1 SWP_NOZORDER = &H4 SWP_SHOWWINDOW = &H40 End Enum 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 Window Long Indexes... Public Enum enGetWindowLong GWL_EXSTYLE = (-20) GWL_HINSTANCE = (-6) GWL_HWNDPARENT = (-8) GWL_ID = (-12) GWL_STYLE = (-16) GWL_USERDATA = (-21) GWL_WNDPROC = (-4) End Enum Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long '\ Window Style Public Enum enWindowStyles WS_BORDER = &H800000 WS_CAPTION = &HC00000 WS_CHILD = &H40000000 WS_CLIPCHILDREN = &H2000000 WS_CLIPSIBLINGS = &H4000000 WS_DISABLED = &H8000000 WS_DLGFRAME = &H400000 WS_EX_ACCEPTFILES = &H10& WS_EX_DLGMODALFRAME = &H1& WS_EX_NOPARENTNOTIFY = &H4& WS_EX_TOPMOST = &H8& WS_EX_TRANSPARENT = &H20& WS_EX_TOOLWINDOW = &H80& WS_GROUP = &H20000 WS_HSCROLL = &H100000 WS_MAXIMIZE = &H1000000 WS_MAXIMIZEBOX = &H10000 WS_MINIMIZE = &H20000000 WS_MINIMIZEBOX = &H20000 WS_OVERLAPPED = &H0& WS_POPUP = &H80000000 WS_SYSMENU = &H80000 WS_TABSTOP = &H10000 WS_THICKFRAME = &H40000 WS_VISIBLE = &H10000000 WS_VSCROLL = &H200000 '\ New from 95/NT4 onwards WS_EX_MDICHILD = &H40 WS_EX_WINDOWEDGE = &H100 WS_EX_CLIENTEDGE = &H200 WS_EX_CONTEXTHELP = &H400 WS_EX_RIGHT = &H1000 WS_EX_LEFT = &H0 WS_EX_RTLREADING = &H2000 WS_EX_LTRREADING = &H0 WS_EX_LEFTSCROLLBAR = &H4000 WS_EX_RIGHTSCROLLBAR = &H0 WS_EX_CONTROLPARENT = &H10000 WS_EX_STATICEDGE = &H20000 WS_EX_APPWINDOW = &H40000 WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE) WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW Or WS_EX_TOPMOST) End Enum Dim oldVal As FormBorderStyleConstants
...continued...
...continued...
VB Code:
Public Property Get RuntimeFormBorderStyle() As FormBorderStyleConstants RuntimeFormBorderStyle = oldVal End Property Public Property Let RuntimeFormBorderStyle(ByVal newVal As FormBorderStyleConstants) If newVal <> oldVal Then Select Case newVal Case vbBSNone WindowStyle(WS_BORDER) = False WindowStyle(WS_CAPTION) = False WindowStyle(WS_DLGFRAME) = False WindowStyle(WS_EX_DLGMODALFRAME) = False WindowStyle(WS_EX_TOOLWINDOW) = False WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton WindowStyle(WS_MINIMIZEBOX) = Me.MinButton WindowStyle(WS_SYSMENU) = Me.ControlBox WindowStyle(WS_THICKFRAME) = False WindowStyle(WS_VSCROLL) = False WindowStyle(WS_EX_WINDOWEDGE) = False WindowStyle(WS_EX_CLIENTEDGE) = False WindowStyle(WS_EX_CONTROLPARENT) = False WindowStyle(WS_EX_STATICEDGE) = False WindowStyle(WS_EX_APPWINDOW) = False Case vbFixedDialog WindowStyle(WS_BORDER) = True WindowStyle(WS_CAPTION) = True WindowStyle(WS_DLGFRAME) = True WindowStyle(WS_EX_DLGMODALFRAME) = True WindowStyle(WS_EX_TOOLWINDOW) = False WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton WindowStyle(WS_MINIMIZEBOX) = Me.MinButton WindowStyle(WS_SYSMENU) = Me.ControlBox WindowStyle(WS_THICKFRAME) = False WindowStyle(WS_EX_WINDOWEDGE) = True WindowStyle(WS_EX_CLIENTEDGE) = False WindowStyle(WS_EX_STATICEDGE) = False WindowStyle(WS_EX_APPWINDOW) = Me.ShowInTaskbar Case vbFixedSingle WindowStyle(WS_BORDER) = True WindowStyle(WS_CAPTION) = True WindowStyle(WS_DLGFRAME) = True WindowStyle(WS_EX_DLGMODALFRAME) = False WindowStyle(WS_EX_TOOLWINDOW) = False WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton WindowStyle(WS_MINIMIZEBOX) = Me.MinButton WindowStyle(WS_SYSMENU) = Me.ControlBox WindowStyle(WS_THICKFRAME) = True WindowStyle(WS_EX_WINDOWEDGE) = True WindowStyle(WS_EX_CLIENTEDGE) = False WindowStyle(WS_EX_STATICEDGE) = False WindowStyle(WS_EX_APPWINDOW) = Me.ShowInTaskbar Case vbFixedToolWindow WindowStyle(WS_BORDER) = True WindowStyle(WS_CAPTION) = True WindowStyle(WS_DLGFRAME) = True WindowStyle(WS_EX_DLGMODALFRAME) = False WindowStyle(WS_EX_TOOLWINDOW) = True WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton WindowStyle(WS_MINIMIZEBOX) = Me.MinButton WindowStyle(WS_SYSMENU) = Me.ControlBox WindowStyle(WS_THICKFRAME) = False WindowStyle(WS_EX_WINDOWEDGE) = True WindowStyle(WS_EX_CLIENTEDGE) = False WindowStyle(WS_EX_STATICEDGE) = False WindowStyle(WS_EX_APPWINDOW) = Me.ShowInTaskbar Case vbSizable WindowStyle(WS_BORDER) = True WindowStyle(WS_CAPTION) = True WindowStyle(WS_DLGFRAME) = True WindowStyle(WS_EX_DLGMODALFRAME) = False WindowStyle(WS_EX_TOOLWINDOW) = False WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton WindowStyle(WS_MINIMIZEBOX) = Me.MinButton WindowStyle(WS_SYSMENU) = Me.ControlBox WindowStyle(WS_THICKFRAME) = True WindowStyle(WS_EX_WINDOWEDGE) = True WindowStyle(WS_EX_CLIENTEDGE) = False WindowStyle(WS_EX_APPWINDOW) = Me.ShowInTaskbar Case vbSizableToolWindow WindowStyle(WS_BORDER) = True WindowStyle(WS_CAPTION) = True WindowStyle(WS_DLGFRAME) = True WindowStyle(WS_EX_DLGMODALFRAME) = False WindowStyle(WS_EX_TOOLWINDOW) = True WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton WindowStyle(WS_MINIMIZEBOX) = Me.MinButton WindowStyle(WS_SYSMENU) = Me.ControlBox WindowStyle(WS_THICKFRAME) = False WindowStyle(WS_EX_WINDOWEDGE) = True WindowStyle(WS_EX_CLIENTEDGE) = False WindowStyle(WS_EX_STATICEDGE) = False WindowStyle(WS_EX_APPWINDOW) = Me.ShowInTaskbar End Select '\\ Refresh the window Call SetWindowPos(Me.hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE + SWP_FRAMECHANGED) Me.Refresh oldVal = newVal End If End Property Public Property Let WindowStyle(ByVal Index As enWindowStyles, ByVal newVal As Boolean) Dim gwIndex As enGetWindowLong Dim lCurStyle As Long If Index = WS_EX_ACCEPTFILES Or Index = WS_EX_APPWINDOW _ Or Index = WS_EX_CLIENTEDGE Or Index = WS_EX_CONTEXTHELP _ Or Index = WS_EX_CONTROLPARENT Or Index = WS_EX_DLGMODALFRAME _ Or Index = WS_EX_LEFT Or Index = WS_EX_LEFTSCROLLBAR _ Or Index = WS_EX_LTRREADING Or Index = WS_EX_MDICHILD _ Or Index = WS_EX_NOPARENTNOTIFY Or Index = WS_EX_OVERLAPPEDWINDOW _ Or Index = WS_EX_PALETTEWINDOW Or Index = WS_EX_RIGHT _ Or Index = WS_EX_RIGHTSCROLLBAR Or Index = WS_EX_RTLREADING _ Or Index = WS_EX_STATICEDGE Or Index = WS_EX_TOOLWINDOW _ Or Index = WS_EX_TOPMOST Or Index = WS_EX_TRANSPARENT _ Or Index = WS_EX_WINDOWEDGE Then '\ Extended window style gwIndex = GWL_EXSTYLE Else '\ Standard window style gwIndex = GWL_STYLE End If lCurStyle = GetWindowLong(Me.hwnd, gwIndex) If newVal Then lCurStyle = lCurStyle Or Index Else lCurStyle = lCurStyle And (Not Index) End If Call SetWindowLong(Me.hwnd, gwIndex, lCurStyle) End Property Public Property Get WindowStyle(ByVal Index As enWindowStyles) As Boolean Dim gwIndex As enGetWindowLong Dim lCurStyle As Long If Index = WS_EX_ACCEPTFILES Or Index = WS_EX_APPWINDOW _ Or Index = WS_EX_CLIENTEDGE Or Index = WS_EX_CONTEXTHELP _ Or Index = WS_EX_CONTROLPARENT Or Index = WS_EX_DLGMODALFRAME _ Or Index = WS_EX_LEFT Or Index = WS_EX_LEFTSCROLLBAR _ Or Index = WS_EX_LTRREADING Or Index = WS_EX_MDICHILD _ Or Index = WS_EX_NOPARENTNOTIFY Or Index = WS_EX_OVERLAPPEDWINDOW _ Or Index = WS_EX_PALETTEWINDOW Or Index = WS_EX_RIGHT _ Or Index = WS_EX_RIGHTSCROLLBAR Or Index = WS_EX_RTLREADING _ Or Index = WS_EX_STATICEDGE Or Index = WS_EX_TOOLWINDOW _ Or Index = WS_EX_TOPMOST Or Index = WS_EX_TRANSPARENT _ Or Index = WS_EX_WINDOWEDGE Then '\\ Extended window style gwIndex = GWL_EXSTYLE Else '\\ Standard window style gwIndex = GWL_STYLE End If lCurStyle = GetWindowLong(Me.hwnd, gwIndex) WindowStyle = (lCurStyle Or Index) = lCurStyle End Property
Cheers, but I can't seem to get it to work :(
I am using:
Any ideas?VB Code:
Private Sub Form_Load() RuntimeFormBorderStyle = vbBSNone End Sub
Woka
Good catch..I hadn't initialised oldVal, so it thought the form was already borderless and didn't do anything. Try:
VB Code:
Private Sub Form_Load() oldVal = Me.BorderStyle Me.RuntimeFormBorderStyle = vbBSNone End Sub
Almost works...
Like the code posted at the top of the thread, when you set the caption, the caption bar appears :( If you set the borderstyle at design time to = None, then this doesn't happen. Also, setting the style to vbBSNone, still leaves the forms with a border, just no caption box :(VB Code:
Private Sub Form_Load() oldVal = Me.BorderStyle RuntimeFormBorderStyle = vbBSNone Me.Caption = "WOOF" End Sub
Getting there :D
Cheers for your help...
Woka
Whenever you change the caption, VB refreshes the form with it's own internal version of the window style. Therefore you need to replace calls to the Caption propertyu with SetWindowText API call:Quote:
when you set the caption, the caption bar appears
VB Code:
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long Private Sub Form_Load() oldVal = Me.BorderStyle RuntimeFormBorderStyle = vbBSNone Call SetWindowText(Me.hwnd, "WOOF") Debug.Print Me.Caption End Sub
Excellent...cheers :D
Ok, when you set the border = vbBSNone, should it remove the border from the form??? It just removed the caption bar, so I am left with a 3d looking form...not a completely flat borderless one :(
Woka
PS My API is getting there slowly...My work/company, doesn't give us time to develop UI, which use API...all our development time is concentrated into the DCOM side of applications, which is a shame as I prefer UI coding...Boooooooo!