2013-05-10 59 views
0

,我有一個簡單的問題: 我應該繪製在黑色背景上的白色顆粒(矩形),並將其從屏幕的一個到另一個水平移動。 我做到了,但問題是它閃爍太多(即不順利,即使速度很高,我可以很容易地看到每個動作和另一個的黑色背景)C#繪製我非常新的C#移動粒子

t.Interval = 1000/speed; 
t.Tick += new EventHandler(t_Tick); 
t.Start(); 

....

void t_Tick(object sender, EventArgs e) 
     { 
      //g.Clear(Color.Black); 
      g.DrawRectangle(new Pen(Brushes.Black, 20), r);  //draw a black rectangle in the old position...20 is the thickness of the pen 
      r.X += move_x; 
      g.DrawRectangle(new Pen(Brushes.White, 20), r);  //draw a white rectangle in the new position...20 is the thickness of the pen 
      if (r.X >= 1700)  ///this means it reached the end of the screen 
       t.Stop(); 
     } 

我用g.Clear來清除圖形,但這也不起作用,所以在將它移動到新的位置之前,我在舊位置畫了一個黑色的矩形。

任何想法如何消除這種閃爍,甚至它以另一種方式嗎?

+0

想你的窗體的'DoubleBuffered'屬性設置爲TRUE;? – Blorgbeard 2013-05-10 04:42:30

+0

不,那該怎麼辦? – CSawy 2013-05-10 04:43:24

+0

在窗體的設計視圖中,轉到屬性窗口(視圖 - >屬性窗口),並找到顯示「DoubleBuffered」的行,將該值更改爲「True」 – Blorgbeard 2013-05-10 04:44:53

回答

0

嘗試了這一點......一個面板(PANEL1)添加到窗體:

public partial class Form1 : Form 
{ 

    private Rectangle r; 
    private const int rSize = 50; 
    private const int move_x = 10; 
    private System.Windows.Forms.Timer tmr; 

    public Form1() 
    { 
     InitializeComponent(); 

     panel1.BackColor = Color.Black; 
     r = new Rectangle(0, panel1.Height/2 - rSize/2, rSize, rSize); 

     tmr = new System.Windows.Forms.Timer(); 
     tmr.Interval = 50; 
     tmr.Tick += new EventHandler(tmr_Tick); 
     tmr.Start(); 

     panel1.Paint += new PaintEventHandler(panel1_Paint); 
    } 

    void tmr_Tick(object sender, EventArgs e) 
    { 
     r.X += move_x; 
     panel1.Refresh(); 
     if (r.X > panel1.Width) 
     { 
      tmr.Stop(); 
      MessageBox.Show("Done"); 
     } 
    } 

    void panel1_Paint(object sender, PaintEventArgs e) 
    { 
     e.Graphics.DrawRectangle(Pens.White, r); 
    } 

} 
+0

感謝了很多,但還是同樣的問題:( – CSawy 2013-05-12 20:13:02

+0

更好?......更糟糕的?有多大小組?你有什麼其他的圖紙怎麼回事?你能告訴我們您正在使用? – 2013-05-12 20:23:12

+0

相同的完整代碼,沒有更好或更差 不,沒有其他的繪圖 我的代碼和你的完全一樣我只是複製/粘貼它 – CSawy 2013-05-14 03:36:24