开发者

Custom Control flickers when changing background

开发者 https://www.devze.com 2023-04-11 15:09 出处:网络
I have a custom control that has other controls on it. When the user clicks on it, I recursively go through all controls and change their background color to blue. However, I get a massive flicker pro

I have a custom control that has other controls on it. When the user clicks on it, I recursively go through all controls and change their background color to blue. However, I get a massive flicker problem as the controls change color individually. I have double buffering enabled, but I doubt that it开发者_开发技巧 optimizes my drawing. I have a suspicion that this may not be the best way of doing such an effect.

How can I get rid of this flickering? Or is there a better way of doing this?

My call OnClick:

ControlUtils.SetColorRecursive(this, Color.LightSteelBlue);

SetColorRecursive:

    tCtl.SuspendLayout();

        if (tCtl != null)
        {
            // Set Color
            tCtl.BackColor = tColor;

            foreach (Control tSubCtl in tCtl.Controls)
            {
                // Ignore the following
                if (tSubCtl is TextBox) continue;
                if (tSubCtl is ListBox) continue;
                if (tSubCtl is NumericUpDown) continue;

                // Recursively change sub-controls
                SetColorRecursive(tSubCtl, tColor);
            }
        }

    tCtl.ResumeLayout();


Do you have double buffering enabled on the background of every control being re-colored? (-not only the Form)


I found that this solves my problem on Vista and above. WinXP users might be SOL.

    protected override CreateParams CreateParams
    {
        get
        {
            // This eliminates child control flicker when selecting
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;
            return cp;
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号