C# - Win32:在失去焦点后获取最顶层窗口的句柄?
尝试从我的 WPF 应用程序(仅通过系统托盘运行)深入研究 win32 api。
单击系统托盘图标时,我会向用户显示一个上下文菜单,该菜单将焦点放在最上面的窗口上。
我想获得该窗口的句柄(刚刚失去焦点的窗口),并尝试使用不同的方法使用
GetForeGroundWindow()
GetTopWindow()
GetDesktopWindow()
To no end 然而。 我目前正在考虑迭代所有进程,以获取 MainWindowHandles 并检查每个窗口的 z 顺序。
但我认为有一种更简单/更聪明的方法; 只是我无法在谷歌上搜索或回忆起我的旧佩佐德著作中的一个。
另一种方法是我的系统托盘菜单在激活时不获得焦点?
谢谢!
Trying to dig into the win32 api from my WPF application (which just runs through the systray).
When clicking on the systray icon, I present the user with a contextmenu, which gains focus over whatever window was topmost.
I want to get a handle to that window (the one, that just lost focus) and have tried with different approaches using
GetForeGroundWindow()
GetTopWindow()
GetDesktopWindow()
To no end however. I'm currently considering iterating through all processes, to get the MainWindowHandles and checking the z-order of each and every window.
But I reckoned that there's an easier/smarter way; simply just one I cannot google or recall from my old Petzold tome.
Another way would be for my systray menu not to gain focus when activated?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论用户最终选择哪个菜单项,您都可以在打开菜单之前获取最上面的窗口。
然后,如果您需要最上面的窗口,您可以使用打开菜单之前获得的值。
You could get the topmost window before opening the menu, regardless of the menu item the user will eventually choose.
Then, if you need the topmost window, you could just use the value you got before opening the menu.
我认为没有什么比你描述的 z-index 迭代更简单的了。 您的系统托盘菜单必须获得焦点,否则人们将无法使用它(例如,使用键盘)。 如果它获得焦点,那么它就成为前台窗口,因此旧的前台窗口与系统中任何其他非活动窗口没有任何区别。 确实,z-index 检查恕我直言是唯一的方法。
I don't think there is anything simpler than your described z-index iteration. Your systray menu has to get focus, because otherwise people won't be able to use it (for example, with keyboard). And if it gets focus, then it becomes the foreground window, so the old foreground window is left without any distinction from any other inacitve window in the system. Truly, the z-index check IMHO is the only way.