gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:19:17 26 4
gpt4 key购买 nike

我想在按下按钮时为我的按钮应用动画。在 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);
}

最佳答案

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

例如你可以设置FlatStyle按钮的 Flat 并覆盖 OnMouseEnterOnMouseLeave .在 OnMouseEnter 中启动计时器,在 OnMouseLeave 中停止计时器。在计时器 Tick 事件中,您可以设置 MouseOverBackColorFlatAppearance按钮的颜色,您可以在 Tick 中增加它的 alpha channel 事件。

Glow Button

发光按钮代码

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

关于c# - 按钮的动画发光效果 - C# Windows 窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39020262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com