简体   繁体   English

如何知道 Window 是否通过 python ctypes 最小化?

[英]How to know if the Window is minimized via python ctypes?

I hide a window using codes below我使用下面的代码隐藏了一个 window

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.我需要一个线程来监视这个 window 是否被其他进程重新打开,但我找不到 user32 api 来执行此操作。 windll.user32.GetWindowRect doesn't meet my purpose either. windll.user32.GetWindowRect也不符合我的目的。 Any suggestion?有什么建议吗?

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).在一切之前,检查[SO]: C function 通过 ctypes 从 Python 调用返回不正确的值(@CristiFati 的回答) ,这是使用CTypes (调用函数)时的常见陷阱。
Even if your code works, it's still U ndefined B ehavior !即使您的代码有效,它仍然是U ndefined B行为

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).我准备了一个使用[MS.Learn]的小例子: IsIconic function (winuser.h)

code00.py :代码00.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 : 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 WinAPI s (and it's user friendlier - easier to use).作为CTypes的替代方案,您可以使用[GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions ),它是WinAPI s 的Python包装器(并且它对用户更友好 - 更易于使用)。

You can use the SetWinEventHook() function to start monitoring for specific events.您可以使用SetWinEventHook() function 开始监视特定事件。 You will need to provide a callback function to be called each time an event is triggered.您需要提供一个回调 function,以便在每次触发事件时调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM