gpt4 book ai didi

c# - 如何将另一个应用程序的窗口句柄最小化到系统托盘?

转载 作者:可可西里 更新时间:2023-11-01 10:09:26 24 4
gpt4 key购买 nike

实例 A 正在尝试恢复实例 B 的窗口,但我无法获取实例 B 的窗口句柄。我认为问题是 B 正在使用以下方法将窗口最小化到系统托盘:

this.Visibility = Visibility.Hidden;

A 尝试使用以下方法获取 B 的窗口句柄:

Process process = Process.GetCurrentProcess();
Process.GetProcessesByName(process.ProcessName).First().MainWindowHandle;

等于 IntPtr.Zero

我还尝试使用 Spy++ 按类名获取窗口句柄,但类名具有以下格式的每个实例 GUID:

HwndWrapper[FileName.exe;;ad445199-cf93-48a4-bd24-2f97d54c8af8]

最佳答案

那是因为你想要的基本上不存在,而 MainWindowHandle 的概念是一个严重的误称,潜入 .Net Framework 中永远混淆。来自 There can be more than one (or zero): Converting a process to a window :

"I have a thread ID. How do I get the corresponding window?"

You can use the EnumThreadWindows function to get all the windows on the thread.

"Yes, I know about EnumThreadWindows, but how do I get the window that I want?"

Well, you haven't said what you wanted yet.

"I want the window that corresponds to the thread."

But which one? How will you decide among all the windows?

"That's what I'm asking you!"

But you haven't yet described what you want.

"I want the window corresponding to the thread. Why won't you answer my question?"

Note that saying, "I am looking for the top-level unowned window" is a step forward, but it still doesn't uniquely identify a window. There can be multiple top-level unowned windows in a process. For example, Explorer typically has lots of top-level unowned windows. There's the desktop, the taskbar, your open folder windows, and property sheets. If you ask for "the" top-level unowned window of Explorer, which one do you want?

Perhaps people are getting the idea that there is a way to uniquely specify "the" window for a process because the System.Diagnostics.Process object has a property called MainWindowHandle. The documentation for that property doesn't do anything to dispel the notion, either. I have no idea how that property decides among multiple top-level unowned windows.

MSDN 问答中也详细阐述了该主题 Get the Main Window :

Q How can I find the main window for a process? I'm writing a Spy-like tool and I need to get the main window (HWND) for a process so I can send it a message like WM_ACTIVATEAPP.

A Which main window do you mean?

关于c# - 如何将另一个应用程序的窗口句柄最小化到系统托盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3721192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com