C#如何实现窗体背景颜色渐变?
实现窗体颜色渐变需要用到Color结构的FormArgb方法,该方法可以重载。
实现的主要代码如下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 窗体背景颜色渐变 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } protected override void OnPaintBackground(PaintEventArgs e) { int intLocation, intHeight;//定义两个int型的变量intLocation、intHeight intLocation = this.ClientRectangle.Location.Y;//为变量intLocation赋值 intHeight = this.ClientRectangle.Height / 200;//为变量intHeight赋值 for (int i = 255; i >= 0; i--) { Color color = new Color();//定义一个Color类型的实例color //为实例color赋值 color = Color.FromArgb(0, i, 0); SolidBrush SBrush = new SolidBrush(color);//实例化一个单色画笔类对象SBrush Pen pen = new Pen(SBrush, 1);//实例化一个用于绘制直线和曲线的对象pen e.Graphics.DrawRectangle(pen, this.ClientRectangle.X, intLocation, this.Width, intLocation + intHeight);//绘制图形 intLocation = intLocation + intHeight;//重新为变量intLocation赋值 } } } }