【问题标题】:Creating a fade out label创建淡出标签
【发布时间】:2011-07-25 03:59:00
【问题描述】:

这似乎是一个简单的问题......

我正在 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();
            };
        }
    };
}

【问题讨论】:

  • 如果可以,请显示 MyTimer 类。
  • 无关紧要。我发现默认情况下无法在 winforms 标签上使用透明度。
  • 顺便说一句,要使标签不可见,将其颜色设置为背景颜色就足够了,而不是摆弄alpha通道。

标签: c# winforms label opacity fadeout


【解决方案1】:

这是我用来淡出标签的方法:

    label1.Text = "I'm fading out now";
    label1.ForeColor = Color.Black;
    timer1.Start();

    private void timer1_Tick(object sender, EventArgs e)
    {
        int fadingSpeed = 3;
        label1.ForeColor = Color.FromArgb(label1.ForeColor.R + fadingSpeed, label1.ForeColor.G + fadingSpeed, label1.ForeColor.B + fadingSpeed);

        if (label1.ForeColor.R >= this.BackColor.R)
        {
            timer1.Stop();
            label1.ForeColor = this.BackColor;
        }
    }

也许不是最好的解决方案,但我仍然是初学者,所以这是我可以贡献的。我把timer1.Interval 至少放在了fadingSpeed 上,直到它看起来不错。

【讨论】:

  • 对于不熟悉计时器的人,请确保在 Start() 之前添加:System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();timer1.Tick += new EventHandler(timer1_Tick);
【解决方案2】:

我发现创建平滑淡入淡出的一种方法是使用计时器调整 ForeColor RGB 变量。这使您可以控制持续时间,并允许您微调从当前 ForeColor 值到目标值的过渡。

private void timer1_Tick(object sender, EventArgs e)
    {
        // timer interval set to 10 to ensure smooth fading
        // declared int: r = 0, g = 215 and b = 180
        // target values are r = 32, g = 32 and b = 32 to match BackColor
        fade++;
        if (fade >= 500) // arbitrary duration set prior to initiating fade
        {
            if (r < 32) r++; // increase r value with each tick
            if (g > 32) g--; // decrease g value with each tick
            if (b > 32) b--; // decrease b value with each tick
            label1.ForeColor = Color.FromArgb(255, r, g, b);
            if (r == 32 && g == 32 && b == 32) // arrived at target values
            {
                // fade is complete - reset variables for next fade operation
                label1.ForeColor = Color.FromArgb(255, 0, 215, 180);
                label1.Text = "";
                fade = 0;
                r = 0;
                g = 215;
                b = 180;
                timer1.Enabled = false;
            }
        }
    }

【讨论】:

    【解决方案3】:

    您的计时器是否阻塞了 UI 线程?如果是这样,在它过去之前你什么都看不到。解决问题的快速方法是调用Application.DoEvents 而不是this.Invalidate();

    【讨论】:

    • 实际上.. 在我当前的代码中,第二个计时器已启动,但代码未达到刻度。 “MyTimer”类是倒计时类
    【解决方案4】:

    这是一个更完整和优雅的解决方案:

    // Aufruf der Methode aus dem Worker-Thread
    private void OnFadeTimerEvent(object sender, ElapsedEventArgs e)
    {
        this.Invoke(new Action(() => FadeOutLabel()));
    }
    
    private void FadeOutLabel()
    {
        if (labelStartHelp.ForeColor.GetBrightness() <= 0.01)
        {
            FadeTimer.Enabled = false;
            labelStartHelp.Visible = false;
            return;
        }
        HslColor hsl = new HslColor(labelStartHelp.ForeColor);
        hsl.L -= 0.002; // Brightness is here lightness
        labelStartHelp.ForeColor = (System.Drawing.Color)hsl.ToRgbColor();
    }
    

    使用 APCyotek HslColor 类:http://cyotek.com/downloads/view/Cyotek.Windows.Forms.ColorPicker.zip/Cyotek.Windows.Forms.ColorPicker/Cyotek.Windows.Forms.ColorPicker/HslColor.cs

    虽然不知道许可证。希望你喜欢!

    【讨论】:

    • 我认为使用“invoke”是个好主意。我们可以用 RGBA 颜色而不是 HSL 调整 Alpha 通道吗?
    【解决方案5】:

    这是我写的。它还处理不同颜色的标签(例如绿色、红色)

    实例化类,随时附加到标签上。设置好消息和初始前景色后,再调用 setColorSteps 和你想要多少步。

    当你想运行淡入淡出时,调用 doFade()

    用于计时器循环事件的 OP 道具,我在编写此解决方案时使用了它,该解决方案适用于我需要的东西。

    /// <summary>
    /// Fade a label - Derek Piper 2019
    /// </summary>
    public class LabelFader
    {
        Label label;
        List<ColorStep> colorSteps = new List<ColorStep>();
    
        public void attachToControl(Label useLabel)
        {
            this.label = useLabel;
        }
    
        public void setColorSteps(int numSteps)
        {
            if (this.label != null)
            {
                ColorStep start = new ColorStep(this.label.ForeColor);
                ColorStep end = new ColorStep(this.label.BackColor);
                int redIncrement = ((end.R - start.R) / numSteps);
                int greenIncrement = ((end.G - start.G) / numSteps);
                int blueIncrement = ((end.B - start.B) / numSteps);
                this.colorSteps = new List<ColorStep>();
                for (int i = 0; i <= numSteps; i++)
                {
                    ColorStep newStep = new ColorStep();
                    if (redIncrement > 0)
                    {
                        newStep.R = start.R + (i * redIncrement);
                    }
                    else
                    {
                        newStep.R = start.R - (i * redIncrement);
                    }
                    if (greenIncrement > 0)
                    {
                        newStep.G = start.G + (i * greenIncrement);
                    }
                    else
                    {
                        newStep.G = start.G - (i * greenIncrement);
                    }
                    if (blueIncrement > 0)
                    {
                        newStep.B = start.B + (i * blueIncrement);
                    }
                    else
                    {
                        newStep.B = start.B - (i * blueIncrement);
                    }
                    this.colorSteps.Add(newStep);
                }
            }
        }
    
        public void doFade(int speedMs = 40)
        {
            Timer tm = new Timer();
            tm.Interval = speedMs;
            int step = 0;
            int end = this.colorSteps.Count;
            if (this.colorSteps.Count > 0)
            {
                tm.Tick += (sa, aea) =>
                {
                    ColorStep thisStep = this.colorSteps[step];
                    this.label.ForeColor = Color.FromArgb(thisStep.R, thisStep.G, thisStep.B);
                    step++;
                    if (step >= end)
                    {
                        tm.Stop();
                        this.label.Visible = false;
                    }
                };
                tm.Start();
            }
        }
    }
    
    class ColorStep
    {
        public int R = 0;
        public int G = 0;
        public int B = 0;
    
        public ColorStep()
        {
        }
    
        public ColorStep(Color from)
        {
            this.setFromColor(from);
        }
    
        public void setFromColor(Color from)
        {
            this.R = from.R;
            this.G = from.G;
            this.B = from.B;
        }
    
        public Color getColor()
        {
            return Color.FromArgb(this.R, this.G, this.B);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 2010-11-28
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 2021-04-10
      相关资源
      最近更新 更多