Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

win32gui MoveWindow() not aligned with left edge of screen

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

like image 826
Jan-Benedikt Jagusch Avatar asked Aug 05 '18 13:08

Jan-Benedikt Jagusch


1 Answers

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)

like image 193
cheazy Avatar answered Nov 07 '22 06:11

cheazy