Control.Invalidate不触发隐藏或不可见控件的paint事件

Control.Invalidate does not trigger the paint event of hidden or invisible Control

当我在隐藏或不可见控件上调用 Invalidate 方法时,没有触发绘制事件。

这是 windows 表单控件的行为吗?有可用的文档吗?

我查看了 Control.Invalidate 方法的文档,但它没有提到不可见控件 https://msdn.microsoft.com/en-in/library/system.windows.forms.control.invalidated(v=vs.110).aspx

我检查了以下SO问题How are the painting of invisible controls handled in WinForms?,但它被问及紧凑框架中的闪烁问题,与我的问题无关

好吧,我认为调查此问题的最佳方法是检查 Microsoft 的源代码。这是怎么回事:

Control设置为Visiblefalse时,控件的句柄创建标志也设置为false.

Invalidate() 方法(下面的源代码)在 IsHandleCreated 为 false 时不起作用;这就是整个故事。

/// <include file='doc\Control.uex' path='docs/doc[@for="Control.Invalidate3"]/*' />
/// <devdoc>
///     Invalidates the control and causes a paint message to be sent to the control.
///     This will not force a synchronous paint to occur, calling update after
///     invalidate will force a synchronous paint.
/// </devdoc>
public void Invalidate(bool invalidateChildren)
{
    if (IsHandleCreated)
    {
        if (invalidateChildren)
        {
            SafeNativeMethods.RedrawWindow(new HandleRef(window, Handle),
                                            null, NativeMethods.NullHandleRef,
                                            NativeMethods.RDW_INVALIDATE |
                                            NativeMethods.RDW_ERASE |
                                            NativeMethods.RDW_ALLCHILDREN);
        }
        else
        {
            // It's safe to invoke InvalidateRect from a separate thread.
            using (new MultithreadSafeCallScope())
            {
                SafeNativeMethods.InvalidateRect(new HandleRef(window, Handle),
                                                    null,
                                                    (controlStyle & ControlStyles.Opaque) != ControlStyles.Opaque);
            }
        }

        NotifyInvalidate(this.ClientRectangle);
    }
}