win32gui MoveWindow() not aligned with left edge of screen
Asked Answered
A

1

10

I am using win32gui to move a Notepad window to the origin of the screen (0, 0) with width and height equal to 500. The result is that the window is not moved to the true left border but ~10 px. to the right. Also the width and height do not equal 500 px. (~620 px. instead).
I am using the following code to produce my results.

import win32gui
from PIL import ImageGrab

# Open notepad.exe manually.
hwnd = win32gui.FindWindow(None, "Untitled - Notepad")
win32gui.MoveWindow(hwnd, 0, 0, 500, 500, True)
bbox = win32gui.GetWindowRect(hwnd)
img = ImageGrab.grab(bbox)

Here a screenshot of the overall position of the window on the screen:
enter image description here

And here a picture of img: enter image description here

Allieallied answered 5/8, 2018 at 13:46 Comment(1)
I can't reproduce the problem using the latest versions of both pywin32 and PIL (pillow). I'm running Windows 7.Bartell
C
9

Windows 10 has an invisible border of 7 pixels. (Totaling to 8 pixels if you include the visible 1 pixel window border.) It is the border for resizing windows which is on the left, right and bottom edge of the window.

Notice how the resizing cursor reacts with the top edge. There is no invisible border there.

An easy fix is to just offset the x in MoveWindow.

win32gui.MoveWindow(hwnd, -7, 0, 500, 500, True)

Or make a new function to do that:

def move_window(hwnd, x, y, n_width, n_height, b_repaint):
    win32gui.MoveWindow(hwnd, x - 7, y, n_width, n_height, b_repaint)

Chrysler answered 8/8, 2018 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.