Results 1 to 15 of 15

Thread: [RESOLVED] How to get the window frame width and height.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    [RESOLVED] How to get the window frame width and height.

    Does anyone know how to find the width of the left part of a windows frame (The width of the red rectangle in the image below), and how to find the height of the top part of a windows frame (The height of the yellow rectangle in the image below). I want it to be able to find this information regardless of the operating system.

    Last edited by nuclear112; Dec 18th, 2007 at 11:17 PM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to get the window frame width and height.

    You can use GetSystemMetrics api:
    Code:
    Option Explicit
    
    Private Const SM_CYSIZE = 31&   'Titlebar height
    Private Const SM_CXBORDER = 5&  'Borders width
    
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    
    Private Sub Command1_Click()
    Dim lngTHeight As Long
    Dim lngBWidth As Long
    
        lngTHeight = Screen.TwipsPerPixelY * GetSystemMetrics(SM_CYSIZE)
        lngBWidth = Screen.TwipsPerPixelX * GetSystemMetrics(SM_CXBORDER)
        
        Debug.Print "Titlebat height = " & lngTHeight
        Debug.Print "Border width = " & lngBWidth
    
    End Sub

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the window frame width and height.

    You can use the GetSystemMetrics API function to get these settings. However the following will also calculate the height of the titlebar + the lower border (if the ScaleMode of the Form is set to Twips).
    Code:
    X = Me.Height - Me.ScaleHeight
    That will of course return it in Twips, divide the result with Screen.TwipsPerPixelY to get it in pixels.

    In the same manner you can calculate the border (left + right side):
    Code:
    Y = Me.Width - Me.ScaleWidth
    Since the border is equal on each side you can simply divide Y by 2 to get your "red rectangle" size, and if you subtract that from X above you would get the size of the "yellow rectangle".

    EDIT: I see I was a bit late replying However I just want to point out that the SM_CYSIZE actually returns the height of a button in the titlebar (minimize, maximize, and close buttons) not the actual height of the titlebar.
    Last edited by Joacim Andersson; Dec 18th, 2007 at 10:31 PM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How to get the window frame width and height.

    How do I make it so that it returns it in pixels and it can get the information from "hWnd".

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the window frame width and height.

    The GetSystemMetrics function returns the various sizes in pixels. Also hWnd has nothing to do with it since these are system wide settings, all windows use the same metrics unless the window has its own skin to modify the non-client area.

  6. #6

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How to get the window frame width and height.

    Ok thanks.

    Code:
    Option Explicit
    
    Private Const SM_CYSIZE = 31&   'Titlebar height
    Private Const SM_CXBORDER = 5&  'Borders width
    
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    
    Private Sub Command1_Click()
    Dim lngTHeight As Long
    Dim lngBWidth As Long
    
        lngTHeight = GetSystemMetrics(SM_CYSIZE)
        lngBWidth = GetSystemMetrics(SM_CXBORDER)
        
        MsgBox "Titlebat height = " & lngTHeight
        MsgBox "Border width = " & lngBWidth
    
    End Sub
    It returns 1, for the border width, which the border with isn't 1.

  8. #8

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How to get the window frame width and height.

    Yes but how do I make it so that it returns the pixels on the twip measurement?

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the window frame width and height.

    Quote Originally Posted by RhinoBull
    [Me.Height - Me.ScaleHeight] doesn't return accurate result - you need to encounter for borders too (at least).
    GetSystemMetrics works best - perhaps you need to use some other const.
    If you read my post you'll see that I meantion that this will return the height of the title bar+border. If you also have a menu it will return that as well. But I also recommend using GetSystemMetrics, I just added this code as an alternative. However SM_CYSIZE will still only return the height of the buttons in the titlebar not the height of the titlebar itself.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How to get the window frame width and height.

    Ok, is it possible to return the height of the toolbar.

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the window frame width and height.

    Quote Originally Posted by RhinoBull
    That is not what I posted and therfore you're getting 1.
    Nope, he gets 1 since that is normally what you would expect to get from SM_CXBORDER and SM_CYBORDER. Those are the width and height of the border around a 2D control (for example a flat textbox). To get the border of a sizable window you should use SM_CXSIZEFRAME (=32) and SM_CYSIZEFRAME (=33). The height of the caption bar is returned by SM_CYCAPTION (=4).

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to get the window frame width and height.

    Quote Originally Posted by nuclear112
    Ok, is it possible to return the height of the toolbar.
    A toolbar is a control window that resides inside the client area. To get the size of any such windows you'll need to know the hWnd, you can then for example use the GetClientRect to determent its size.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: How to get the window frame width and height.

    Ok thanks

  15. #15
    Junior Member
    Join Date
    Oct 2021
    Posts
    23

    Re: How to get the window frame width and height.

    Quote Originally Posted by nuclear112 View Post
    Ok, is it possible to return the height of the toolbar.

    Assuming you mean to end the sentence with a question mark, I am positive the answer is yes with out API. To be absolute legit on all language variant (or English and custom fonts possibilities) Windows installs and accessibility healthy of app awareness to he effect never a Window with fixed sizes and controls ran off out of bounds due to the particular users view anything different then yours. With that said, the most troubling is aspect and it is just a ratio that Twips aren't bound to equal each other in X/Y per pixel. But the title bar portion of the window usually blue in retro theme Windows classic is this:

    1. Outside container allowance client rectangle is left border, and right border, top border and bottom border, and the title bar height is the difference in vertical only.

    2. So if Screen.TwipsPerPixelX = Screen.TwipsPerPixelY then you can go on easier, and just to understand let's do that before aspect involved too.

    Get the size of one vertical border, assume there is two the same in horizontal borders, but with a title bar height in too.
    Code:
    ScaleMode = Twips 'usually the default
    OneVertBorder = ((Width-ScaleWidth) / 2)
    TitleBarHeight = ((Height-ScaleHeight)-(OneVertBorder*2))
    Debug.Print (TitleBarHeight/Screen.TwipsPerPixelY) 'in pixels
    Unimaginable theme differences in Chinese Character Installs and Tweak UI custom themes, even Grandpa's bigger button accessibility options, *gasp!* (don't slowly suffocate, some countries dropped their own languaged Windows even their own language as main use to run with the English crowd, there's not so many anymore as earlier Windows)

    4. Then we incorporate the aspect ratio by finding the slop factor before we may say a horizontal border may equal a vertical border size, truth is, the above code "hopes" all are equal but 90% of accessibility big button or monitor differences in what Twip is not as a pixel, is 15 per pixelX and 15 per pixelY is a crowd norm. By resolution their not "points" ether which printers more so are understanding in exact metric actual size achieved inch is a inch even squared. Twips are more a Bitmap thing, a inherently Windows format that even borders that look about three shade pixels of grey are far more how it brings a warm view. No matter how, can't seem to draw it with just lines, and can't seem to exact draw it with Pixels, I don't know if you tried but I sure entertained myself. Even pulled out the accessibility magnifier don't'cha'know? Bitmap says do a average byte scale scan and present that image is what most people think, like many Twips per one pixel, it is not that in Liquid Crystal and even older monitors that handle the color in friction of fire, temperatures one pixel next to another. Where that falls can be tones in monitors not believing me it is just a grid of RGB LED's to break it down. Ahh, yes, ANTROSCOPIC, that's the word, ANTROSCOPIC is the Classic Windows theme, a 2D ANTROSCOPIC if you can believe it in a DirectX scenery example where it's more available to read about then asking Bitmap and classic Windows theme, Windows Classic is ANTROSCOPIC 2D, and in a hardware sense independent of monitors don't even know they are preforming it in megahertz rates not expressed directly.

    So let's do this, slope is rise/run, but pixels we may end up no different in like a post earlier in this thread expressed, "4" after the code snippet. So a border is 2 pixels, in Twips, because that code finds both Horizontal borders, first divide by 2, then we get 2, and 2 * Screen.TwipsPerPixelX to put it in Twips, for vertical ones 2 * Screen.TwipsPerPixelY similar, but there is no Screen.TwipsPerPixel, there for both X and Y, so we have the two values under the Screen object. The only assumption we need to make is that each vertical border will equal same amount each other, and each horizontal equals same amount as each other, the multiplied by 2 part, because if they don't! Well, it is like trying to find the Title bar that did it, a bigger vertical top border then the vertical bottom border.

    We can do this by factor, or simple deduction, more so OS compatibility, then stray away from floats like Single and Double the precision in a unsigned primitive world can also be extremely vast in expect same results across compatibility, as would be needed. Even though all system metric dimensions are expressed in integral whole number primitives like long, factor means we would need floats in calculating. So, no decimals is accepted where ever VISA and MasterCard are. Seemed like a good model, let's do both:

    a. Deduction
    Code:
    ScaleMode = Twips 'usually the default
    OneVertBorder = ((Width-ScaleWidth) / 2)
    If Screen.TwipsPerPixelX>Screen.TwipsPerPixelY Then
    TitleBarHeight = ((Height-ScaleHeight)-((OneVertBorder*2)+((Screen.TwipsPerPixelX-Screen.TwipsPerPixelY)*2))
    Else
    TitleBarHeight = ((Height-ScaleHeight)-((OneVertBorder*2)+((Screen.TwipsPerPixelY-Screen.TwipsPerPixelX)*2))
    End If
    Debug.Print (TitleBarHeight/Screen.TwipsPerPixelY) 'in pixels
    b. Factor
    Code:
    ScaleMode = Twips 'usually the default
    OneVertBorder = ((Width-ScaleWidth) / 2)
    If Screen.TwipsPerPixelX>Screen.TwipsPerPixelY Then
    TitleBarHeight = ((Height-ScaleHeight)-(OneVertBorder*2)) * (Screen.TwipsPerPixelY/Screen.TwipsPerPixelX)
    Else
    TitleBarHeight = ((Height-ScaleHeight)-(OneVertBorder*2)) * (Screen.TwipsPerPixelX/Screen.TwipsPerPixelY)
    End If
    Debug.Print (TitleBarHeight/Screen.TwipsPerPixelY) 'in pixels
    5. I think I got that right, I'm not sure how far Windows went across the globe when it first came out as far as languages are concerned, I have yet to see the truth is even Right to Left languages and Orient symbols keyboards in Asia exist, and Windows covered it is how so successful. Mathematics is a language. I was seeking the gap between the close button and top of the blue and bottom when I seen your question. I think it was a book called "Secret's of Visual Basic Masters 3" that covers it in VB3 days. But I stayed in XP because I believe in the theme so much, the household network wont let me on the internet with it for security reasons. I tried to tell them it's not true! Windows ME network neighborhood on INTERNET is humming in security because nobody is using it! Don't'cha'know?

    Boss, final answer.
    Last edited by nforystek; May 24th, 2022 at 06:24 AM.

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