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

前端 未结 10 2146
小鲜肉
小鲜肉 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 01:52

    Based on ng5000's answer, I like using this extension:

            #region Suspend
            [DllImport("user32.dll")]
            private static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
            private const int WM_SETREDRAW = 11;
            public static IDisposable BeginSuspendlock(this Control ctrl)
            {
                return new suspender(ctrl);
            }
            private class suspender : IDisposable
            {
                private Control _ctrl;
                public suspender(Control ctrl)
                {
                    this._ctrl = ctrl;
                    SendMessage(this._ctrl.Handle, WM_SETREDRAW, false, 0);
                }
                public void Dispose()
                {
                    SendMessage(this._ctrl.Handle, WM_SETREDRAW, true, 0);
                    this._ctrl.Refresh();
                }
            }
            #endregion
    

    Use:

    using (this.BeginSuspendlock())
    {
        //update GUI
    }
    

提交回复
热议问题