2012-10-17 50 views
-1

我在Windows窗體中使用下面的代碼來移動按鈕。但是,按鈕在移動時閃爍。即使我使用SmoothingMode.HighQuality和DoubleBuffer。移動Win窗體中的對象

如何減少按鈕的閃爍?

public Form1() 
    { 
     InitializeComponent(); 

     this.DoubleBuffered = true; 
     this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); 
     this.UpdateStyles(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     timer1.Enabled = true; 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     this.DoubleBuffered = true; 
     if (button1.Location.X > 10 && button1.Location.X < 550) 
     { 
      Point osp = new Point(button1.Location.X + 1, button1.Location.Y);     
      button1.Location = osp; 
     } 
     else 
     { 
      Point osp = new Point(11, button1.Location.Y); 
      button1.Location = osp; 
     } 
    } 
+0

穆罕默德的初始化使用它同時刪除從timer1_tick方法下面的一段代碼,我只是想,您張貼相同的代碼和它爲我工作。 您不需要重複的「this.DoubleBuffered = true;」但在Tick事件處理程序中。您已經在初始化代碼中設置了它。我誤解你的問題嗎? –

+0

代碼工作,但移動時物體閃爍。 – Mehmet

+0

像這樣的動畫是一個非常糟糕的想法。移動控制窗口需要父級重新繪製自己,以便重繪以前由控制窗口占據的空間。這需要時間,雙緩衝使其*慢*。通過繪畫動畫,而不是通過移動控件。 –

回答

0

嘗試使用計時器間隔屬性減少每個滴答之間的間隔;

private void Form1_Load(object sender, EventArgs e) 
{ 
    timer1.Enabled = true; 
    timer1.Interval = 400; 
} 

,因爲你已經在形式

this.DoubleBuffered = true; 
+0

感謝阿瓜但不工作 – Mehmet

+0

已更新的答案。 –