如下图,如何才能实现窗体颜色渐变?
实现窗体颜色渐变需要用到Color结构的FormArgb方法,该方法可以重载。
实现的主要代码如下
using System;using System.Collections.Generic;using ponentModel;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、intHeightintLocation = 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);//实例化一个单色画笔类对象SBrushPen pen = new Pen(SBrush, 1);//实例化一个用于绘制直线和曲线的对象pene.Graphics.DrawRectangle(pen, this.ClientRectangle.X, intLocation, this.Width, intLocation + intHeight);//绘制图形intLocation = intLocation + intHeight;//重新为变量intLocation赋值}}}}
原文地址:/index.php/post/33.html