2011-01-28 15 views
3

작은 이미지를 WinForms pictureBox 컨트롤에로드 한 다음 폼의 반대쪽으로 움직 이도록 애니메이션으로 만듭니다.C#의 이미지 이동

나는 이미지를로드하고 타이머를 사용하여 이미지를 이동 시켰지만, 실행할 때 응용 프로그램은 pictureBox 및 그 이미지의 최종 위치 만 보여줍니다.

이미지를 최종 위치로 부드럽게 전환하는 방법은 무엇입니까? 여기

내 코드는 지금까지 있습니다 :

public partial class Form1 : Form 
{ 
    private int counter = 0; 

    void timer_Tick(object sender, EventArgs e) 
    { 
     counter++; 
     if (counter == 1) 
     { 
      pictureBox1.Show(); 
      timer1.Stop(); 
      counter = 0; 
     } 
    } 

    public Form1() 
    { 
     InitializeComponent(); 

     timer1.Interval = 10; 
     timer1.Tick += new EventHandler(timer_Tick); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 

     while(i<=100){ 

      int x = pictureBox1.Location.X; 
      int y = pictureBox1.Location.Y; 

      pictureBox1.Location = new Point(x+25, y); 
      timer1.Start(); 
     } 
    } 
} 
+2

while (i <= 100) {'어디에 정의되어 있습니까? 루프는 제시된 코드에 따라 절대로 종료되지 않습니다. – Amy

답변

3

이 일을합니까? 죄송합니다, 지금은 어디에 (VS없이 netbook)에 그것을 테스트 할 수 없습니다.

public partial class Form1 : Form 
{ 
    void timer_Tick(object sender, EventArgs e) 
    { 
     int x = pictureBox1.Location.X; 
     int y = pictureBox1.Location.Y; 

     pictureBox1.Location = new Point(x+25, y); 

     if (x > this.Width) 
      timer1.Stop(); 
    } 

    public Form1() 
    { 
     InitializeComponent(); 

     timer1.Interval = 10; 
     timer1.Tick += new EventHandler(timer_Tick); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     pictureBox1.Show(); 
     timer1.Start(); 
    } 
} 
+0

네, 작동합니다 ... 고맙습니다. yodaj007 !!! 하지만 한 가지 더 : 나는 움직이는 이미지의 속도를 줄일 수 있습니까?! –

+1

물론, 픽셀 수를 줄이면 (+25 대신 +15) 사용하거나 간격을 늘리면 (10 대신 20 사용). 또는 둘 다. – quip

+0

도 감사합니다. –