Results 1 to 15 of 15

Thread: Changing a forms border at runtime?

Hybrid View

  1. #1

  2. #2
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918
    Check out the attached file. It should do what you want.

    Cheers
    Attached Files Attached Files

  3. #3

  4. #4
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918
    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.
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     If Me.BorderStyle = 0 Then
    4.         Me.BorderStyle = vbSizable
    5.     Else
    6.         Me.BorderStyle = vbBSNone
    7.     End If
    8.    
    9.     Me.Caption = Me.Caption
    10.    
    11. End Sub
    The trick to this working is to reference the form's Caption property which forces the form to redraw.

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Excellent tip!! Thanks for the input.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Originally posted by pnish
    Check out the attached file. It should do what you want.

    Cheers
    Just out of interest, did you write this code?
    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

  7. #7
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918
    Just out of interest, did you write this code?
    Not guilty I'm afraid, and I can't remember where I got it, maybe the VB-Helper web site.

    This is the only code I can own up to:
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     If Me.BorderStyle = vbBSNone Then
    4.         Me.BorderStyle = vbSizable
    5.     Else
    6.         Me.BorderStyle = vbBSNone
    7.     End If
    8.    
    9.     Me.Caption = Me.Caption
    10.    
    11. End Sub
    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.

    Good luck

  8. #8

  9. #9
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    To change the border style at runtime use:

    VB Code:
    1. Option Explicit
    2.  
    3. Public Enum enSetWindowPos
    4.     SWP_FRAMECHANGED = &H20 ' The frame changed: send WM_NCCALCSIZE
    5.     SWP_HIDEWINDOW = &H80
    6.     SWP_NOACTIVATE = &H10
    7.     SWP_NOCOPYBITS = &H100
    8.     SWP_NOMOVE = &H2
    9.     SWP_NOOWNERZORDER = &H200 ' Don't do owner Z ordering
    10.     SWP_NOREDRAW = &H8
    11.     SWP_NOSIZE = &H1
    12.     SWP_NOZORDER = &H4
    13.     SWP_SHOWWINDOW = &H40
    14. End Enum
    15.  
    16. 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
    17. '\ Get Window Long Indexes...
    18. Public Enum enGetWindowLong
    19.     GWL_EXSTYLE = (-20)
    20.     GWL_HINSTANCE = (-6)
    21.     GWL_HWNDPARENT = (-8)
    22.     GWL_ID = (-12)
    23.     GWL_STYLE = (-16)
    24.     GWL_USERDATA = (-21)
    25.     GWL_WNDPROC = (-4)
    26. End Enum
    27.  
    28. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    29. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    30.  
    31. '\ Window Style
    32. Public Enum enWindowStyles
    33.     WS_BORDER = &H800000
    34.     WS_CAPTION = &HC00000
    35.     WS_CHILD = &H40000000
    36.     WS_CLIPCHILDREN = &H2000000
    37.     WS_CLIPSIBLINGS = &H4000000
    38.     WS_DISABLED = &H8000000
    39.     WS_DLGFRAME = &H400000
    40.     WS_EX_ACCEPTFILES = &H10&
    41.     WS_EX_DLGMODALFRAME = &H1&
    42.     WS_EX_NOPARENTNOTIFY = &H4&
    43.     WS_EX_TOPMOST = &H8&
    44.     WS_EX_TRANSPARENT = &H20&
    45.     WS_EX_TOOLWINDOW = &H80&
    46.     WS_GROUP = &H20000
    47.     WS_HSCROLL = &H100000
    48.     WS_MAXIMIZE = &H1000000
    49.     WS_MAXIMIZEBOX = &H10000
    50.     WS_MINIMIZE = &H20000000
    51.     WS_MINIMIZEBOX = &H20000
    52.     WS_OVERLAPPED = &H0&
    53.     WS_POPUP = &H80000000
    54.     WS_SYSMENU = &H80000
    55.     WS_TABSTOP = &H10000
    56.     WS_THICKFRAME = &H40000
    57.     WS_VISIBLE = &H10000000
    58.     WS_VSCROLL = &H200000
    59.     '\ New from 95/NT4 onwards
    60.     WS_EX_MDICHILD = &H40
    61.     WS_EX_WINDOWEDGE = &H100
    62.     WS_EX_CLIENTEDGE = &H200
    63.     WS_EX_CONTEXTHELP = &H400
    64.     WS_EX_RIGHT = &H1000
    65.     WS_EX_LEFT = &H0
    66.     WS_EX_RTLREADING = &H2000
    67.     WS_EX_LTRREADING = &H0
    68.     WS_EX_LEFTSCROLLBAR = &H4000
    69.     WS_EX_RIGHTSCROLLBAR = &H0
    70.     WS_EX_CONTROLPARENT = &H10000
    71.     WS_EX_STATICEDGE = &H20000
    72.     WS_EX_APPWINDOW = &H40000
    73.     WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE)
    74.     WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW Or WS_EX_TOPMOST)
    75. End Enum
    76.  
    77. Dim oldVal As FormBorderStyleConstants

    ...continued...
    Last edited by MerrionComputin; Oct 14th, 2002 at 04:32 AM.
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  10. #10
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    ...continued...

    VB Code:
    1. Public Property Get RuntimeFormBorderStyle() As FormBorderStyleConstants
    2.  
    3. RuntimeFormBorderStyle = oldVal
    4.  
    5. End Property
    6.  
    7. Public Property Let RuntimeFormBorderStyle(ByVal newVal As FormBorderStyleConstants)
    8.  
    9.  
    10. If newVal <> oldVal Then
    11.     Select Case newVal
    12.     Case vbBSNone
    13.         WindowStyle(WS_BORDER) = False
    14.         WindowStyle(WS_CAPTION) = False
    15.         WindowStyle(WS_DLGFRAME) = False
    16.         WindowStyle(WS_EX_DLGMODALFRAME) = False
    17.         WindowStyle(WS_EX_TOOLWINDOW) = False
    18.         WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton
    19.         WindowStyle(WS_MINIMIZEBOX) = Me.MinButton
    20.         WindowStyle(WS_SYSMENU) = Me.ControlBox
    21.         WindowStyle(WS_THICKFRAME) = False
    22.         WindowStyle(WS_VSCROLL) = False
    23.         WindowStyle(WS_EX_WINDOWEDGE) = False
    24.         WindowStyle(WS_EX_CLIENTEDGE) = False
    25.         WindowStyle(WS_EX_CONTROLPARENT) = False
    26.         WindowStyle(WS_EX_STATICEDGE) = False
    27.         WindowStyle(WS_EX_APPWINDOW) = False
    28.        
    29.     Case vbFixedDialog
    30.         WindowStyle(WS_BORDER) = True
    31.         WindowStyle(WS_CAPTION) = True
    32.         WindowStyle(WS_DLGFRAME) = True
    33.         WindowStyle(WS_EX_DLGMODALFRAME) = True
    34.         WindowStyle(WS_EX_TOOLWINDOW) = False
    35.         WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton
    36.         WindowStyle(WS_MINIMIZEBOX) = Me.MinButton
    37.         WindowStyle(WS_SYSMENU) = Me.ControlBox
    38.         WindowStyle(WS_THICKFRAME) = False
    39.         WindowStyle(WS_EX_WINDOWEDGE) = True
    40.         WindowStyle(WS_EX_CLIENTEDGE) = False
    41.         WindowStyle(WS_EX_STATICEDGE) = False
    42.         WindowStyle(WS_EX_APPWINDOW) = Me.ShowInTaskbar
    43.  
    44.        
    45.     Case vbFixedSingle
    46.         WindowStyle(WS_BORDER) = True
    47.         WindowStyle(WS_CAPTION) = True
    48.         WindowStyle(WS_DLGFRAME) = True
    49.         WindowStyle(WS_EX_DLGMODALFRAME) = False
    50.         WindowStyle(WS_EX_TOOLWINDOW) = False
    51.         WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton
    52.         WindowStyle(WS_MINIMIZEBOX) = Me.MinButton
    53.         WindowStyle(WS_SYSMENU) = Me.ControlBox
    54.         WindowStyle(WS_THICKFRAME) = True
    55.         WindowStyle(WS_EX_WINDOWEDGE) = True
    56.         WindowStyle(WS_EX_CLIENTEDGE) = False
    57.         WindowStyle(WS_EX_STATICEDGE) = False
    58.         WindowStyle(WS_EX_APPWINDOW) = Me.ShowInTaskbar
    59.        
    60.     Case vbFixedToolWindow
    61.         WindowStyle(WS_BORDER) = True
    62.         WindowStyle(WS_CAPTION) = True
    63.         WindowStyle(WS_DLGFRAME) = True
    64.         WindowStyle(WS_EX_DLGMODALFRAME) = False
    65.         WindowStyle(WS_EX_TOOLWINDOW) = True
    66.         WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton
    67.         WindowStyle(WS_MINIMIZEBOX) = Me.MinButton
    68.         WindowStyle(WS_SYSMENU) = Me.ControlBox
    69.         WindowStyle(WS_THICKFRAME) = False
    70.         WindowStyle(WS_EX_WINDOWEDGE) = True
    71.         WindowStyle(WS_EX_CLIENTEDGE) = False
    72.         WindowStyle(WS_EX_STATICEDGE) = False
    73.         WindowStyle(WS_EX_APPWINDOW) = Me.ShowInTaskbar
    74.    
    75.     Case vbSizable
    76.         WindowStyle(WS_BORDER) = True
    77.         WindowStyle(WS_CAPTION) = True
    78.         WindowStyle(WS_DLGFRAME) = True
    79.         WindowStyle(WS_EX_DLGMODALFRAME) = False
    80.         WindowStyle(WS_EX_TOOLWINDOW) = False
    81.         WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton
    82.         WindowStyle(WS_MINIMIZEBOX) = Me.MinButton
    83.         WindowStyle(WS_SYSMENU) = Me.ControlBox
    84.         WindowStyle(WS_THICKFRAME) = True
    85.         WindowStyle(WS_EX_WINDOWEDGE) = True
    86.         WindowStyle(WS_EX_CLIENTEDGE) = False
    87.         WindowStyle(WS_EX_APPWINDOW) = Me.ShowInTaskbar
    88.  
    89.        
    90.     Case vbSizableToolWindow
    91.         WindowStyle(WS_BORDER) = True
    92.         WindowStyle(WS_CAPTION) = True
    93.         WindowStyle(WS_DLGFRAME) = True
    94.         WindowStyle(WS_EX_DLGMODALFRAME) = False
    95.         WindowStyle(WS_EX_TOOLWINDOW) = True
    96.         WindowStyle(WS_MAXIMIZEBOX) = Me.MaxButton
    97.         WindowStyle(WS_MINIMIZEBOX) = Me.MinButton
    98.         WindowStyle(WS_SYSMENU) = Me.ControlBox
    99.         WindowStyle(WS_THICKFRAME) = False
    100.         WindowStyle(WS_EX_WINDOWEDGE) = True
    101.         WindowStyle(WS_EX_CLIENTEDGE) = False
    102.         WindowStyle(WS_EX_STATICEDGE) = False
    103.         WindowStyle(WS_EX_APPWINDOW) = Me.ShowInTaskbar
    104.    
    105.     End Select
    106.     '\\ Refresh the window
    107.     Call SetWindowPos(Me.hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE + SWP_FRAMECHANGED)
    108.     Me.Refresh
    109.     oldVal = newVal
    110.    
    111. End If
    112.  
    113. End Property
    114.  
    115.  
    116. Public Property Let WindowStyle(ByVal Index As enWindowStyles, ByVal newVal As Boolean)
    117.  
    118. Dim gwIndex As enGetWindowLong
    119. Dim lCurStyle As Long
    120.  
    121. If Index = WS_EX_ACCEPTFILES Or Index = WS_EX_APPWINDOW _
    122.    Or Index = WS_EX_CLIENTEDGE Or Index = WS_EX_CONTEXTHELP _
    123.    Or Index = WS_EX_CONTROLPARENT Or Index = WS_EX_DLGMODALFRAME _
    124.    Or Index = WS_EX_LEFT Or Index = WS_EX_LEFTSCROLLBAR _
    125.    Or Index = WS_EX_LTRREADING Or Index = WS_EX_MDICHILD _
    126.    Or Index = WS_EX_NOPARENTNOTIFY Or Index = WS_EX_OVERLAPPEDWINDOW _
    127.    Or Index = WS_EX_PALETTEWINDOW Or Index = WS_EX_RIGHT _
    128.    Or Index = WS_EX_RIGHTSCROLLBAR Or Index = WS_EX_RTLREADING _
    129.    Or Index = WS_EX_STATICEDGE Or Index = WS_EX_TOOLWINDOW _
    130.    Or Index = WS_EX_TOPMOST Or Index = WS_EX_TRANSPARENT _
    131.    Or Index = WS_EX_WINDOWEDGE Then
    132.     '\ Extended window style
    133.     gwIndex = GWL_EXSTYLE
    134. Else
    135.     '\ Standard window style
    136.     gwIndex = GWL_STYLE
    137. End If
    138.  
    139. lCurStyle = GetWindowLong(Me.hwnd, gwIndex)
    140. If newVal Then
    141.     lCurStyle = lCurStyle Or Index
    142. Else
    143.     lCurStyle = lCurStyle And (Not Index)
    144. End If
    145. Call SetWindowLong(Me.hwnd, gwIndex, lCurStyle)
    146.  
    147. End Property
    148.  
    149. Public Property Get WindowStyle(ByVal Index As enWindowStyles) As Boolean
    150.  
    151. Dim gwIndex As enGetWindowLong
    152. Dim lCurStyle As Long
    153.  
    154. If Index = WS_EX_ACCEPTFILES Or Index = WS_EX_APPWINDOW _
    155.    Or Index = WS_EX_CLIENTEDGE Or Index = WS_EX_CONTEXTHELP _
    156.    Or Index = WS_EX_CONTROLPARENT Or Index = WS_EX_DLGMODALFRAME _
    157.    Or Index = WS_EX_LEFT Or Index = WS_EX_LEFTSCROLLBAR _
    158.    Or Index = WS_EX_LTRREADING Or Index = WS_EX_MDICHILD _
    159.    Or Index = WS_EX_NOPARENTNOTIFY Or Index = WS_EX_OVERLAPPEDWINDOW _
    160.    Or Index = WS_EX_PALETTEWINDOW Or Index = WS_EX_RIGHT _
    161.    Or Index = WS_EX_RIGHTSCROLLBAR Or Index = WS_EX_RTLREADING _
    162.    Or Index = WS_EX_STATICEDGE Or Index = WS_EX_TOOLWINDOW _
    163.    Or Index = WS_EX_TOPMOST Or Index = WS_EX_TRANSPARENT _
    164.    Or Index = WS_EX_WINDOWEDGE Then
    165.     '\\ Extended window style
    166.     gwIndex = GWL_EXSTYLE
    167. Else
    168.     '\\ Standard window style
    169.     gwIndex = GWL_STYLE
    170. End If
    171.  
    172. lCurStyle = GetWindowLong(Me.hwnd, gwIndex)
    173.  
    174. WindowStyle = (lCurStyle Or Index) = lCurStyle
    175.  
    176. End Property
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  11. #11

  12. #12
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Good catch..I hadn't initialised oldVal, so it thought the form was already borderless and didn't do anything. Try:

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. oldVal = Me.BorderStyle
    4. Me.RuntimeFormBorderStyle = vbBSNone
    5.  
    6. End Sub
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  13. #13

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Almost works...
    VB Code:
    1. Private Sub Form_Load()
    2.     oldVal = Me.BorderStyle
    3.     RuntimeFormBorderStyle = vbBSNone
    4.     Me.Caption = "WOOF"
    5. End Sub
    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

    Getting there

    Cheers for your help...

    Woka

  14. #14
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    when you set the caption, the caption bar appears
    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:

    VB Code:
    1. Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    2.  
    3. Private Sub Form_Load()
    4.     oldVal = Me.BorderStyle
    5.     RuntimeFormBorderStyle = vbBSNone
    6.     Call SetWindowText(Me.hwnd, "WOOF")
    7.     Debug.Print Me.Caption
    8.    
    9. End Sub
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  15. #15

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Excellent...cheers

    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width