简单的按钮动画按钮、简单、动画

2023-09-07 09:00:34 作者:爸气外漏!

我想学习 .NET 编程。作为我学习的一部分,我试图让上的按钮有一定的影响。这是工作......但并不像我想象的那样顺利!有没有更好的方式来做到这一点?预先感谢您!

我需要:

有3个按钮。 当您在其中一个悬停鼠标,它扩展,当你的鼠标从该按钮,返回到其初始大小。

 私人无效button1_MouseHover(对象发件人,EventArgs的)
    {
        button1.BackColor = Color.White;
        button1.Width = 130;
        button1.BringToFront();
    }

    私人无效button1_MouseLeave(对象发件人,EventArgs的)
    {
        button1.BackColor = Color.Red;
        button1.Width = 75;
    }

    私人无效button2_MouseHover(对象发件人,EventArgs的)
    {
        button2.BackColor = Color.Gray;
        button2.Width = 130;
        button2.BringToFront();
    }

    私人无效Form1_MouseLeave(对象发件人,EventArgs的)
    {
        button2.BackColor = Color.Red;
        button2.Width = 75;

    }

    私人无效button3_MouseHover(对象发件人,EventArgs的)
    {
        button3.BackColor = Color.DimGray;
        button3.Width = 130;
        button3.BringToFront();
    }

    私人无效button3_MouseLeave(对象发件人,EventArgs的)
    {
        button3.BackColor = Color.Red;
        button3.Width = 75;

    }
 

解决方案

所以第一关,你不想做同样的事情3倍。创建一个方法来添加相应的处理程序按钮,然后只写了code的一次的处理任何给定的按钮。

巧用HTML5设计按钮背景不同动画特效

请注意,您可以进入扩张/收缩蜱处理程序和使用 percentComplete 值来设置高度为好,移动的颜色沿着光谱(这将涉及的颜色一些数学做虽然)或改变按钮的任何其他方面。如果你的真正的动力去概括它,你可以将参数添加到动作&LT的方法;双> 做一些事,以基于对象在给定的百分比进度。

 公共无效AddAnimation(Button按钮)
{
    VAR expandTimer =新System.Windows.Forms.Timer();
    VAR contractTimer =新System.Windows.Forms.Timer();

    expandTimer.Interval = 10; //可以调节,以确定刷新率
    contractTimer.Interval = 10;

    日期时间animationStarted = DateTime.Now;

    // TODO更新适当或使其参数
    时间跨度animationDuration = TimeSpan.FromMilliseconds(250);
    INT initialWidth = 75;
    INT endWidth = 130;

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

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

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

        如果(percentComplete> = 1)
        {
            expandTimer.Stop();
        }
        其他
        {
            button.Width =(INT)(initialWidth +
                (endWidth  -  initialWidth)* percentComplete);
        }
    };

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

        如果(percentComplete> = 1)
        {
            contractTimer.Stop();
        }
        其他
        {
            button.Width =(INT)(endWidth  - 
                (endWidth  -  initialWidth)* percentComplete);
        }
    };
}
 

I am trying to learn .NET programming. As a part of my learning, I tried to make some effects on buttons. It is working... but not as smooth as I imagined! Is there any better way to do this? Thank you in advance!

My need:

There are 3 buttons. When you hover the mouse over one of them, it expands and when you mouse out from that button, it returns to its initial size.

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;

    }

解决方案

So first off, you don't want to do the exact same thing 3 times. Create a single method to add the appropriate handlers for a button, and then just write the code once to handle any given button.

Note that you can go into the expand/contract tick handlers and use the percentComplete value to set the height as well, to move the color along a spectrum (this would involve some mathematics of colors to do though) or to alter any other aspect of the button. If you're really motivated to generalize it you could add a parameter to the method of Action<double> that does something to the object based on the given percent progress.

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);
        }
    };
}