How do I suspend painting for a control and its children?

前端 未结 10 1943
小鲜肉
小鲜肉 2020-11-22 01:34

I have a control which I have to make large modifications to. I\'d like to completely prevent it from redrawing while I do that - SuspendLayout and ResumeLayout aren\'t eno

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 02:09

    The following is the same solution of ng5000 but doesn't use P/Invoke.

    public static class SuspendUpdate
    {
        private const int WM_SETREDRAW = 0x000B;
    
        public static void Suspend(Control control)
        {
            Message msgSuspendUpdate = Message.Create(control.Handle, WM_SETREDRAW, IntPtr.Zero,
                IntPtr.Zero);
    
            NativeWindow window = NativeWindow.FromHandle(control.Handle);
            window.DefWndProc(ref msgSuspendUpdate);
        }
    
        public static void Resume(Control control)
        {
            // Create a C "true" boolean as an IntPtr
            IntPtr wparam = new IntPtr(1);
            Message msgResumeUpdate = Message.Create(control.Handle, WM_SETREDRAW, wparam,
                IntPtr.Zero);
    
            NativeWindow window = NativeWindow.FromHandle(control.Handle);
            window.DefWndProc(ref msgResumeUpdate);
    
            control.Invalidate();
        }
    }
    

提交回复
热议问题