How to fix the flickering in User controls

后端 未结 12 1556
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 02:32

In my application i am constantly moving from one control to another. I have created no. of user controls, but during navigation my controls gets flicker. it takes 1 or 2 se

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 02:58

    If you are doing any custom painting in the control (i.e. overriding OnPaint) you can try the double buffering yourself.

    Image image;
    protected override OnPaint(...) {
        if (image == null || needRepaint) {
            image = new Bitmap(Width, Height);
            using (Graphics g = Graphics.FromImage(image)) {
                // do any painting in image instead of control
            }
            needRepaint = false;
        }
        e.Graphics.DrawImage(image, 0, 0);
    }
    

    And invalidate your control with a property NeedRepaint

    Otherwise the above answer with SuspendLayout and ResumeLayout is probably what you want.

提交回复
热议问题