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

前端 未结 10 1945
小鲜肉
小鲜肉 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:56

    I usually use a little modified version of ngLink's answer.

    public class MyControl : Control
    {
        private int suspendCounter = 0;
    
        private void SuspendDrawing()
        {
            if(suspendCounter == 0) 
                SendMessage(this.Handle, WM_SETREDRAW, false, 0);
            suspendCounter++;
        }
    
        private void ResumeDrawing()
        {
            suspendCounter--; 
            if(suspendCounter == 0) 
            {
                SendMessage(this.Handle, WM_SETREDRAW, true, 0);
                this.Refresh();
            }
        }
    }
    

    This allows suspend/resume calls to be nested. You must make sure to match each SuspendDrawing with a ResumeDrawing. Hence, it wouldn't probably be a good idea to make them public.

提交回复
热议问题