且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当ShowinTaskBar为false时,窗口消失

更新时间:2023-11-30 20:35:58

这是Winforms中的错误.ShowInTaskbar属性是非常重要的属性,只能在创建本机窗口时指定.在后台,它是传递给 CreateWindowEx() winapi函数,必须在第一个参数中使用WS_EX_APPWINDOW样式才能显示任务栏按钮.

This is a bug in Winforms. The ShowInTaskbar property is a very non-trivial one, it can only be specified when the native window is created. Under the hood, it is a style flag that's passed to the CreateWindowEx() winapi function, the WS_EX_APPWINDOW style has to be used in the first argument to get the taskbar button to display.

问题是,在Load事件触发时,已经进行了该调用.是CreateWindowEx()调用触发了Load事件.因此Winforms必须做一些非常简单的事情,它必须销毁本机窗口并再次重新创建它,现在为第一个参数使用一个不同的值.通常正常,但有时会出错.在您的情况下,它与ShowDialog()的交互作用很差.这样可以确保在对话框窗口关闭或隐藏时,对话框自动关闭.它被关闭,作为ShowInTaskbar分配的副作用.但是,当然是由于错误的原因.

Problem is, by the time the Load event fires, that call was already made. It was the CreateWindowEx() call that got the Load event to fire. So Winforms has to do something very nontrivial, it has to destroy the native window and recreate it again, now using a different value for the first argument. That usually works just fine, but sometimes things go wrong. It interacts very poorly with the ShowDialog() call in your case. Which ensures that a dialog is automatically closed when the dialog window closes or is hidden. It was closed, as a side-effect of your ShowInTaskbar assignment. But of course for the wrong reason.

您必须确保在触发加载事件之前设置该属性.您可以通过使用表单的构造函数来实现.修复:

You must ensure that the property is set before the Load event fires. You do so by using the constructor of the form. Fix:

Public Sub New()
    InitializeComponent()
    Me.ShowInTaskbar = False
End Sub

或者在设计表单时在属性"窗口中设置属性.

Or just set the property in the Properties window when you design the form.