如何在自定义控件中使用Control.Update方法

时间:2012-06-08 20:43:21

标签: c# custom-controls flicker onpaint

我将首先发布我的代码,因为它简短易懂,然后我会问我的问题。

public class BatteryLabel : Control
{
    private Color _captionColor = SystemColors.Control;
    private Color _textColor = SystemColors.Info;
    private Color _failColor = Color.Red;
    private Color _passColor = Color.Green;
    private string _caption;
    string text2;
    string text3;
    bool battery1Fail = false;
    bool battery2Fail = false;
    bool battery3Fail = false;

    public BatteryLabel()
    {

    }

    public Color BackgroundTextColor
    {
        get{ return _textColor;}
        set{_textColor = value; Invalidate();}
    }

    public string Caption
    {
        get
        {
            return _caption;
        }
        set
        {
            _caption = value;
            Invalidate();
        }
    }

    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
            base.Text = value;
            Invalidate();
        }
    }

    public string Text2
    {
        get { return text2; }
        set { text2 = value; Invalidate(); }
    }

    public string Text3
    {
        get { return text3; }
        set { text3 = value; Invalidate(); }
    }

    public bool Battery1Fail
    {
        get { return battery1Fail; }
        set { battery1Fail = value; Invalidate(); }
    }

    public bool Battery2Fail
    {
        get { return battery2Fail; }
        set { battery2Fail = value; Invalidate(); }
    }

    public bool Battery3Fail
    {
        get { return battery3Fail; }
        set { battery3Fail = value; Invalidate(); }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        e.Graphics.DrawRectangle(Pens.Black, 0,0, Width-1, Height-1);

        var x1 = 50;
        var x2 = 98;
        var x3 = 146;
        var color1 = battery1Fail?_failColor:BackgroundTextColor;
        var color2 = battery2Fail?_failColor:BackgroundTextColor;
        var color3 = battery3Fail?_failColor:BackgroundTextColor;

        e.Graphics.FillRectangle(new SolidBrush(color1),x1+1, 1, 47, Height-2);
        e.Graphics.FillRectangle(new SolidBrush(color2),x2+1, 1, 47, Height-2);
        e.Graphics.FillRectangle(new SolidBrush(color3),x3+1, 1, 47, Height-2);

        e.Graphics.DrawLine(Pens.Black, x1,0, x1, Height-1);
        e.Graphics.DrawLine(Pens.Black, x2,0, x2, Height-1);
        e.Graphics.DrawLine(Pens.Black, x3,0, x3, Height-1);

        var BoldFont = new Font(this.Font, FontStyle.Bold);

        e.Graphics.DrawString(Caption, BoldFont, new SolidBrush(ForeColor), 0,0);
        e.Graphics.DrawString(Text, this.Font, new SolidBrush(ForeColor), x1,0);
        e.Graphics.DrawString(Text2, this.Font, new SolidBrush(ForeColor), x2,0);
        e.Graphics.DrawString(Text3, this.Font, new SolidBrush(ForeColor), x3,0);

    }   
}

控件大小意味着195,14,以防您决定尝试使用它。我在面板中有8个这样的200,200在1.6Ghz原子处理器上运行。它用于在计算机上显示最多3个电池的值。标签每500毫秒刷新一次。你可能已经聚集了一点点闪烁,但这是可以容忍的。如果可能的话,我只想更少。所以我开始研究使用Update,并移动我的一些代码,例如背景位,我想也许我应该将它移动到OnPaintBackground(),但在测试框架中,我组成的Update方法不会改变任何东西,并且当我使用Invalidate方法时,它运行OnPaintBackground和OnPaint。这是我在那种情况下尝试过的。

public class InformationLabel : Control
{
    Random r = new Random();
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        base.OnPaintBackground(e);
        Color randomCOlor = Color.FromArgb(r.Next());
        e.Graphics.FillRectangle(new SolidBrush(randomCOlor),0,0, Width-1, Height-1);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Color randomCOlor = Color.FromArgb(r.Next());
        e.Graphics.FillPie(new SolidBrush(randomCOlor),15,15,15,15, 0.0f, 120.0f);
    }
}

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    void Button1Click(object sender, EventArgs e)
    {
        informationLabel1.Update();
    }

    void Button2Click(object sender, EventArgs e)
    {
        informationLabel1.Invalidate();
    }
}

我做了一个用户控制大约300,300,所以我可以肯定我所看到的。我忘了提到在我500ms计时器的电池控制中我只更新text,text2和text3。我想如果那个文本的值超出规格我将设置电池失败标志然后无效..但我不确定。那我该怎么做才更新文本???

2 个答案:

答案 0 :(得分:3)

您可以在构造函数中添加以下行来消除闪烁:

SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.Opaque | ControlStyles.AllPaintingInWmPaint, true);

现在在绘制处理程序中绘制背景和其他所有内容。

可以通过仅对需要重新绘制的区域传递Rectangle到Invalidate来完成优化。然后在OnPaint覆盖中使用e.ClipRectangle来确定要绘制的内容。对于这样一个简单的绘图,这可能不是必需的。

答案 1 :(得分:2)

我相信你正在寻找错误的地方来消除闪烁。我可以使用你的BatteryLabel基本上只用一行就可以无文字地更新文本。将构造函数更改为如下所示:

public BatteryLabel()
{
    this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
}

这告诉控件双重缓冲其图形,这会使闪烁消失。

以100毫秒的刷新间隔进行测试:

Timer t;
public Form1()
{
    InitializeComponent();

    t = new Timer();
    t.Interval = 100;
    t.Tick += new EventHandler(t_Tick);
    t.Start();
}

void t_Tick(object sender, EventArgs e)
{
    string ticks = DateTime.Now.Ticks.ToString();
    string ticks1 = ticks.Substring(ticks.Length-4),
        ticks2 = ticks.Substring(ticks.Length - 5,4),
        ticks3 = ticks.Substring(ticks.Length - 6,4);

    batteryLabel1.Text = ticks1;
    batteryLabel1.Text2 = ticks2;
    batteryLabel1.Text3 = ticks3;
    batteryLabel1.Battery1Fail = ticks1.StartsWith("1");
    batteryLabel1.Battery2Fail = ticks2.StartsWith("1");
    batteryLabel1.Battery3Fail = ticks3.StartsWith("1");
}

这有用,还是我误解了你?