win32gui.EnumWindows
时间: 2023-12-01 15:51:27 浏览: 156
win32gui.EnumWindows is a function in the Python win32gui library that is used to enumerate all top-level windows on the desktop. The function takes a callback function as an argument and calls it once for each window. The callback function receives two arguments: the handle of the window and an optional argument that can be passed to EnumWindows. The return value of EnumWindows is not used.
The typical use of EnumWindows is to find a specific window by its title or class name. The callback function can use win32gui.GetWindowText and win32gui.GetClassName to get the window's title and class name, respectively. Once the desired window is found, its handle can be used to perform various operations on the window, such as setting its position or sending it messages.
Here is an example of using EnumWindows to find a window with a specific title:
```
import win32gui
def find_window(title):
handle = None
def callback(hwnd, arg):
nonlocal handle
if win32gui.GetWindowText(hwnd) == title:
handle = hwnd
win32gui.EnumWindows(callback, None)
return handle
handle = find_window("Notepad")
if handle:
# do something with the window handle
else:
print("Window not found")
```
阅读全文