Home > database >  How to know if the Window is minimized via python ctypes?
How to know if the Window is minimized via python ctypes?

Time:01-25

I hide a window using codes below

h = windll.user32.FindWindowA(b'xxx', None)
windll.user32.ShowWindow(h, 0)

I need a thread to monitor if this window is reopen by other proccess, but I can't find a user32 api to do that. windll.user32.GetWindowRect doesn't meet my purpose either. Any suggestion?

CodePudding user response:

You can use the SetWinEventHook() function to start monitoring for specific events. You will need to provide a callback function to be called each time an event is triggered.

CodePudding user response:

Before everything, check [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer) for a common pitfall when working with CTypes (calling functions).
Even if your code works, it's still Undefined Behavior!

I prepared a small example that uses [MS.Learn]: IsIconic function (winuser.h) (might also want to check [SO]: win32: check if window is minimized for more details).

code00.py:

#!/usr/bin/env python

import ctypes as cts
import ctypes.wintypes as wts
import msvcrt
import sys


def main(*argv):
    user32 = cts.CDLL("User32.dll")
    FindWindow = user32.FindWindowW
    FindWindow.argtypes = (wts.LPCWSTR, wts.LPCWSTR)
    FindWindow.restype = wts.HWND
    IsIconic = user32.IsIconic
    IsIconic.argtypes = (wts.HWND,)
    IsIconic.restype = wts.BOOL

    title = "Untitled - Notepad"
    hwnd = FindWindow(None, title)
    print("Window ('{:s}') HWND: {:}".format(title, hwnd))
    if not hwnd:
        # Could display some error message (GetLastError())
        return
    while True:
        print("\nWindow {:s} minimized".format("is" if IsIconic(hwnd) else "is NOT"))
        print("Play with the window ((un)minimize it) then press a key (<ESC> to exit) ...")
        c = ord(msvcrt.getch())
        if c == 0x1B:
            break


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.\n")
    sys.exit(rc)

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075227812]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" ./code00.py
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] 064bit on win32

Window ('Untitled - Notepad') HWND: 919796

# @TODO - cfati: Window is initially unminimized

Window is NOT minimized
Play with the window ((un)minimize it) then press a key (<ESC> to exit) ...

Window is NOT minimized
Play with the window ((un)minimize it) then press a key (<ESC> to exit) ...

# @TODO - cfati: Minimize window

Window is minimized
Play with the window ((un)minimize it) then press a key (<ESC> to exit) ...

Window is minimized
Play with the window ((un)minimize it) then press a key (<ESC> to exit) ...

# @TODO - cfati: Restore window

Window is NOT minimized
Play with the window ((un)minimize it) then press a key (<ESC> to exit) ...

Done.

As an alternative to CTypes, you can use [GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions), which is a Python wrapper over WinAPIs (and it's user friendlier - easier to use).

  • Related