The actual problem I’m trying to solve is, I want to automatically find out the size of the margins around windows. If you can find a better way, please by all means answer that instead of this.
To do this I decided to take a screenshot of a test window and measure the margins. This is simple enough, as I expect no margins will ever be bright pink, but I admit it’s a hack. I use GetWindowRect (py) to get the bounding box, and PIL to grab a screenshot and crop to the bounding box. The problem is that while the crop operates correctly, the bounding box is not accurate. The Windows 7 “Snipping Tool” gets the correct dimensions. How may I do the same?
My first thoughts were listed below but if, as you state, you’re certain that
GetWindowRectis returning incorrect values, see RESOLUTION further down.“What’s wrong with
GetSystemMetrics(SM_CXBORDER)andGetSystemMetrics(SM_CYBORDER)?The method you’re using seems a very roundabout way of doing it and, if you can call
GetWindowRect(), I’m pretty certain you can callGetSystemMetrics()as well.One other possibility is to use
GetWindowRectto get the entire bounding rectangle for the window andGetClientRectto get the bounding rectangle for the client (non-border) area.This should give you something like
(100,200),(1000,900)and(112,227),(988,888)respectively and you can work out the top border as227-200, bottom as900-888, left as112-100and right as900-888(27,12,12,12).RESOLUTION:
A bit of investigation turns up this. It’s a thread from 2006 stating that you might not get the correct values from
GetWindowsRect. The thread that pointed me to this stated:So basically, use something like (you may have to use ctypes to do this from Python):
and that should give you the correct bounding rectangle.