GetWindowPlacement() different values for windows in the exact same location

Coptil David 20 Reputation points
2024-07-30T22:18:25.2566667+00:00

Why does GetWindowPlacement() give different coordinates for two applications that are positioned in the exact same location.

So I opened three different windows of Chrome, and placed them on the exact location all three. One on top of the other. In code (in python) I just print the placement of the current selected window with: print( win32gui.GetWindowPlacement( win32gui.GetForegroundWindow() ) )

When I go through them, the printed coordinates change for each one... why is that? they are places in the exact same location, same width/height, even the same application...

This is what I get in the console:

window 1: (0, 1, (-1, -1), (-1, -1), (193, 484, 1995, 2034))

window 2: (0, 1, (-1, -1), (-1, -1), (1067, 307, 2870, 1858))

window 3: (0, 1, (-1, -1), (-1, -1), (466, 133, 2271, 1686))

The thing I am trying to do is to open a chrome window at that speciffic location from this pyhton script.. But with the problem from above it is impossible, because the coordinates seem to change everytime.

I think I have to specify that I have 3 monitors, with different scaling(two - 1080p and one - 4k). but in the case specified above, all windows were opened on the same monitor, so I don;t think the scaling is the problem in this case.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,653 questions
{count} votes

Accepted answer
  1. RLWA32 45,701 Reputation points
    2024-08-08T09:01:09.0366667+00:00

    @Coptil David It is known that when a window is snapped the snapped location is not reflected in the information returned by calling GetWindowPlacement. The rcNormalPosition RECT is unchanged after snapping.

    As I understand it, snapping is a feature of the shell, not the window manager. To my knowledge there is no Windows API function that will snap a window programmatically. Simply moving a window is not equivalent to using the snap feature. This is not unlike Microsoft's implementation of Virtual Desktops which is intended to be controlled by users, not by applications. Consequently there is virtually no available documented API that enables programmatic control.

    However, beginning with Win10 1903 there is an API function to indicate if a Window is snapped -- refer to IsWindowArranged function.

    Update -- I can get the snap process started by using SendInput. But I haven't figured out yet how to finalize the process by exiting Snap Assist.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. RLWA32 45,701 Reputation points
    2024-08-08T16:37:20.8933333+00:00

    Following uses SendInput to send keystrokes to simulate a user snapping a window to the left side of the screen -

    Simulated

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.