SoFunction
Updated on 2025-03-06

C# method to implement winform gradient effect

This example implements a startup screen, using the display Aform. After a while, the method of hiding this Aform, showingdialog the next Bform, and closeAForm is done. I wonder if there is a better way.
The setup program starts from Aform:

Copy the codeThe code is as follows:
static void Main() 

  (); 
  (false); 
  (new Aform()); 
}

The timer is defined in AForm as follows:

StartWindowShowTime    HideWindowStart    HideWindowSpeed   ShowWindowStart

Define their properties:

StartWindowShowTime (displays the length of Aform) Enabled=True Interval=5000 (100=1 second)
HideWindowStart (start the process of hiding Aform) Enabled=True Interval=4500
HideWindowSpeed ​​(hide Aform's gradient interval) Enabled=False Interval=10
ShowWindowStart  (Show the gradient interval of AForm) Enabled=True Interval=10

Let’s start defining these timesticks. You can fill in the Ticks directly in Events. This is the timer, or you can write in the background. However, I think it’s more convenient to fill in here, and you can automatically generate a declaration of the method, so you don’t need to find it. Be lazy.

StartWindowShowTime Tick:ShowMainwindow
HideWindowStart  Tick:HideWindow
HideWindowSpeed  Tick:HideWindowSpeedStart
ShowWindowStart Tick:ShowWindow

Okay, at this point I want to say that the most important member of Windows Form implements transparent effects, gradient effects, and fade effects, that is Opacity in Form attribute, which is used. I have verified that only systems over 2,000 support this attribute.

Let's set Aform's Opacity to 0 first, and then start writing Aform's code

Copy the codeThe code is as follows:
public partial class Aform: Form 

       public Form() 
       { 
           InitializeComponent();  
       } 
 
       private void Start_Load(object sender, EventArgs e) 
       { 
           (); 
           (); 
       } 
 
       private void ShowMainwindow(object sender, EventArgs e) 
       { 
           Bform showmainwindows = new Bform();             
           (); 
           (); 
           (); 
           (); 
           (); 
           (); 
       } 
 
       private void HideWindow(object sender, EventArgs e) 
       { 
           (); 
       } 
 
       private void HideWindowSpeedStart(object sender, EventArgs e) 
       { 
           = - 0.02; 
       } 
 
       private void ShowWindow(object sender, EventArgs e) 
       { 
           if ( == 1) 
           { 
               (); 
           } 
           else 
           { 
               = + 0.02; 
           } 
       } 
}

Okay, at this time, everyone can run it and see that there is a fading effect.
I originally set the value of Opacity to 0.1 each time, but found that if that was the case, the fading was not very smooth, so I reduced the value and interval time. This makes it look much more moist. Feel good about myself.
If your program only needs to be transparent, then you just need to set Opacity.

Gradient and fade in and out can be done by combining timer and Opacity.

I hope this article will be helpful to everyone's C# programming