C# 简单按钮动画
我正在努力学习C# 简单按钮动画,c#,.net,animation,button,C#,.net,Animation,Button,我正在努力学习.NET编程。作为我学习的一部分,我试着在按钮上做出一些效果。它正在工作。。。但不像我想象的那么顺利!有没有更好的办法?提前谢谢你 我的需要: 有3个按钮。 当您将鼠标悬停在其中一个按钮上时,它将展开,当您将鼠标从该按钮上移出时,它将返回其初始大小 private void button1_MouseHover(object sender, EventArgs e) { button1.BackColor = Color.White; but
.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;
}
所以首先,你不想做同样的事情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);
}
};
}
修改控件属性时,它会立即生效。你想要的东西通常被称为某种褪色或粗花呢。可能有一些库可以做到这一点,但是如果你想自己写这篇文章来取乐,你可以使用一个定时器对象,并在每个勾号上更新颜色 您要做的是在某处将颜色设置为TargetColor(这是一个变量或由您组成的属性),然后启动一个计时器,该计时器可能每10毫秒发出一次滴答声。在每一个刻度中,你会看到开始时间,以及从那时起经过了多长时间。如果希望动画发生在一整秒的位置,则为1000毫秒。因此,在每次滴答声中,您查看经过的时间量,可能是200毫秒,然后除以200/1000以获得进入动画的时间分数。然后查看开始颜色和目标颜色之间的差异,将该差异乘以分数,然后将结果添加到开始颜色。换言之,动画持续1000毫秒的200毫秒意味着您对动画的参与度为20%。因此,您希望将颜色设置为从开始颜色到结束颜色的20%的任何颜色
你可以做很多来改进这个。可能有一个子类按钮控件,它封装计时器并公开函数/属性以跟踪开始/结束颜色、动画过渡时间等。大多数类似的动画用户界面功能允许您指定动画应持续多长时间,然后在动画过渡时插入中间的状态。这就是术语tweening的由来,因为它来自于通过介于之间的方式从一种状态过渡到另一种状态。如果您使用的是WinForms,动画将非常痛苦,您必须通过
定时器
对象自己处理它们
如果您进入.NET并希望制作具有动画和样式的酷炫应用程序,我强烈建议您使用WPF。通过C#或XAML,它可以非常轻松地制作动画
虽然在WinForms中仍有可能实现,但由于这些功能已经内置到WPF中(并经过优化),因此需要花费更多的开发时间。当你说它没有你想要的那么平滑时,你能解释一下问题是什么吗?@AaronLS嗯,它将从一种状态跳到下一种状态,并且不会在一段时间内缓慢过渡,就像一个真正的“动画”。好吧,你所做的只是设置一个颜色-这里面没有隐含的动画。@AngryHacker他正在改变大小…如果你想要动画(我假设这是WinForms),你将不得不通过定时器对它们进行编码。是的,正是我所想的。我只是在编代码。唯一不同的是,按钮收缩是在另一个计时器上进行的。