如何跟踪窗口何时被激活和停用?
我正在制作一个任务栏,我需要知道活动窗口何时更改。我不想使用计时器。我想也许有一个 WndProc 消息我可以拦截。
我正在使用 C#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我正在制作一个任务栏,我需要知道活动窗口何时更改。我不想使用计时器。我想也许有一个 WndProc 消息我可以拦截。
我正在使用 C#
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
这需要通过 SetWindowsHookEx WH_SHELL 设置一个钩子。该回调会获取 HSHELL_WINDOWACTIVATED 和 HSHELL_WINDOWCREATED 等通知,以便您可以了解顶级窗口发生的情况。查看“ShellProc 的 SDK 文章”查看您可以获得的通知的完整列表。几乎所有这些都与实现您自己的任务栏相关。
这是个好消息。坏消息是 WH_SHELL 是一个全局钩子。它需要一个可以注入另一个进程的 DLL。问题是,您无法用托管代码编写该 DLL。将托管代码注入非托管进程是不可能的。
再次回到好消息,有人解决了这个问题,并创建了一个可注入且可以与托管代码互操作的非托管 DLL。项目在这里。不知道它有多好。我怀疑它可能不太能证明 UAC。
That requires a hook set by SetWindowsHookEx, WH_SHELL. The callback gets notifications like HSHELL_WINDOWACTIVATED and HSHELL_WINDOWCREATED so that you can know what's happening with the top-level windows. Check out the SDK article for "ShellProc" for the full list of notifications you can get. Just about all of them are relevant to implementing your own taskbar.
That was the good news. The bad news is that WH_SHELL is a global hook. It requires a DLL that can be injected into another process. Problem is, you can't write that DLL in managed code. Injecting managed code into an unmanaged process is not possible.
Back to good news again, somebody has solved that problem and has created an unmanaged DLL that is injectable and can interop with managed code. The project is here. No idea how good it is. I suspect it might not be quite UAC proof.