简单的按钮动画

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

我的需要:

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

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

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

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

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

如果您使用的是WinForms ,动画将会非常痛苦,您必须通过Timer对象自行处理它们。

如果你正在进入.NET并希望用动画和样式制作看起来很酷的应用程序,我强烈建议你看看WPF 。 它可以通过C#XAML轻松完成动画。

虽然它仍然可以在WinForms中使用,但是由于这些function已经内置到WPF中(并且已经优化),因此需要更多的开发时间。

修改控件属性时,它会立即生效。 你想要的东西通常被称为某种类型的淡化或补间。 可能有库可以做到这一点,但是如果你想自己写这个就好玩,可以使用Timer对象,并在每个tick上更新颜色。

你要做的是将颜色设置为某处的TargetColor(这是你组成的变量或属性),然后启动一个可能每10毫秒滴答一次的计时器。 在每个刻度线中,您可以查看开始时间以及此后经过的时间。 如果您希望动画取代整秒,则为1000毫秒。 因此,在每次打勾期间,您会查看已经过去的时间量,可能是200毫秒,然后除以200/1000以获得您进入动画的时间部分。 然后,您查看“开始”和“目标颜色”之间的差异,将该差异乘以分数,并将结果添加到起始颜色。 换句话说,持续200毫秒的动画200毫秒,意味着你进入动画的20%。 因此,您希望将颜色设置为从开始颜色到结束颜色的任何颜色为20%。

你可以做很多事情来改进它。 也许有一个子类Button控件封装了计时器并公开了函数/属性来跟踪开始/结束颜色,动画转换时间等。这样的大多数动画UIfunction允许你指定动画应该持续多长时间,然后插入inbetween当它转变时陈述。 这是补间这个词的起源,因为它来自于通过中间从一个状态转换到另一个状态