2013-03-08 105 views
0

我已經創建了一個用戶控件將屏幕分割成矩形,但我的控件正在執行OnPaintBackground很多時候它應該執行它,只有一個請幫助我,因爲執行它一次非常重要,因爲scrren開始閃爍很多。OnPaintBackground多次執行C#?

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; 
    } 

} 

輸出是屏幕flickaring多繪製背景時一遍又一遍......

+0

使用雙緩衝。 – SLaks 2013-03-08 14:06:50

+0

@SLaks你能指出如何做到這一點 – 2013-03-08 14:17:23

回答