pywin32:如何从进程句柄获取窗口句柄,反之亦然 [英] pywin32: how to get window handle from process handle and vice versa

查看:112
本文介绍了pywin32:如何从进程句柄获取窗口句柄,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个用例:

  1. 枚举窗口,然后获取每个窗口的进程句柄

  1. Enumerate windows and then get the process handle for each window

枚举进程,然后获取每个进程的主应用程序窗口句柄

Enumerate processes and then get the main application window handle for each process

推荐答案

枚举窗口,然后获取每个窗口的进程句柄

Enumerate windows and then get the process handle for each window

您需要这些 API:

枚举进程,然后获取主应用程序窗口句柄每个进程

Enumerate processes and then get the main application window handle for each process

您需要这些 API:

  • win32process.EnumProcesses() to enumerate all processes
  • win32api.GetWindowLong() with arguent GWL_STYLE to get window styles and GWL_EXSTYLE to get extended window styles
  • win32gui.GetParent() to determine unowned windows

通过使用 GetWindowThreadProcessId() 过滤 EnumWindows() 的结果,您可以获得属于给定进程的所有窗口.

By filtering the result of EnumWindows() using GetWindowThreadProcessId() you can get all windows that belong to a given process.

确定主窗口可能很棘手,因为没有单一的窗口样式可以将一个窗口指定为主窗口.毕竟,一个应用程序可能有多个主窗口.

Determining the main window can be tricky as there is no single window style that would designate a window as the main window. After all, an application might have multiple main windows.

您能做的最好的事情是使用与 任务栏用于确定应用程序窗口,因为这是用户认为的主窗口:

Best you could do is to use the same rules that the taskbar uses to determine application windows, because that's what the user perceives as main windows:

每当应用程序运行时,Shell 都会在任务栏上放置一个按钮创建一个无主窗口——也就是说,一个没有parent 并且具有适当的扩展样式位.

The Shell places a button on the taskbar whenever an application creates an unowned window—that is, a window that does not have a parent and that has the appropriate extended style bits.

确保窗口按钮是放置在任务栏上,创建一个无主窗口WS_EX_APPWINDOW 扩展样式.为了防止窗口按钮被放置在任务栏上,使用WS_EX_TOOLWINDOW 扩展样式.作为替代方案,您可以创建一个隐藏窗口并使这个隐藏窗口成为您可见的所有者窗口.

To ensure that the window button is placed on the taskbar, create an unowned window with the WS_EX_APPWINDOW extended style. To prevent the window button from being placed on the taskbar, create the unowned window with the WS_EX_TOOLWINDOW extended style. As an alternative, you can create a hidden window and make this hidden window the owner of your visible window.

使用 GetParent()GetWindowLong() 根据这些规则确定具有正确窗口样式的无主窗口.

Use GetParent() and GetWindowLong() to determine the unowned windows that have the right window styles according to these rules.

这篇关于pywin32:如何从进程句柄获取窗口句柄,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆