创建淡出标签
这似乎是一个简单的问题...
我正在寻找 C# Winforms 中的 Label.Opacity 属性。
我想做的是有一种方法可以逐渐淡出标签。也许通过计时器?
由于没有不透明度,我尝试将其透明度设置为更高的数字,直到它足够高以使该项目不可见。但我似乎无法完成这项工作。
目前我有:
public FadeLabel()
{
MyTimer timer = new MyTimer();
this.TextChanged += (s, ea) =>
{
if (timer.IsActive)
{
timer.Reset();
}
else
{
timer.WaitTime.Miliseconds = 500;
timer.Start();
timer.Completed += (a) =>
{
int i = 0;
Timer tm = new Timer();
tm.Interval = 1;
tm.Tick += (sa, aea) =>
{
i++;
this.ForeColor = Color.FromArgb(i, Color.Black);
this.BackColor = Color.FromArgb(i, Color.White);
this.Invalidate();
if (i == 255)
{
tm.Stop();
}
};
tm.Start();
};
}
};
}
This might seem like a simple question...
I'm looking for the Label.Opacity property in C# Winforms.
What I wish to do is have a method that fade's out a label gradually. By means of a timer perhaps?
Since there is not Opacity I'm trying to set it's transperency to higher numbers untill it's high enough that the item should be invisible. But I can't seem to make this work.
Currently I have:
public FadeLabel()
{
MyTimer timer = new MyTimer();
this.TextChanged += (s, ea) =>
{
if (timer.IsActive)
{
timer.Reset();
}
else
{
timer.WaitTime.Miliseconds = 500;
timer.Start();
timer.Completed += (a) =>
{
int i = 0;
Timer tm = new Timer();
tm.Interval = 1;
tm.Tick += (sa, aea) =>
{
i++;
this.ForeColor = Color.FromArgb(i, Color.Black);
this.BackColor = Color.FromArgb(i, Color.White);
this.Invalidate();
if (i == 255)
{
tm.Stop();
}
};
tm.Start();
};
}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是我用来淡出标签的方法:
也许不是最好的解决方案,但我仍然是初学者,所以这就是我可以做出的贡献。我将
timer1.Interval
设置为最小值,并使用fadingSpeed
进行播放,直到看起来不错为止。This is what I'm using to fade out labels:
Maybe not the best solution but I'm still a beginner so this is what I can contribute with. I put
timer1.Interval
at minimum and played withfadingSpeed
until it looked good.我发现创建平滑淡入淡出效果的一种方法是使用计时器调整 ForeColor RGB 变量。这使您可以控制持续时间,并允许您巧妙地从当前前景色值到目标值的过渡。
One way that I've found to create a smooth fade is to adjust the ForeColor RGB variables using a timer. This gives you control over the duration and allows you to finesse the transition from current ForeColor values to the target values.
你的计时器会阻塞 UI 线程吗?如果是这样,直到它过去之后你才会看到任何东西。解决问题的快速方法是调用 < code>Application.DoEvents 而不是
this.Invalidate();
。Does your timer block the UI thread? If so, you won't see anything until after it elapses. A quick way to fix things will be to call
Application.DoEvents
instead ofthis.Invalidate();
.这是一个更完整和优雅的解决方案:
使用 APCyotek HslColor 类: http://cyotek.com/downloads/view/Cyotek.Windows.Forms.ColorPicker.zip/Cyotek.Windows.Forms.ColorPicker/Cyotek.Windows .Forms.ColorPicker/HslColor.cs
但不知道许可证。希望你喜欢它!
This is a more complete and elegant solution:
using APCyotek HslColor class: http://cyotek.com/downloads/view/Cyotek.Windows.Forms.ColorPicker.zip/Cyotek.Windows.Forms.ColorPicker/Cyotek.Windows.Forms.ColorPicker/HslColor.cs
dont know about license though. Hope u like it!
这是我写的。它还处理不同颜色的标签(例如绿色、红色)
实例化类,随时附加到标签。设置消息和初始前景色后,调用 setColorSteps 并确定所需的步数。
当您想要运行淡入淡出时,请
为计时器循环事件调用 OP 的 doFade() Props,我在编写适合我需要的解决方案时使用了它。
Here's what I wrote. It also handles labels of different colors (e.g. green, red)
Instance the class, attach to the label any time. After you set the message and the initial foreground color, then call setColorSteps and how many steps you want.
When you want to run the fade, call doFade()
Props to OP for the timer loop event, which I used when I wrote this solution that worked for what I needed.