VBA form height INCREASES when window caption is removed. Why?

Posted on 2007-07-30
Last Modified: 2013-12-03
I use this code to remove the window caption and window border from an Excel VBA form:

Public Sub RemoveWindowFrame( _
      TargetForm As Object _

' Remove the form's title bar and window frame.

   Const OffsetX = 3
   Const OffsetY = 3
   Dim hRegion As Long
   Dim OriginalRectangle As RECT
   Dim NewRectangle As RECT
   Dim hWnd As Long
   If Val(Application.Version) < 9 Then
      ' Excel 97 or earlier
      hWnd = FindWindow("ThunderXFrame", TargetForm.Caption)
      ' Excel 2000 or later
      hWnd = FindWindow("ThunderDFrame", TargetForm.Caption)
   End If
   GetClientRect hWnd, OriginalRectangle
   SetWindowLong hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) And Not enWindowStyles.WS_CAPTION
   If GetClientRect(hWnd, NewRectangle) Then
      If NewRectangle.Bottom > OriginalRectangle.Bottom Then NewRectangle = OriginalRectangle
      With OriginalRectangle
         ' Offset the rectangle to exclude the borders
         .Left = .Left + OffsetX
         .Top = .Top + OffsetY
         .Right = .Right - OffsetX
         .Bottom = .Bottom - OffsetY
         ' Create a region from the rectangle
         hRegion = CreateRectRgn(.Left, .Top, .Right, .Bottom)
         ' Set the form's window region to this region
         Call SetWindowRgn(hWnd, hRegion, True)
         ' Do not delete this region; it is now owned by Windows and Windows will destroy it when the Form unloads
      End With
   End If
End Sub

On Windows 2000 the NewRectangle height is the same as the OriginalRectangle height. On Windows XP the NewRectangle height is greater than the OriginalRectangle height by the height of the window caption. Logic would tell me that in BOTH cases the height after removing the caption should be LESS, not GREATER. So why is it the SAME on Windows 2000 and GREATER on Windows XP?

I have added the line:

   If NewRectangle.Bottom > OriginalRectangle.Bottom Then NewRectangle = OriginalRectangle

to make the code work but I would like to understand what the window rectangle really is and how the window caption relates to it. From the above it appears that on Windows 2000 the caption is always outside the window rectangle and has no impact on it, and on Windows XP the caption is inside the window rectangle and removing it increases the real estate available. These tests were conducted with Excel 2000 and 2003.

Question by:zorvek (Kevin Jones)
    LVL 38

    Expert Comment

    >GetClientRect hWnd, OriginalRectangle

    This line retrieves the client area of the form... all the surface below the title bar, and excluding the borders.  So logically, at least to me, they should be the same.

    >On Windows XP the NewRectangle height is greater than the OriginalRectangle height by the height of the window caption.

    It appears that there is a difference in how XP handles having the caption removed.  It resizes the client area to the full window height.

    LVL 38

    Accepted Solution

    Here's an example showing the difference between GetWindowRect and GetClientRect... Also shows the VB form properties that correspond.  Simply put, GetWindowRect returns the full window coordinates, while GetClientRect returns just the client area.  Form.Width and Height return full width and height, while ScaleWidth and ScaleHeight return the client area.

    Option Explicit
    Private Declare Function GetClientRect Lib "user32.dll" ( _
         ByVal hwnd As Long, _
         ByRef lpRect As RECT) As Long
    Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Long, ByRef lpRect As RECT) As Long
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type

    Private Sub Command1_Click()
        Dim ClientHeight As Long, ClientWidth As Long
        Dim Height As Long, Width As Long
        ClientHeight = Me.ScaleHeight / Screen.TwipsPerPixelY
        ClientWidth = Me.ScaleWidth / Screen.TwipsPerPixelX
        Height = Me.Height / Screen.TwipsPerPixelY
        Width = Me.Width / Screen.TwipsPerPixelX
        MsgBox "Height " & Height & " Width " & Width
        MsgBox "ClientHeight " & ClientHeight & " ClientWidth " & ClientWidth
        Dim r As RECT
        GetWindowRect Me.hwnd, r
        MsgBox "Api Height: " & r.Bottom - r.Top & " Width " & r.Right - r.Left
        GetClientRect Me.hwnd, r
        MsgBox "Api ClientHeight: " & r.Bottom - r.Top & " ClientWidth " & r.Right - r.Left
    End Sub

    I haven't been able to track down any documentation referring to the difference between 2000 and XP when setting the window caption style value.  

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
    This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now