来自 Process.MainWindowHandle 的 C# HwndSource
我试图“挂钩”窗口的消息来检测最小化/最大化。我环顾四周,认为执行此操作的唯一/最佳解决方案是挂钩窗口的消息,并检查 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HwndSource
和HwndSourceHook
不做你的想法。它们仅用于 WPF 和标准 Win32 窗口之间的互操作 - 在同一进程中。它们不能用于挂钩不同进程中窗口的窗口过程。HwndSource.FromHwnd() 不会创建新的 HwndSource 对象,它“返回指定窗口的 HwndSource 对象”。如果 hWnd 没有关联的 hWnd,
FromHwnd()
将返回 null。这就像从记事本调用 hWnd 上的 System.Windows.Forms.Control.FromHandle 一样 - 这也会返回 null,因为记事本窗口不是 WinForms 控件。挂钩另一个进程的窗口过程的方法是使用 SetWindowsHookEx。为了挂钩另一个进程,代码必须用 C 或 C++ 编写。
HwndSource
andHwndSourceHook
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 callingSystem.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++.
您误用了
WindowInteropHelper
。 构造函数的文档指出:记事本窗口不是 WPF 窗口,这就是
FromHwnd
返回null
的原因。事实上,我不相信它可以适用于单独进程中的窗口,即使另一个窗口是 WPF 窗口。
You are misusing
WindowInteropHelper
. The documentation for the constructor states:The notepad window is not a WPF window which is why
FromHwnd
returnsnull
.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.