Results 1 to 4 of 4

Thread: Changing forms Borderstyle at Runtime...Heres How!

  1. #1

    Thread Starter
    Frenzied Member Blobby's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    1,512

    Changing forms Borderstyle at Runtime...Heres How!

    Im changing a forms Borderstyle dynamically in code via an option on a form. Altho i set the Form.Bordertyle to either 0 or 1 the form doesnt keep or remove the border. This only happens if i set this state at designtime. How do i force the screen to take the new borderstyle into effect? Thanks
    Last edited by Blobby; Jan 10th, 2003 at 03:59 AM.
    There are 3 types of people in this world.........those that can count, and those that can't.

    Blobby

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    I do believe that you cannot change the border style in runtime...its a Design time only setting. (Unless you want to start messing with the API's and "paint" a new border. I have seen it done with Buttons / Textboxes....just not forms)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Frenzied Member Blobby's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    1,512
    I found out how to do it. As the Borderstyle of a form is a readonly property, you cannot just set it at runtime. You have to use the API to set the 'bits' then redraw the screen using the API gain. Heres how to do it:

    Create a new class and paste the following into it:
    VB Code:
    1. ' Win32 APIs used to toggle border styles.
    2. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
    3.     (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    4.  
    5. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    6.     (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    7.    
    8. Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, _
    9.     ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, _
    10.     ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    11.  
    12. Private Const GWL_STYLE = (-16)
    13. Private Const WS_CAPTION = &HC00000
    14.  
    15. ' Force total pRedraw that shows new styles.
    16. Private Const SWP_FRAMECHANGED = &H20
    17. Private Const SWP_NOMOVE = &H2
    18. Private Const SWP_NOZORDER = &H4
    19. Private Const SWP_NOSIZE = &H1
    20.  
    21. Private mhWnd As Long
    22.  
    23. Public Property Let Titlebar(ByVal Value As Boolean)
    24.    ' Set WS_CAPTION On or Off as requested.
    25.    Call fFlipBit(WS_CAPTION, Value)
    26. End Property
    27.  
    28. Public Property Get Titlebar() As Boolean
    29.    ' Return value of WS_CAPTION bit.
    30.    Titlebar = CBool(fStyle And WS_CAPTION)
    31. End Property
    32.  
    33. Public Property Set Client(ByVal obj As Form)
    34.    ' Store reference to client form.
    35.    Set mClient = obj
    36.    ' Cache hWnd as it'll be accessed frequently.
    37.    If mClient Is Nothing Then
    38.       mhWnd = 0
    39.    Else
    40.       mhWnd = mClient.hWnd
    41.    End If
    42. End Property
    43.  
    44. Public Sub pRedraw()
    45.    ' Redraw window with new style.
    46.    Const swpFlags As Long = _
    47.       SWP_FRAMECHANGED Or SWP_NOMOVE Or _
    48.       SWP_NOZORDER Or SWP_NOSIZE
    49.    Call SetWindowPos(mhWnd, 0, 0, 0, 0, 0, swpFlags)
    50. End Sub
    51.  
    52. Private Function fStyle(Optional ByVal NewBits As Long = 0) As Long
    53.    '
    54.    ' Set new style bits.
    55.    '
    56.    If NewBits Then
    57.       Call SetWindowLong(mhWnd, GWL_STYLE, NewBits)
    58.    End If
    59.    ' Retrieve current style bits.
    60.    fStyle = GetWindowLong(mhWnd, GWL_STYLE)
    61. End Function
    62.  
    63. Private Function fFlipBit(ByVal Bit As Long, ByVal Value As Boolean) As Boolean
    64. Dim lStyle As Long
    65.    
    66.    ' Retrieve current style bits.
    67.    lStyle = GetWindowLong(mhWnd, GWL_STYLE)
    68.    
    69.    ' Set requested bit On or Off and Redraw.
    70.    If Value Then
    71.       lStyle = lStyle Or Bit
    72.    Else
    73.       lStyle = lStyle And Not Bit
    74.    End If
    75.    Call SetWindowLong(mhWnd, GWL_STYLE, lStyle)
    76.    Call pRedraw
    77.    
    78.    ' Return success code.
    79.    fFlipBit = (lStyle = GetWindowLong(mhWnd, GWL_STYLE))
    80. End Function

    Then in your form declaration code where you want to set/unset the border:

    VB Code:
    1. Private mclsStyle As clsStyle

    Then in the Form_Load event

    VB Code:
    1. Set mclsStyle = New clsStyle
    2.     Set mclsStyle.Client = UltiMame

    Then to Actually do the change:

    VB Code:
    1. mclsStyle.Titlebar = Abs(1) 'Sets the form WITH a border

    or

    VB Code:
    1. mclsStyle.Titlebar = Abs(0) 'Sets the form WITHOUT a border

    For more information, visit www.TheScarms.com as its to their credit this code has been modified from. The original code on this site did a lot more but i managed to condense it down to just the essentials for doing the job
    There are 3 types of people in this world.........those that can count, and those that can't.

    Blobby

  4. #4
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Changing the form border style at runtime has been dealt with in this thread...
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

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