this.Visible 在 Windows 窗体中不起作用 [英] this.Visible is not working in Windows Forms
问题描述
我有问题.我需要在窗口加载时隐藏我的窗口.但是
I have a problem. I need to hide my window at window load. But
private void Form1_Load(object sender, EventArgs e)
{
this.Visible = false;
}
不工作.并且属性 Visible
仍然为真.我错过了什么吗?
is not working. And property Visible
remains true. Am I missing something?
推荐答案
是的,Visible 属性在 Windows 窗体中很重要,它实际上是创建句柄并导致 OnLoad() 运行的原因.换句话说,窗口在它变得可见之前不存在.它会忽略撤消此操作的尝试.
Yes, the Visible property is a big deal in Windows Forms, that's what actually gets the handle created and causes OnLoad() to run. In other words, the window doesn't exist until it gets visible. And it will ignore attempts to undo this.
如果您使用 NotifyIcon,想要仍然创建句柄但不使窗口可见是很常见的.您可以通过覆盖 SetVisibleCore 来实现:
It is pretty common to want to still create the handle but not make the window visible if you use a NotifyIcon. You can achieve this by overriding SetVisibleCore:
protected override void SetVisibleCore(bool value) {
if (!this.IsHandleCreated) {
value = false;
CreateHandle();
}
base.SetVisibleCore(value);
}
请注意,在窗口实际可见之前,OnLoad 仍不会运行,因此如有必要,请将代码移至构造函数中.只需在 NotifyIcon 的上下文菜单事件处理程序中调用 Show() 即可使窗口可见.
Beware that OnLoad still won't run until the window actually gets visible so move code into the constructor if necessary. Just call Show() in the NotifyIcon's context menu event handler to make the window visible.
这篇关于this.Visible 在 Windows 窗体中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!