?
Solved

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

Posted on 2007-07-30
2
Medium Priority
?
497 Views
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)
   Else
      ' 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.

Kevin
0
Comment
Question by:zorvek (Kevin Jones)
  • 2
2 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 19594826
>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.

0
 
LVL 38

Accepted Solution

by:
PaulHews earned 2000 total points
ID: 19597333
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.  
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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