Results 1 to 8 of 8

Thread: [RESOLVED] Changing a Form's BorderStyle at Runtime

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Resolved [RESOLVED] Changing a Form's BorderStyle at Runtime

    Can it be done?

    At startup of app it is borderless. When I click on Command1 I have this

    Form1.BorderStyle = 2 ' Make Sizable

    but it doesn't change from borderless to sizable.

  2. #2

  3. #3
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Changing a Form's BorderStyle at Runtime

    There is another easier way. I got this idea from someone in VBF and modified it to make it 'near-perfect'.
    Only drawback is that, the Borderless form will be shown in the TaskBar.
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.  
    5.     ' At design-time set the form as Sizable
    6.     ' We'll make this form Borderless at runtime.
    7.     ' Otherwise, the Close (X) button will not and the form will not be shown at taskbar
    8.     ' when we change the border back to Sizable at runtime.
    9.    
    10.     Me.BorderStyle = 0 ' none
    11.     Me.Caption = Me.Caption ' Forces VB to redraw titlebar, system menu and window border.
    12.    
    13. End Sub
    14.  
    15. Private Sub Command1_Click()
    16.     Me.BorderStyle = 2 'sizable
    17.     Me.Caption = Me.Caption ' Forces VB to redraw titlebar, system menu and window border.
    18. End Sub
    Last edited by iPrank; Jun 6th, 2008 at 08:27 PM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Changing a Form's BorderStyle at Runtime

    This is what I use,

    Code:
    Option Explicit
    
    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
    
    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
    
    Private Const GWL_STYLE As Long = (-16&)
    Private Const WS_THICKFRAME As Long = &H40000
    Private Const WS_MINIMIZEBOX As Long = &H20000
    Private Const WS_MAXIMIZEBOX As Long = &H10000
    Private Const SWP_FRAMECHANGED = &H20
    Private Const SWP_NOZORDER = &H4
    Private Const SWP_NOMOVE = 2
    Private Const SWP_NOSIZE = 1
    
    
    Private Sub Command1_Click()
        ' // Toggle Forms Borderstyle //
        Call SetWindowLong(Me.hwnd, GWL_STYLE, GetWindowLong(Me.hwnd, GWL_STYLE) Xor _
         (WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX))
        
        Call SetWindowPos(Me.hwnd, 0&, 0&, 0&, 0&, 0&, SWP_NOMOVE Or _
         SWP_NOSIZE Or SWP_NOZORDER Or SWP_FRAMECHANGED)
    End Sub

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Changing a Form's BorderStyle at Runtime

    So how to change it to swap between a borderless and a sizable toolwindow?

  6. #6

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Changing a Form's BorderStyle at Runtime

    Yes I did and as a matter of fact I used pieces from it for my situation. Thanks alot for that link, by the way.

    I was just wondering about Edgemeal's method just out of currosity.

  8. #8

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