how to get the form border size ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: how to get the form border size ?

  1. #1
    Join Date
    Jun 2005
    Posts
    60

    Question how to get the form border size ?

    example : i want to resize a form to 5000 Height (at run-time, not design-time)
    but the real size (ScaleHeight) is 4524 because of the border size.
    the border size is not the same in winXP and win98
    and is not the same for "Fixed Single" and "Sizable"

    i use this :
    Code:
    Do
      DoEvents
      Me.Height = Me.Height + 1
    Loop Until Me.ScaleHeight >= 5000
    is there a function to get the form border size (vb or api) ?
    Last edited by sergelac; June 10th, 2005 at 09:05 PM.

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    Re: how to get the form border size ?

    The dimensions you refer to are in twips. Differences in the sizes on xp could vary from system to system due to themes if you are supporting them. Anyway, if you want to ensure a certain client area, you can use the GetClientRect and GetWindowRect API calls, then SetWindowPos to set the size accordingly. This will be in Pixels, not twips.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Jun 2005
    Posts
    60

    Smile Re: how to get the form border size ?

    thanks, it works

    here is my code :
    Code:
    Option Explicit
    
    Private Type RECT
      Left As Long
      Top As Long
      Right As Long
      Bottom As Long
    End Type
    
    Public Type FormSize
      Width As Long
      Height As Long
      ScaleWidth As Long
      ScaleHeight As Long
      BorderWidth As Long
      BorderHeight As Long
      zError As Boolean
    End Type
    
    Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) 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
    
    Public Function GetFormSize(ByVal hWnd As Long) As FormSize
      Dim TMP As FormSize, rectClient As RECT, rectWindow As RECT
      If (GetClientRect(hWnd, rectClient) <> 0) And (GetWindowRect(hWnd, rectWindow) <> 0) Then
        TMP.Width = rectWindow.Right - rectWindow.Left
        TMP.Height = rectWindow.Bottom - rectWindow.Top
        TMP.ScaleWidth = rectClient.Right - rectClient.Left
        TMP.ScaleHeight = rectClient.Bottom - rectClient.Top
        TMP.BorderWidth = TMP.Width - TMP.ScaleWidth
        TMP.BorderHeight = TMP.Height - TMP.ScaleHeight
        TMP.zError = False
      Else
        TMP.Width = 0
        TMP.Height = 0
        TMP.ScaleWidth = 0
        TMP.ScaleHeight = 0
        TMP.BorderWidth = 0
        TMP.BorderHeight = 0
        TMP.zError = True
      End If
      GetFormSize = TMP
    End Function
    
    Public Function ResizeForm(ByVal hWnd As Long, Optional ByVal zWidth As Long = -1, Optional ByVal zHeight As Long = -1) As Boolean
      Const SWP_NOACTIVATE = &H10
      Const SWP_NOMOVE = &H2
      Const SWP_NOOWNERZORDER = &H200
    '  Const SWP_NOREPOSITION = SWP_NOOWNERZORDER
      Const SWP_NOZORDER = &H4
      Const zFLAGS = SWP_NOACTIVATE Or SWP_NOMOVE Or SWP_NOOWNERZORDER Or SWP_NOZORDER
      Dim TMP As FormSize
      ResizeForm = False
      TMP = GetFormSize(hWnd)
      If TMP.zError = False Then
        If zWidth = -1 Then zWidth = TMP.Width
        If zHeight = -1 Then zHeight = TMP.Height
        If SetWindowPos(hWnd, 0, 0, 0, zWidth + TMP.BorderWidth, zHeight + TMP.BorderHeight, zFLAGS) <> 0 Then ResizeForm = True
      End If
    End Function
    Code:
    Option Explicit
    
    Private Sub Form_Activate()
      Dim zForm As FormSize
      
      Me.Width = 6000
      Me.Height = 3000
      zForm = GetFormSize(Me.hWnd)
      
      Debug.Print "BEFORE :"
      Debug.Print "normal :"; Me.Width; "x"; Me.Height
      Debug.Print "scale  :"; Me.ScaleWidth; "x"; Me.ScaleHeight
      Debug.Print "Window :"; zForm.Width; "x"; zForm.Height
      Debug.Print "client :"; zForm.ScaleWidth; "x"; zForm.ScaleHeight
      Debug.Print vbCrLf & "border :"; zForm.BorderWidth; "x"; zForm.BorderHeight
     
      ResizeForm Me.hWnd
      zForm = GetFormSize(Me.hWnd)
      
      Debug.Print vbCrLf & "AFTER :"
      Debug.Print "normal :"; Me.Width; "x"; Me.Height
      Debug.Print "scale  :"; Me.ScaleWidth; "x"; Me.ScaleHeight
      Debug.Print "Window :"; zForm.Width; "x"; zForm.Height
      Debug.Print "client :"; zForm.ScaleWidth; "x"; zForm.ScaleHeight
    
      End
    End Sub
    and the result :
    BEFORE :
    normal : 6000 x 3000
    scale : 5910 x 2625
    Window : 400 x 200
    client : 394 x 175

    border : 6 x 25

    AFTER :
    normal : 6090 x 3375
    scale : 6000 x 3000
    Window : 406 x 225
    client : 400 x 200
    short version :
    Code:
      Me.Width = 6000
      Me.Height = 3000
      ResizeForm Me.hWnd
    Last edited by sergelac; June 11th, 2005 at 07:00 AM. Reason: changed code

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332

    Re: how to get the form border size ?

    Good work sergelac. Glad to see you worked it out so nicely I might suggest using Me.Move if you want to initially size the form in twips, since it will only cause one resize event. Setting width and height seporately causes two. I would point out also that since the values of your FormSize UDT structure are zero until you set them, it is not necessary to do so in the error handling portion of your code.

    I usually work in pixels, so I appreciate the fact that your function accepts parameters for the desired client size in pixels.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center