C # Winform using Paint events to achieve gradients

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.