执行OnPaintBackground很多次C#?

时间:2013-03-08 14:06:07

标签: c# winforms user-controls panels

我创建了一个UserControl,将屏幕拆分为矩形,但我的控件正在执行OnPaintBackground很多次,它应该只执行一次,请帮助我,因为scrren开始只执行一次非常重要闪烁多了。

public partial class Schedual : UserControl
{
    int days;

    public int Days
    {
        get { return days; }
        set
        {
            days = value;
            change = true;
            Invalidate(true);
        }
    }

    int periods;

    public int Periods
    {
        get { return periods; }
        set
        {
            periods = value;
            change = true;
            Invalidate(true);
        }
    }

    Brush brush;

    bool change = false;

    List<Panel> panels;

    public Schedual()
    {
        InitializeComponent();
        this.ResumeLayout(true);
        this.days = 1;
        this.periods = 1;
        brush = Brushes.White;
        change = false;
     }

    protected override void OnPaint(PaintEventArgs e)
    {
        //stuff ....... or base.OnPaint(e);
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        var h = this.Height / days;
        var w = this.Width / periods;
        for (int i = 0; i < days; i++)
        {
            for (int j = 0; j <= periods; j++)
            {
                g.FillRectangle(brush, j * w, i * h, w, h);
                if (change)
                {
                    AddPanel(j * w, i * h, w, h);
                }
                g.DrawLine(Pens.Black, 0, i * h, this.Right, i * h); //draw the horizantle lines
                g.DrawLine(Pens.Black, j * w, 0, this.Bottom, j * w); //draw the verical lines
            }
        }
        change = false;
    }

}

输出是一次又一次地绘制背景时屏幕闪烁很多......

1 个答案:

答案 0 :(得分:0)

双缓冲...

在此处查看更多信息 - http://msdn.microsoft.com/en-us/library/3t7htc9c.aspx