2012-12-01 50 views
1

所以,我有一個矩形「矩形1」,在160,160。如何動畫一個簡單的形狀垂直移動?

我希望它移動順利到座標160,30,持續約1秒。 (延時)

我已經想通了,一些基本的代碼到移動形狀是

rectangle1.Location = new Point(160,30); 

然而,當我試圖做一個for循環與

rectangle1.Location = new Point(160, rectangle1.Location.Y - 100); 

它只是搬到那裏即刻。我應該真的期待這一點。同樣發生與

int count = 0; 
while(count != 300) 
{ 
     rectangle1.Location = new Point(160, rectangle1.Location.Y -1); 
     count += 2; 
} 

所以,我想我需要某種形式的時鐘/定時器循環,通過x pixels every x milliseconds移動它。不知道如何做到這一點,所以幫助將不勝感激。


另外,我要在水平方向動畫其他兩個矩形,然後將在同一時間/速度rectangle1向上移動。我想我必須「延遲」矩形1的移動,直到它們到位,對嗎?

謝謝。 PS:我已經使用了一點兒,但是因爲我不完全確定我在找什麼,所以並不是很有成效。

+0

移動cordinates;)上,下,左,右:d用'timer','sleep'或'backgroundworker' :)或者你也可以看看這個[MSDN](http://msdn.microsoft.com/en-us/library/ms752312.aspx)鏈接太 – bonCodigo

回答

3

如果需要流暢的動作,這是偉大的使用定時器,線程,backgroundworkers。

這是你需要做的。假設你有用於增加/減少x,y形狀的代碼的代碼。

步驟:

  • 組定時器間隔爲例如100

  • 設置整數int count = 0; *

  • 在timer_tick事件

    做移動工作

    private void timer1_Tick(object sender, EventArgs e) 
    // no need to use your while loop anymore :)) 
        {  
        If(count< 300) //set to your own criteria 
        { 
        //e.g. myrect.location=new point(x,y); 
        // rectangle1.Location = new Point(160, rectangle1.Location.Y -1);  
        } 
    
        count += 2; 
    } 
    
+0

完美。將它與http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/43daf8b2-67ad-4938-98f7-cae3eaa5e63f相結合,並得到了解決方案。 – JakeSteam

+0

+1這就是精神:)你的聯繫更好。 – bonCodigo

0

你可以做這樣的事情:

public partial class Form1 : Form 
{ 
    private BackgroundWorker worker = null; 
    private Rectangle rect = new Rectangle(0, 0, 100, 100); 
    private Button button1 = new Button(); 
    private TextBox textBox1 = new TextBox(); 

    public Form1() 
    { 
     InitializeComponent(); 

     this.SuspendLayout(); 

     button1.Location = new System.Drawing.Point(260, 171); 
     button1.Name = "button1"; 
     button1.Size = new System.Drawing.Size(75, 23); 
     button1.TabIndex = 0; 
     button1.Text = "button1"; 
     button1.UseVisualStyleBackColor = true; 
     button1.Click += new System.EventHandler(button1_Click); 

     textBox1.Location = new System.Drawing.Point(282, 245); 
     textBox1.Name = "textBox1"; 
     textBox1.Size = new System.Drawing.Size(100, 20); 
     textBox1.TabIndex = 1; 

     this.Controls.Add(this.textBox1); 
     this.Controls.Add(this.button1); 

     this.ResumeLayout(); 

     worker = new BackgroundWorker(); 
     worker.WorkerReportsProgress = true; 
     worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); 
     worker.DoWork += new DoWorkEventHandler(worker_DoWork); 
    } 

    private void worker_DoWork(object sender, DoWorkEventArgs e) 
    { 
     for (int i = 0; i < 100; i++) 
     { 
      rect.Y = i; 
      System.Threading.Thread.Sleep(100); 
      worker.ReportProgress(i); 
     } 
    } 

    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     if (this.InvokeRequired) 
     { 
      Invoke(new ProgressChangedEventHandler(worker_ProgressChanged), new object[] { sender, e }); 
      return; 
     } 

     this.Refresh(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     worker.RunWorkerAsync(); 
    } 

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

     e.Graphics.DrawRectangle(SystemPens.ActiveBorder, rect); 
    } 
} 
+0

那麼,只是凍結了整個窗口的所需時間,直到對象已完成移動。這是一個單線程程序,如果這有所作爲。 – JakeSteam

+0

你想知道如何移動它。不是如何移動它以保持UI的響應。讓我修改你的需求 –

+0

好吧,如果窗口沒有更新,我只能看到對象的原始位置,幾秒鐘內沒有變化,然後是新的位置。爲了澄清,我沒有看到任何流暢的動畫。 – JakeSteam