Create a fade mark

This may seem like a simple question ...

I am looking for the Label.Opacity property in C # Winforms.

What I want to do is a method that gradually reduces the label. Perhaps using a timer?

Since there is no opacity, I try to set its transparency to higher numbers until it is high enough so that the element is invisible. But I can not do this job.

I currently 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(); }; } }; } 
+4
source share
4 answers

This is what I use to wipe the labels:

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

This may not be the best solution, but I'm still a beginner, so I can contribute. I put timer1.Interval at a minimum and played with fadingSpeed until it looked good.

+7
source

One of the ways I found to create a smooth fade out is to adjust the ForeColor RGB settings using a timer. This gives you the ability to control the duration and allows you to improve the transition from current ForeColor values โ€‹โ€‹to target values.

 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
source

Does your timer block the user interface thread? If so, you will not see anything until it passes. A quick way to remedy the situation would be to call Application.DoEvents instead of this.Invalidate(); .

+2
source

This is a more complete and elegant solution:

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

using the APCyotek HslColor class: http://cyotek.com/downloads/view/Cyotek.Windows.Forms.ColorPicker.zip/Cyotek.Windows.Forms.ColorPicker/Cyotek.Windows.Forms.ColorPicker/HslColor.cs

I donโ€™t know about the license. I hope you will like it!

+1
source

Source: https://habr.com/ru/post/1345685/


All Articles