troubleshooting Question

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

Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)Flag for United States of America asked on
Visual Basic ClassicMicrosoft ExcelMicrosoft Development
2 Comments1 Solution574 ViewsLast Modified:
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.

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros