我创建了一个 Windows 窗体,它有 3 个按钮。因此,我想通过使用 mouseenter 事件来更改颜色的一键式按钮。工作正常。但我需要通过淡入或淡出的过渡来改变颜色。任何对此问题有答案的人请告诉我下面的代码,我将向您展示我的 mouseenter 和 mouseleave 事件
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.UseVisualStyleBackColor = false;
button1.BackColor = Color.Black;
button1.ForeColor = Color.White;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.UseVisualStyleBackColor = true;
button1.ForeColor = Color.Black;
}
这里有一些代码可以帮助您继续:它通过混合 Alpha 通道引入新的颜色。
public Form1()
{
InitializeComponent();
oldColor = button1.BackColor;
}
Color oldColor;
Color newColor = Color.FromArgb(0, Color.MediumAquamarine); // your pick, including Black
int alpha = 0;
private void button1_MouseEnter(object sender, EventArgs e)
{
alpha = 0;
timer1.Interval = 15;
timer1.Start();
}
private void button1_MouseLeave(object sender, EventArgs e)
{
timer1.Stop();
button1.BackColor = oldColor;
button1.ForeColor = Color.Black;
}
private void timer1_Tick(object sender, EventArgs e)
{
alpha += 17; // change this for greater or less speed
button1.BackColor = Color.FromArgb(alpha, newColor);
if (alpha >= 255) timer1.Stop();
if (button1.BackColor.GetBrightness() < 0.3) button1.ForeColor = Color.White;
}
Edit:如果将 newColor 设置为太暗,则最后一条刻度线现在会将 ForeColor 设置为白色。
Edit 2:应用相同的动画several纽扣:
- 添加类变量
Button curButton;
- 有
MouseEnter
and MouseLeave
所有按钮的事件都指向上面完全相同的事件
- 在顶部添加这一行
MouseEnter
: Button curButton = (Button) sender;
- 更改每次出现的
button1
to curButton
.
拥有一个个性新色对于每个按钮
我开始喜欢这一切,尽管不是黑色;-)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)