来自 Process.MainWindowHandle 的 C# HwndSource

发布于 2024-12-11 21:30:59 字数 719 浏览 0 评论 0原文

我试图“挂钩”窗口的消息来检测最小化/最大化。我环顾四周,认为执行此操作的唯一/最佳解决方案是挂钩窗口的消息,并检查 WM_WINDOWPOSCHANGED 消息,然后检查其状态。

我遇到了问题。

System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(System.Diagnostics.Process.GetProcessesByName("notepad")[0].MainWindowHandle);
System.Windows.Interop.HwndSourceHook hook = new System.Windows.Interop.HwndSourceHook(WndProc);
source.AddHook(hook);

它会给我一个“对象引用未设置为对象实例”。 “source.AddHook...”错误。当断点时,源变量为空也变得很清楚。换句话说:它无法在第一行获取 HwndSource。

我知道可以通过使用“WindowInteropHelper”来实现,但那是当您将实际窗口作为 Windows.Window 可用时,但在我的情况下我没有。

任何解决方法/解决方案将非常感激,
René Sackers

PS 我 100% 确定执行代码时记事本正在运行,并且它设法找到它,并且它是主窗口句柄。

I am trying to "hook" in to the messages of a window to detect a minimize/maximize. I've looked around, and think that the only/best solution to do this, is to hook into the messages of a window, and check for the WM_WINDOWPOSCHANGED message, and then check it's status.

I've run into a problem.

System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(System.Diagnostics.Process.GetProcessesByName("notepad")[0].MainWindowHandle);
System.Windows.Interop.HwndSourceHook hook = new System.Windows.Interop.HwndSourceHook(WndProc);
source.AddHook(hook);

It will give me a "Object refrence not set to the instance of an object." error on "source.AddHook...". When breakpointing, it also becomes clear that the source variable is null. In other words: It fails to get the HwndSource on the first line.

I know that it's possible by using an "WindowInteropHelper", but that is when you have the actual window as a Windows.Window available, but in my situation I do not.

Any workarounds/solutions would be very much appreciated,
René Sackers

P.S. I am 100% sure that Notepad is running when the code is executed, and it manages to find it, and it's main window handle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄离 2024-12-18 21:30:59

HwndSourceHwndSourceHook 不做你的想法。它们仅用于 WPF 和标准 Win32 窗口之间的互操作 - 在同一进程中。它们不能用于挂钩不同进程中窗口的窗口过程。

HwndSource.FromHwnd() 不会创建新的 HwndSource 对象,它“返回指定窗口的 HwndSource 对象”。如果 hWnd 没有关联的 hWnd,FromHwnd() 将返回 null。这就像从记事本调用 hWnd 上的 System.Windows.Forms.Control.FromHandle 一样 - 这也会返回 null,因为记事本窗口不是 WinForms 控件。

挂钩另一个进程的窗口过程的方法是使用 SetWindowsHookEx。为了挂钩另一个进程,代码必须用 C 或 C++ 编写

HwndSource and HwndSourceHook don't do what you are thinking. They only exist for interop between WPF and standard Win32 windows - in the same process. They can't be used for hooking the window procedure of a window in a different process.

HwndSource.FromHwnd() doesn't create a new HwndSource object it "Returns the HwndSource object of the specified window." If the hWnd doesn't have one it associated, FromHwnd() will return null. It would be like calling System.Windows.Forms.Control.FromHandle on the hWnd from notepad - which would return null as well since the notepad window isn't a WinForms control.

The way to hook another process's window procedure is to use SetWindowsHookEx. And for to hook another process, the code has to be written in C or C++.

鹿! 2024-12-18 21:30:59

您误用了 WindowInteropHelper构造函数的文档指出:

为指定的 Windows Presentation Foundation (WPF) 窗口初始化 WindowInteropHelper 类的新实例。

记事本窗口不是 WPF 窗口,这就是 FromHwnd 返回 null 的原因。

事实上,我不相信它可以适用于单独进程中的窗口,即使另一个窗口是 WPF 窗口。

You are misusing WindowInteropHelper. The documentation for the constructor states:

Initializes a new instance of the WindowInteropHelper class for a specified Windows Presentation Foundation (WPF) window.

The notepad window is not a WPF window which is why FromHwnd returns null.

In fact, I don't believe it could ever work for a window in a separate process, even if the other window was a WPF window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文