/// <summary> /// 使控件的特定区域无效并向控件发送绘制消息。还可以使分配给该控件的子控件无效。 /// </summary> /// <param name="hWnd">控件句柄。</param> /// <param name="invalidateChildren">若要使控件的子控件无效,则为 true;否则为 false。</param> public static void Invalidate(IntPtr hWnd, bool invalidateChildren) { if (GetIsHandleCreated(hWnd)) { if (invalidateChildren) { UnsafeNativeMethods.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, NativeMethods.RDW_ALLCHILDREN | NativeMethods.RDW_INVALIDATE);//.Net Framework :多NativeMethods.RDW_ERASE } else { UnsafeNativeMethods.InvalidateRect(hWnd, IntPtr.Zero, false);//.Net Framework :支持透明为true,否则false } } }
/// <summary> /// 使控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向控件发送绘制消息。还可以使分配给该控件的子控件无效。 /// </summary> /// <param name="hWnd">控件句柄。</param> /// <param name="rc">一个 System.Drawing.Rectangle,表示要使之无效的区域。</param> /// <param name="invalidateChildren">若要使控件的子控件无效,则为 true;否则为 false。</param> public static void Invalidate(IntPtr hWnd, Rectangle rc, bool invalidateChildren) { if (rc.IsEmpty) { Invalidate(hWnd, invalidateChildren); } else if (GetIsHandleCreated(hWnd)) { if (invalidateChildren) { NativeMethods.RECT rcUpdate = NativeMethods.RECT.FromXYWH(rc.X, rc.Y, rc.Width, rc.Height); UnsafeNativeMethods.RedrawWindow(hWnd, ref rcUpdate, IntPtr.Zero, NativeMethods.RDW_ALLCHILDREN | NativeMethods.RDW_INVALIDATE);//.Net Framework :多NativeMethods.RDW_ERASE } else { NativeMethods.RECT rect = NativeMethods.RECT.FromXYWH(rc.X, rc.Y, rc.Width, rc.Height); UnsafeNativeMethods.InvalidateRect(hWnd, ref rect, false);//.Net Framework :支持透明为true,否则false } } }