【问题标题】:Animated Glow Effect for Button - C# Windows Forms按钮的动画发光效果 - C# Windows 窗体
【发布时间】:2016-12-25 12:09:04
【问题描述】:

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

<Storyboard x:Key="AniOpacityDelay">
  <DoubleAnimation
    Storyboard.TargetName="background"
    Storyboard.TargetProperty="Opacity"
    AutoReverse="True"
    To="0.65"
    Duration="0:0:0.2"/>
</Storyboard>

和:

<ColorAnimationUsingKeyFrames
  Storyboard.TargetName="Itemcont"
  Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)">

  <EasingColorKeyFrame KeyTime="0" Value="#EEEEEE"/>
</ColorAnimationUsingKeyFrames>

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);
}

【问题讨论】:

  • WinForms 在这方面并不擅长。
  • 对于无用的浮华,请继续使用 WPF..
  • 是的,WPF 很好。但问题是我没有一台好的电脑。我可能会问一个关于任务和性能的新问题。@TaW @Maarten
  • 在Windows窗体中为按钮实现平滑的动画发光效果并不难。

标签: c# .net winforms button custom-controls


【解决方案1】:

基本思想是使用Timer 并将发光颜色与原始背景颜色混合。

例如,您可以将按钮的FlatStyle 设置为Flat 并覆盖OnMouseEnterOnMouseLeave。在OnMouseEnter 中启动计时器并在OnMouseLeave 中停止计时器。在计时器Tick 事件中,您可以将按钮的FlatAppearanceMouseOverBackColor 设置为您在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 = (A.R * A.A / 255) + (B.R * B.A * (255 - A.A) / (255 * 255));
        var g = (A.G * A.A / 255) + (B.G * B.A * (255 - A.A) / (255 * 255));
        var b = (A.B * A.A / 255) + (B.B * B.A * (255 - A.A) / (255 * 255));
        var a = A.A + (B.A * (255 - A.A) / 255);
        return Color.FromArgb(a, r, g, b);
    }
}

【讨论】:

  • 我现在使用 wpf.but wow.good idea。我需要注意这一点。+2 如果可以@Reza Aghaei
  • 感谢您的反馈 :) 是的,很好,您也可以在不将样式设置为平面的情况下应用此技术,方法是关闭视觉样式背景颜色并使用计时器更改 BackColor
【解决方案2】:

您将使用 winforms 获得的最接近的可能是使其成为 DoubleBuffered。 WinForms 不太适合动画。

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

SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

否则,请在属性中或通过代码使表单为 DoubleBuffered:

DoubleBuffered = true;

然后创建动画(如果你还没有弄清楚的话),循环检查动画是否完成(在计时器事件中)。

【讨论】:

  • DoubleBuffered = true;在 Form 和 DelayTime 上是 eual 1.2 似乎不错。但仍然很快。@PaddiM8
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多