且构网

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

THIS.VISIBLE是不是在Windows窗体工作

更新时间:2023-12-06 13:56:16

是的,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.

这是pretty共同想还是创建手柄,但不能使该窗口可见,如果你使用的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仍然不能运行,直到窗口实际上得到可见,所以如果需要移动code到构造。只要打电话展()中的NotifyIcon的上下文菜单事件处理程序,以使窗口可见。

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.