用渐变色填充区域
using System;
using System.Drawing;
using System.Collections;
using ponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
namespace 渐变
... {
/**////<summary>
///Form1的摘要说明。
///</summary>
publicclassForm1:System.Windows.Forms.Form
...{
/**////<summary>
///必需的设计器变量。
///</summary>
ponentModel.Containercomponents=null;
publicForm1()
...{
//
//Windows窗体设计器支持所必需的
//
InitializeComponent();
//
//TODO:在InitializeComponent调用后添加任何构造函数代码
//
}
/**////<summary>
///清理所有正在使用的资源。
///</summary>
protectedoverridevoidDispose(booldisposing)
...{
if(disposing)
...{
if(components!=null)
...{
components.Dispose();
}
}
base.Dispose(disposing);
}
WindowsFormDesignergeneratedcode#regionWindowsFormDesignergeneratedcode
/**////<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
...{
//
//Form1
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(728,494);
this.Name="Form1";
this.Text="Form1";
}
#endregion
/**////<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()
...{
Application.Run(newForm1());
}
protectedoverridevoidOnPaint(PaintEventArgse)
...{
Graphicsg=e.Graphics;
//定义一个线性梯度刷
LinearGradientBrushlGBrush=newLinearGradientBrush(
newPoint(0,10),
newPoint(150,10),
Color.FromArgb(255,255,0,0),
Color.FromArgb(255,0,255,0));
Penpen=newPen(lGBrush);
//用线性梯度刷效果的笔绘制一条直线段,填充一个矩形
g.DrawLine(pen,10,130,500,130);
g.FillRectangle(lGBrush,50,150,370,30);
//定义路径并添加一个椭圆
GraphicsPathgp=newGraphicsPath();
gp.AddEllipse(10,10,200,100);
//用该路径定义路径梯度刷
PathGradientBrushbrush=newPathGradientBrush(gp);
//颜色数组
Color[]colors=...{
Color.FromArgb(255,255,0,0),
Color.FromArgb(255,100,100,100),
Color.FromArgb(255,0,255,0),
Color.FromArgb(255,0,0,255)};
//定义颜色渐变比率
float[]r=...{
0.0F,
0.3F,
0.6F,
1.0F};
ColorBlendblend=newColorBlend();
blend.Colors=colors;
blend.Positions=r;
brush.InterpolationColors=blend;
//在椭圆外填充一个矩形
g.FillRectangle(brush,0,0,210,110);
//用添加了椭圆的路径定义第二个路径梯度刷
GraphicsPathgp2=newGraphicsPath();
gp2.AddEllipse(300,0,200,100);
PathGradientBrushbrush2=newPathGradientBrush(gp2);
//设置中心点的位置和颜色
brush2.CenterPoint=newPointF(450,50);
brush2.CenterColor=Color.FromArgb(255,0,255,0);
//设置边界颜色
Color[]colors2=...{Color.FromArgb(255,255,0,0)};
brush2.SurroundColors=colors2;
//用第二个梯度刷填充椭圆
g.FillEllipse(brush2,300,0,200,100);
}
}
}