Reputation: 1419
Premise
I launch a some program.exe
using arguments -width 640 -height 480
which cause the program to launch in the specified size.
Then I want to verify if the progam launched in the specified size so using win32gui.GetWindowRect()
I calculated the window size:
>>> r = win32gui.GetWindowRect(HWND)
>>> w = r[2] - r[0]
>>> h = r[3] - r[1]
>>> print w,h
646 509
The size does not match. I presume that it's most likely due to the title bar.
My question is: How to get the size of the title bar?
Note: I need to know the size of the title bar in order to verify the program size, therefore using win32gui.GetWindowRect()
and subtract its value with the expected size is not a solution.
Image to clarify my problem
Additional question
Does different Operating System have different size of the title bar and border?
According to my own calculation (information not officially confirmed) at default Windows 10, the size of the border is 3px
and the height of the title bar is 26px
. Can someone confirm/rebuke this information?
Upvotes: 2
Views: 1579
Reputation: 106
Option 1: You can hard code and give it fixed offset of 8px from the left right and bottom, and 31px from top to remove the invisible borders and title bar from the top.
but this way when you change the windows scale, it will no longer work properly, this option is great if you are coding to use the program yourself. If you want the program be compatible with multiple scales, try Option 2.
Option 2:
You can use GetClientRect
to get the size of window without the title bar and the invisible borders, then you can use it to calculate the offset
rect = win32gui.GetWindowRect(hwnd)
clientRect = win32gui.GetClientRect(hwnd)
windowOffset = math.floor(((rect[2]-rect[0])-clientRect[2])/2)
titleOffset = ((rect[3]-rect[1])-clientRect[3]) - windowOffset
newRect = (rect[0]+windowOffset, rect[1]+titleOffset, rect[2]-windowOffset, rect[3]-windowOffset)
here newRect is the rect without the title bar or the invisible borders
Source: GetWindowRect GetClientRect
Upvotes: 4