按钮的动画发光效果 – C#Windows窗体

我想按下按钮时将动画应用到我的按钮。 在WPF中,我可以使用Storyboard和触发器来创建动画。 这是一个例子:

    

和:

    

Windows窗体没有Storyboard和触发器。 如何在Windows窗体中制作流畅的动画?

这是我的Windows窗体代码:

 void DelayTime() { timer = new Timer(); timer.Interval = (int)System.TimeSpan.FromSeconds(this.DelayTime).TotalMilliseconds; timer.Tick += (s, es) => { this.mouseover = false; this.Cursor = Cursors.Hand; this.Enabled = true; }; timer.Start(); } protected override void OnMouseDown(MouseEventArgs mevent) { base.OnMouseDown(mevent); mouseover = true; this.Enabled = false; DelayTime(); } protected override void OnPaint(PaintEventArgs e) { Color bg = this._Background; bg = mouseover ? this._HoverColor : this._Background; e.Graphics.FillRectangle(new SolidBrush(bg), this.ClientRectangle); } 

主要思想是使用计时器并将发光颜色与原始背景颜色混合。

例如,您可以将按钮的FlatStyle设置为Flat并覆盖OnMouseEnterOnMouseLeave 。 在OnMouseEnter启动计时器和in和OnMouseLeave停止计时器。 在计时器Tick事件中,您可以将FlatAppearance of FlatAppearance的按钮的MouseOverBackColor设置为在Tick事件中增加其alpha通道的颜色。

发光按钮

发光按钮代码

 using System; using System.Drawing; using System.Windows.Forms; public class GlowButton : Button { Timer timer; int alpha = 0; public Color GlowColor { get; set; } public GlowButton() { this.DoubleBuffered = true; timer = new Timer() { Interval = 50 }; timer.Tick += timer_Tick; this.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.GlowColor = Color.Gold; this.FlatAppearance.MouseDownBackColor = Color.Gold; } protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); this.FlatAppearance.MouseOverBackColor = CalculateColor(); timer.Start(); } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); timer.Stop(); alpha = 0; this.FlatAppearance.MouseOverBackColor = CalculateColor(); } void timer_Tick(object sender, EventArgs e) { int increament = 25; if (alpha + increament < 255) { alpha += increament; } else { timer.Stop(); alpha = 255; } this.FlatAppearance.MouseOverBackColor = CalculateColor(); } protected override void Dispose(bool disposing) { if (disposing) timer.Dispose(); base.Dispose(disposing); } private Color CalculateColor() { return AlphaBlend(Color.FromArgb(alpha, GlowColor), this.BackColor); } public Color AlphaBlend(Color A, Color B) { var r = (AR * AA / 255) + (BR * BA * (255 - AA) / (255 * 255)); var g = (AG * AA / 255) + (BG * BA * (255 - AA) / (255 * 255)); var b = (AB * AA / 255) + (BB * BA * (255 - AA) / (255 * 255)); var a = AA + (BA * (255 - AA) / 255); return Color.FromArgb(a, r, g, b); } } 

你将获得最接近winforms的可能是DoubleBuffered。 WinForms对动画效果不是很好。

如果您使用的是自定义控件,请在Initialize()下添加;

 SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 

否则,在属性中或通过代码生成DoubleBufferedforms:

 DoubleBuffered = true; 

然后创建动画(如果你还没有想到这一点),做一个循环检查动画是否完成(在计时器事件中)。