简体   繁体   English

带有动画c#的按钮悬停颜色更改

[英]Button Hover Color Change with an animation c#

i'v created a windows form and it has 3 buttons.我创建了一个 Windows 窗体,它有 3 个按钮。 So an one button i figured to change the color with using mouseenter event.因此,我想通过使用 mouseenter 事件来更改颜色的一个按钮。 that's work fine.这很好。 but i need to change the color with transition as fade in or fade out.但我需要将颜色与过渡更改为淡入或淡出。 anyone who have an answer for this question please let me know the below code i'll show you my mouseenter and mouseleave event任何对此问题有答案的人请告诉我下面的代码,我将向您展示我的 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;
}

Here is some code to get you going: It brings in a new Color by blending the alpha channel.这里有一些代码可以帮助您:它通过混合 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 : If you set the newColor to something too dark the last tick line will set the ForeColor to White now.编辑:如果您将 newColor 设置为太暗,则最后一条刻度线现在会将 ForeColor 设置为白色。

Edit 2 : To apply the same animation to several Buttons:编辑 2 :要将相同的动画应用于多个按钮:

  • Add a class variable Button curButton;添加类变量Button curButton;
  • Have the MouseEnter and MouseLeave events of all Buttons point to the very same events above让所有按钮的MouseEnterMouseLeave事件都指向上面相同的事件
  • Add this line at the top of MouseEnter : Button curButton = (Button) sender;MouseEnter的顶部添加这一行: Button curButton = (Button) sender;
  • Change each occurence of button1 to curButton .button1的每次出现更改为curButton

To have an individual new Color for each Button为每个按钮设置一个单独的新颜色

  • Store the newColors it in the Buttons' Tags instead of a class variable:将 newColors 存储在 Buttons 的Tags中,而不是类变量中:

    • button1.Tag = Color.MediumAquamarine;
    • button2.Tag = Color.MediumSeaGreen; //..etc.. //..ETC..
  • Add this to the MouseEnter : newColor = (Color)curButton.Tag;将此添加到MouseEnternewColor = (Color)curButton.Tag; as 2nd line作为第二行

I'm beginning to like the whole thing, though not with black ;-)我开始喜欢整件事情,虽然不是黑色;-)

I wrote a little example for you.我为你写了一个小例子。 Its not perfect, but I think it will work for ya :).它并不完美,但我认为它对你有用:)。

private void button1_MouseEnter(object sender, EventArgs e)
{
    _colorCounter = 250;
    button1.UseVisualStyleBackColor = false;
    //button1.BackColor = Color.Black;
    timer1.Start();
    button1.ForeColor = Color.White;            
}

private void button1_MouseLeave(object sender, EventArgs e)
{
    timer1.Stop();
    _colorCounter = 250;

    button1.UseVisualStyleBackColor = true;
    button1.ForeColor = Color.Black;
    button1.BackColor = SystemColors.Control;

}

private int _colorCounter = 250;

private void timer1_Tick(object sender, EventArgs e)
{
    _colorCounter -= 25;

    if (_colorCounter == 0)
    {
        timer1.Stop();
        _colorCounter = 250;
    }
    else
    {
        // Build up a color from counter
        button1.BackColor = Color.FromArgb(_colorCounter, _colorCounter, _colorCounter);
    }

}

Drag n drop timer into your form.将 n 放置计时器拖放到您的表单中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM