Windows 窗体中的 TopMost 属性真的很烦人 [英] Really annoying bug with TopMost property in Windows Forms

查看:41
本文介绍了Windows 窗体中的 TopMost 属性真的很烦人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 Windows 窗体应用程序,它位于通知区域中.单击图标将其显示在前面,再次单击(或单击应用程序 X 图标)将其返回.在这种应用程序类型中,通过单击图标显示窗口时,窗口始终位于顶部很重要(虽然它是可选的).

右键单击该图标会弹出一个上下文菜单,您可以在其中选择是否启用始终在最前面"选项.当应用程序第一次启动时,应用程序设置是从一个 XML 文件中读取的,我 99% 认为这可以正常工作,TopMost 属性被正确读取(和写入).>

一段时间后(几分钟、几小时、几天等等;我通常会休眠并且很少关机)TopMost 停止工作.我不更改选项,我认为没有任何更改选项值但我单击通知区域图标并且应用程序未显示在前面.它出现了,但它在背景上(它显示在 Alt+Tab 上),它并不像它应该的那样始终在顶部".我调出上下文菜单,禁用该选项(因为它已启用)并重新启用它,然后它开始工作.该应用程序现在始终处于最佳状态".但是,它可能会在一段时间后随时失去此能力.

我无法理解为什么会发生这种情况以及这是如何发生的.有谁知道为什么?如果没有,我怎么能尝试调试这种行为?


TopMost 属性更改时,我添加了一段代码来显示 MessageBox,以查看是否可以注意到任何奇怪的行为,但这并不好.它没有帮助,因为表单带有 TopMost = true 但它仍然在后台......

解决方案

最顶层"窗口不止一个.Topmost 只是说在所有非最顶层窗口之前".

我很确定桌面的重新初始化(例如休眠时)需要另一个 SetWindowPos(hwnd, HWND_TOPMOST, ...)(这是底层的 Win32 API 调用).

作为一种解决方法,您可以在显示窗口时重新设置并重新设置属性.

另一种可能性是隐藏窗口也会改变 Z 顺序 - 隐式地 Win32 如何实现它,或者显式地以 WinForms 调用隐藏/显示窗口的方式.

I have this Windows Forms application where it sits in the notification area. Clicking on the icon brings it up front, clicking it again (or clicking on the app X icon) sends it back. This is the type of app that having the window always on top is important when it's displayed by clicking the icon (it's optional though).

Right-clicking the icon brings up a context menu where one can select to enable the "always on top" option or not. When the application first starts up, the app settings are read from an XML file and I'm 99% that this is working as it should, the TopMost property is properly read (and written).

After some time (minutes, hours, days, whatever; I normally hibernate and rarely shutdown) the TopMost stops working. I don't change the option, I don't think anything is changing the option value but I click the notification area icon and app is not brought up front. It shows up but it's on the background (it displays on Alt+Tab), it's not "always on top" as it should. I bring up the context menu, disable the option (cause it's enabled) and enable it back and it starts to work after that. The app is now "always on top". However, it can lose this ability anytime after a while.

I can't understand why this happens and how this happens. Does anyone have any idea why? If not, any idea how could I try to debug such behavior?

EDIT:
I added a piece of code to show a MessageBox when the TopMost property was changed to see if I could notice any strange behavior but it was no good. It didn't help because the form was with TopMost = true but it still was in the background...

解决方案

There is more than just one "Topmost" window. Topmost just says "Before all non-topmost windows".

I am pretty sure a reinitialization of the desktop (such as when hibernating) requires another SetWindowPos(hwnd, HWND_TOPMOST, ...) (which is the underlying Win32 API call).

As a workaround, you could reset and set the property again when showing the window.

Another possibility is that hiding the window also changes the Z order - either implicitely how Win32 implements that, or explicitely in the way WinForms call the hide/show window.

这篇关于Windows 窗体中的 TopMost 属性真的很烦人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆