2012-01-21 56 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

请看我编辑的问题 –