我正在尝试学习.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;
}
答案 0 :(得分: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);
}
};
}
答案 1 :(得分:4)
如果您使用 WinForms ,动画将会非常痛苦,您必须通过Timer
个对象自行处理它们。
如果您要进入.NET并希望使用动画和样式制作外观酷炫的应用程序,我强烈建议您改为使用 WPF 。它可以通过 C#或 XAML 轻松完成动画。
虽然在WinForms中仍然可以使用,但由于这些功能已经内置到WPF中(并且已经优化),因此需要更多的开发时间。
答案 2 :(得分:0)
修改控件属性时,它会立即生效。你想要的东西通常被称为某种类型的淡化或补间。可能有库可以做到这一点,但是如果你想自己写这个就好玩,可以使用Timer对象,并在每个tick上更新颜色。
你要做的是在某处设置一个颜色作为TargetColor(这是你组成的变量或属性),然后启动一个可能每10毫秒滴答一次的计时器。在每个刻度线中,您可以查看开始时间以及此后经过的时间。如果您希望动画取代整秒,则为1000毫秒。因此,在每次打勾期间,您会查看已经过的时间量,可能是200毫秒,然后除以200/1000以获得您进入动画的时间部分。然后,查看“开始”和“目标颜色”之间的差异,将该差异乘以分数,并将结果添加到起始颜色。换句话说,持续200毫秒的动画200毫秒意味着你进入动画的20%。因此,您希望将颜色设置为从开始颜色到最终颜色的任何颜色为20%。
你可以做很多事情来改进它。也许有一个子类Button控件封装了计时器并公开了函数/属性来跟踪开始/结束颜色,动画转换时间等。这样的大多数动画UI功能允许你指定动画应该持续多长时间,然后插入<在转换时强>中间状态。这是补间术语的起源,因为它来自于从一个状态转换到另一个状态