简单的按钮动画

jub*_*man 2 .net c# animation button

我正在努力学习.NET编程.作为我学习的一部分,我试图对按钮产生一些影响.它工作......但不像我想象的那么顺利!有没有更好的方法来做到这一点?先感谢您!

我的需要:

有3个按钮.当您将鼠标悬停在其中一个上时,它会展开,当您从该按钮鼠标移出时,它会返回到其初始大小.

private void button1_MouseHover(object sender, EventArgs e)
    {
        button1.BackColor = Color.White;
        button1.Width = 130;
        button1.BringToFront();            
    }

    private void button1_MouseLeave(object sender, EventArgs e)
    {
        button1.BackColor = Color.Red;
        button1.Width = 75;         
    }

    private void button2_MouseHover(object sender, EventArgs e)
    {
        button2.BackColor = Color.Gray;
        button2.Width = 130;
        button2.BringToFront();            
    }

    private void Form1_MouseLeave(object sender, EventArgs e)
    {
        button2.BackColor = Color.Red;
        button2.Width = 75;

    }

    private void button3_MouseHover(object sender, EventArgs e)
    {
        button3.BackColor = Color.DimGray;
        button3.Width = 130;
        button3.BringToFront();
    }

    private void button3_MouseLeave(object sender, EventArgs e)
    {
        button3.BackColor = Color.Red;
        button3.Width = 75;

    }
Run Code Online (Sandbox Code Playgroud)

Ser*_*rvy 5

所以首先,你不想做同样的事情3次.创建一个方法来为按钮添加适当的处理程序,然后只编写一次代码来处理任何给定的按钮.

请注意,您可以进入扩展/合约刻度处理程序并使用该percentComplete值来设置高度,沿着光谱移动颜色(这将涉及一些颜色的数学要做)或改变任何其他方面的按钮.如果你真的有动机来推广它,你可以在方法中添加一个参数,Action<double>根据给定的百分比进度对对象做一些事情.

public void AddAnimation(Button button)
{
    var expandTimer = new System.Windows.Forms.Timer();
    var contractTimer = new System.Windows.Forms.Timer();

    expandTimer.Interval = 10;//can adjust to determine the refresh rate
    contractTimer.Interval = 10;

    DateTime animationStarted = DateTime.Now;

    //TODO update as appropriate or make it a parameter
    TimeSpan animationDuration = TimeSpan.FromMilliseconds(250);
    int initialWidth = 75;
    int endWidth = 130;

    button.MouseHover += (_, args) =>
    {
        contractTimer.Stop();
        expandTimer.Start();
        animationStarted = DateTime.Now;
        button.BackColor = Color.DimGray;
    };

    button.MouseLeave += (_, args) =>
    {
        expandTimer.Stop();
        contractTimer.Start();
        animationStarted = DateTime.Now;
        button.BackColor = Color.Red;
    };

    expandTimer.Tick += (_, args) =>
    {
        double percentComplete = (DateTime.Now - animationStarted).Ticks
            / (double)animationDuration.Ticks;

        if (percentComplete >= 1)
        {
            expandTimer.Stop();
        }
        else
        {
            button.Width = (int)(initialWidth +
                (endWidth - initialWidth) * percentComplete);
        }
    };

    contractTimer.Tick += (_, args) =>
    {
        double percentComplete = (DateTime.Now - animationStarted).Ticks
            / (double)animationDuration.Ticks;

        if (percentComplete >= 1)
        {
            contractTimer.Stop();
        }
        else
        {
            button.Width = (int)(endWidth -
                (endWidth - initialWidth) * percentComplete);
        }
    };
}
Run Code Online (Sandbox Code Playgroud)