2012-01-21 45 views
2

這是我的第一篇文章。首先,我介紹一些背景知識,我是C#的新手,並且真正處於我的初級階段。我是一名化學工程師,大部分都有MATLAB和Mathematica等語言的使用經驗,但我一直很喜歡編程,並決定學習C#爲我一直在使用的某些程序創建一些用戶友好的界面。請注意,我使用Windows窗體而不是WPF。使用mouseEnter事件來縮放按鈕

我想要做的是有一個主菜單屏幕鏈接到各種形式。現在爲了讓它看起來更漂亮,我想要做的是這個;當我將鼠標懸停在主窗口中的一個圖片框(圖片是一個按鈕)時,我希望按鈕有點「長大」,然後當我離開時,它應該縮小到原始大小。到目前爲止,我的方法是嘗試在mouseEnter事件上加載這個增長動畫的gif,然後在mouseLeave上加載一個縮小動畫,但這只是反覆循環各自的gif。我怎樣才能讓gif只玩一次?

我試圖按順序加載幀,並在它們之間插入一個線程睡眠,但是當我這樣做的時候我看到的是我嘗試加載的最後一個圖像。這裏的用於該方法,其中i嘗試顯示一個圖像的代碼的一個例子,然後0.1秒

private void pictureBox1_MouseEnter(object sender, EventArgs e) 
    { 
     pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; 
     ((PictureBox)sender).Image =Image.FromFile("C:/Users/michael/Desktop/131.bmp"); 

     Thread.Sleep(100); 
     ((PictureBox)sender).Image = Image.FromFile("C:/Users/michael/Desktop/131a.bmp"); 
    } 

而且是有一種方法可以做到這一點沒有一個GIF,諸如通過使用一個後示出另一for循環來增加按鈕或picturebox的大小?

編輯1: 我在哪裏把計時器停在這樣,當我開始第二個動畫,第一個停止?

 private void pictureBox1_MouseEnter(object sender, EventArgs e) 
     { 
     pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; 
     Timer timeraf = new Timer(); 
     timeraf.Interval = 10; 
     timeraf.Tick += new EventHandler(timerAfwd_Tick); 
     timeraf.Start(); 
    } 

    private void pictureBox1_MouseLeave(object sender, EventArgs e) 
    { 

     pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; 
     Timer timerab = new Timer(); 
     timerab.Interval = 10; 
     timerab.Tick += new EventHandler(timerAbwd_Tick); 
     timerab.Start(); 
    } 

回答

1

如果你把你的主線程睡眠,我敢肯定它都鎖了用戶輸入,則它會阻塞處理塗裝線。所以你的圖片不能繪製任何動畫。嘗試使用像這樣的計時器:

public partial class Animation : Form 
{ 
    Image img1 = Properties.Resources.img1; 
    Image img2 = Properties.Resources.img2; 
    Image img3 = Properties.Resources.img3; 

    List<Image> images = new List<Image>(); 
    Timer timer = new Timer(); 

    public Animation() 
    { 
     InitializeComponent(); 

     timer.Interval = 250; 
     timer.Tick += new EventHandler(timer_Tick); 

     images.Add(img1); 
     images.Add(img2); 
     images.Add(img3); 

     animated_pbx.Image = img1; 
    } 

    private void timer_Tick(object sender, EventArgs e) 
    { 
     if (this.InvokeRequired) 
     { 
      this.BeginInvoke(new EventHandler(timer_Tick)); 
     } 
     else 
     { 
      // Loop through the images unless we've reached the final one, in that case stop the timer 
      Image currentImage = animated_pbx.Image; 
      int currentIndex = images.IndexOf(currentImage); 

      if (currentIndex < images.Count - 1) 
      { 
       currentIndex++; 

       animated_pbx.Image = images[currentIndex]; 

       animated_pbx.Invalidate(); 
      } 
      else 
      { 
       timer.Stop(); 
      } 
     } 
    } 

    private void animated_pbx_MouseEnter(object sender, EventArgs e) 
    { 
     timer.Start(); 
    } 

    private void animated_pbx_MouseLeave(object sender, EventArgs e) 
    { 
     timer.Stop(); 
     animated_pbx.Image = img1; 
     animated_pbx.Invalidate(); 
    } 
} 

編輯:更改代碼以將鼠標懸停在圖片框上時添加一個動畫。動畫將留在最後一幀,而你保持徘徊。當鼠標離開時,它將重置爲第一幀。您可以使用任意數量的幀。您還應該處理MouseDown和MouseUp,並創建另外1張圖像來顯示壓低或「關閉」狀態。

+0

感謝,作品像一個魅力!沒有足夠的代表給你豎起大拇指,但是! :) –

+0

沒問題。此處的慣例是通過單擊答案左側的小箭頭來選擇「已接受」答案。 :) –

+0

請看我編輯的問題 –