Uncategorized

Trouble with creating windows at precise locations with win32gui and python


I’ve been (mostly for recreational purposes) trying to create a bunch of windows at precise coordinates and dimensions, but they don’t seem to appear on the screen in the right place.

I’ve been using win32gui’s CreateWindow function as follows:

hwnd = win32gui.CreateWindow(
    "default", title, 0,
    x, y, w, h,
    0, 0, 0, None
)
win32gui.UpdateWindow(hwnd)
win32gui.ShowWindow(hwnd, win32con.SW_SHOW)

but when I create windows that should be adjacent to one another, they have gaps between them. Using the Screen Ruler from Window’s PowerToys, I’ve found that the windows created are around 20 pixels thinner and 10 pixels shorter than expected. win32gui also doesn’t seem to let me create windows that are smaller than around 150×50 pixels but I assume that that is intended behavior. I’ve been just working around these issues but if anyone can point out what the cause is or a way to fix it, that would be greatly appreciated!

An MRE of the issue is below:

import win32gui, win32con


class Window:
    @classmethod
    def init(cls):
        cl = win32gui.WNDCLASS()
        cl.lpfnWndProc = {win32con.WM_DESTROY: cls.destroy}
        cl.lpszClassName = "default"
        win32gui.RegisterClass(cl)

    def __init__(self, x, y, w, h, title="", style=0):
        self.hwnd = win32gui.CreateWindow(
            "default", title, style,
            x, y, w, h,
            0, 0, 0, None
        )
        win32gui.UpdateWindow(self.hwnd)
        win32gui.ShowWindow(self.hwnd, win32con.SW_SHOW)

    def destroy(self):
        win32gui.DestroyWindow(self.hwnd)

if __name__ == '__main__':
    Window.init()
    Window(100, 100, 200, 100)
    Window(300, 100, 200, 100)
    Window(300, 200, 200, 100)
    Window(100, 200, 200, 100)
    input()

This code should create four windows with no gap between them, but it clearly does not.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *