.NET (C#):当您只有进程句柄或 PID 时获取子窗口? [英] .NET (C#): Getting child windows when you only have a process handle or PID?
问题描述
一种特殊情况的问题:
- 我使用
System.Diagnostics.Process.Start(..)
启动一个进程 - 该过程会打开一个闪屏——这个闪屏成为主窗口.
- 启动画面关闭并显示真实"用户界面.主窗口(启动画面)现在无效.
- 我还有 Process 对象,我可以查询它的句柄、模块等.但主窗口句柄现在无效.
此时我需要获取进程的 UI(或 UI 句柄).假设我无法更改流程的行为以使其更容易(或更理智).
我在网上环顾四周,但我承认我没有找一个多小时.看起来应该有点微不足道:-(
如果你不介意使用 Windows API,你可以使用 EnumWindowsProc
,并使用 GetWindowThreadProcessId
(以查看它是否在您的进程中),以及然后也许 IsWindowVisible
、GetWindowCaption
和 GetWindowTextLength
来确定您的进程中的哪个 hWnd
是您想要的.>
尽管如果您之前没有使用过这些功能,那么这种方法将是一个真正的痛苦,所以希望有一种更简单的方法.
Kind of a special case problem:
- I start a process with
System.Diagnostics.Process.Start(..)
- The process opens a splash screen -- this splash screen becomes the main window.
- The splash screen closes and the 'real' UI is shown. The main window (splash screen) is now invalid.
- I still have the Process object, and I can query its handle, module, etc. But the main window handle is now invalid.
I need to get the process's UI (or UI handle) at this point. Assume I cannot change the behavior of the process to make this any easier (or saner).
I have looked around online but I'll admit I didn't look for more than an hour. Seemed like it should be somewhat trivial :-(
If you don't mind using the Windows API, you could use EnumWindowsProc
, and check each of the handles that that turns up using GetWindowThreadProcessId
(to see that it's in your process), and then maybe IsWindowVisible
, GetWindowCaption
and GetWindowTextLength
to determine which hWnd
in your process is the one you want.
Though if you haven't used those functions before that approach will be a real pain, so hopefully there's a simpler way.
这篇关于.NET (C#):当您只有进程句柄或 PID 时获取子窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!