First create a form FORM1, go to the code page, add a line of code in front
Using System.Drawing.Drawing2D;
Reference Drawing class.
Go back to the Form1 design page, open the Properties page, go to the Events list, find the Paint event, and automatically generate the code for the Paint event.
Write the following code under this event.
private void Form1_paint (object sender, PaintEventArgs e)
{
Graphics g = e.graphics; Instantiating a Graphics object G
Color fcolor = Color.White; Color 1
Color tcolor = Color.Blue; Color 2
Brush B = New LinearGradientBrush (this. ClientRectangle, Fcolor, TColor, lineargradientmode.vertical); Instantiate the brush, the first parameter indicates the shaded area, the second and third parameters are the start and end of the gradient color respectively, and the fourth parameter represents the direction of the color.
G.fillrectangle (b, this.) ClientRectangle); To paint
}
The post-operation effect is as follows.
Similarly, other controls have a paint event, such as a panel, but change the above code to:
Graphics g = e.graphics;
Color fcolor = Color.White;
Color tcolor = Color.Blue;
Brush B = new LinearGradientBrush (Panel1. ClientRectangle, Fcolor, TColor, lineargradientmode.vertical);
G.fillrectangle (b, Panel1. ClientRectangle);
C # Winform using Paint events to achieve gradients