非所有控制項皆提供 BeginUpdate/EndUpdate,需自行透過 Windows API 實作:
P/Invoke Interop Assistant 工具生成簽章。[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
public static void BeginUpdate(Control c) => SendMessage(c.Handle, 0x0B, IntPtr.Zero, IntPtr.Zero);
public static void EndUpdate(Control c) => SendMessage(c.Handle, 0x0B, (IntPtr)1, IntPtr.Zero);
無邊框視窗從 Normal 切換至 Maximized(TopMost=true)時,控制項不閃爍正常,但任務列消失,背景色填充螢幕。原因:WM_SETREDRAW 影響了整個視窗層級,波及任務列重繪。
FindWindow("Shell_TrayWnd", "")。RedrawWindow 強制重繪(參考 Control.Invalidate 內部實作):[DllImport("user32.dll")]
static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, uint flags);
const uint RDW_FRAME = 0x0400;
const uint RDW_INVALIDATE = 0x0001;
const uint RDW_ALLCHILDREN = 0x0080;
RedrawWindow(taskbarHandle, IntPtr.Zero, IntPtr.Zero, RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
發現 Control 內部確有 BeginUpdateInternal 方法,但標記為 internal,僅 ListView 等少數控制項使用。微軟未公開此功能,需自行 P/Invoke 實作。