使用mouseEnter事件缩放按钮

时间:2012-01-21 01:57:10

标签: c# visual-studio-2010 rollover-effect

这是我在这里的第一篇文章。首先,一些背景,我是C#的新手,而且我的确处于初期阶段。我是一名化学工程师,大多数都有MATLAB和Mathematica等语言的经验,但我一直很喜欢编程,并决定学习C#为我一直使用的一些程序创建一些用户友好的界面。请注意,我使用的是Windows窗体,而不是WPF。

我想做的是有一个链接到各种表格的主菜单屏幕。现在让它看起来更好,我想做的就是这个;当我将鼠标悬停在主窗口的一个图片框(图片是一个按钮)上时,我希望按钮能够“增长”一点,然后当我离开时它应该“缩小”到原来的大小。到目前为止,我的方法是尝试在mouseEnter事件上加载这个增长动画的gif,然后在mouseLeave上加载一个缩小动画,但这只是反复循环各自的gif。我怎么才能让gif只玩一次?

我尝试按顺序加载帧,并在它们之间进行线程休眠,但是当我这样做时,我看到的是我尝试加载的最后一个图像。这是一个用于此方法的代码示例,其中我尝试显示一个图像,然后在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循环来增加按钮或图片框的大小?

编辑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 个答案:

答案 0 :(得分: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();
    }
}

编辑:当您将鼠标悬停在PictureBox上时,更改了代码以添加动画。当你留下来的时候,动画会留在最后一帧。当鼠标离开时,它将重置为第1帧。您可以使用任意数量的帧。您还应该处理MouseDown和MouseUp,并再创建一个图像以显示抑郁或“向下”状态。