利用c#怎么实现一个颜色渐变窗体控件

  介绍

本篇文章为大家展示了利用c#怎么实现一个颜色渐变窗体控件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1。建议设置窗体为双缓冲绘图,可有效避免界面刷时引起的闪烁

this.SetStyle (ControlStyles.AllPaintingInWmPaint  |, ControlStyles.OptimizedDoubleBuffer,,真的),

2代码实现

, private  Color  Color1 =, Color.Gray;,//起始颜色   ,private  Color  Color2 =, Color.White ,,//目标颜色   ,private  float  changeAngle =, 0 f;,,,,//渐变角度

3。窗体绘制函数

, private  void  Form1_Paint (object ,发送方,PaintEventArgs  e)   ,{   ,,,,,Graphics  g =, e.Graphics;   ,,,,,Rectangle  grounRect =, new 矩形(0,0,上,,this.Height);   ,,,,,System.Drawing.Drawing2D.LinearGradientBrush  backGround =, new  System.Drawing.Drawing2D.LinearGradientBrush (Color1, grounRect,还以为,Color2, changeAngle);   ,,,,,g.FillRectangle(背景,grounRect);   ,,,,,backGround.Dispose ();   }

<>强补充:WPS中LinearGradientBrush线性渐变的使用

1,颜色列排列

注:

(1)列排列的起始坐标为(0,- 0.5)终止坐标为(0.5)

(2)其中抵消放置的位置参数是需要计算的

例如:一共四个颜色,那么就是1/4=0.25;表示一个颜色0.25,第一个颜色为0.25,第二个就是再加上0.25=0.5,第三个就是0.75,第四个就是1

public 主窗口()   ,,,{   ,,,,,InitializeComponent ();//才能实例化一个边境控件,来设置这个背景线性渐变   ,,,,,Border  bord1 =, new 边界();   ,,,,,bord1.Width =, bord1.Height=200;   ,,,,,indext.Children.Add (bord1);//线才能性渐变设置开始   ,,,,,LinearGradientBrush  brush =, new  LinearGradientBrush();//实例化线性渐变对象//列才能排列的起始坐标为(0,- 0.5)终止坐标为(0.5)   ,,,,,brush.StartPoint =, new 点(0,0.5);//设置线性渐变的二维起始坐标   ,,,,,brush.EndPoint=new 点(0.5);//设置线性渐变的二维终止坐标   ,,,,,brush.GradientStops.Add (new  GradientStop(颜色:,Colors.Pink,抵消:0.25));//GradientStops才能表示设置渐变的终止点//才能GradientStop第一个参数颜色是设置颜色,第二个参数偏移是设置的位置   ,,,,,brush.GradientStops.Add (new  GradientStop(颜色:,Colors.IndianRed,抵消:0.50));   ,,,,,brush.GradientStops.Add (new  GradientStop(颜色:,Colors.LightSteelBlue,抵消:0.75));   ,,,,,brush.GradientStops.Add (new  GradientStop(颜色:,Colors.LightSeaGreen,抵消:1.0));   ,,,,,bord1.Background =,刷;//最才能后将设置好的渐变背景赋值给边境控件   ,,,}

2,颜色行排列

注:

行排列的时候,起始位置和终止位置只是改变了位置

列排列的起始坐标为(0.5,0)终止坐标为(0.5,1)

public 主窗口()   ,,,{   ,,,,,InitializeComponent ();   ,,,,,Border  bord1 =, new 边界();   ,,,,,bord1.Width =, bord1.Height=200;   ,,,,,indext.Children.Add (bord1);   ,,,,,LinearGradientBrush  brush =, new  LinearGradientBrush ();   ,,//颜色行排列位置改变   ,,,,,brush.StartPoint =, new 点(0.5,0);   ,,,,,brush.EndPoint=new 点(0.5,1);   ,,,,,brush.GradientStops.Add (new  GradientStop(颜色:,Colors.Pink,抵消:0.25));   ,,,,,brush.GradientStops.Add (new  GradientStop(颜色:,Colors.IndianRed,抵消:0.50));   ,,,,,brush.GradientStops.Add (new  GradientStop(颜色:,Colors.LightSteelBlue,抵消:0.75));   ,,,,,brush.GradientStops.Add (new  GradientStop(颜色:,Colors.LightSeaGreen,抵消:1.0));   ,,,,,bord1.Background =,刷;   ,,,}

3,左上角到右下角斜着排列

注:

如果说要斜着排列,那么它的起始位置和终止位置不用设置计算,默认排列,只需要计算抵消的位置大小

, public 主窗口()   ,,,{   ,,,,,InitializeComponent ();   ,,,,,Border  bord1 =, new 边界();   ,,,,,bord1.Width =, bord1.Height=200;   ,,,,,indext.Children.Add (bord1);   ,,,,,LinearGradientBrush  brush =, new  LinearGradientBrush ();   null   null   null   null   null   null

利用c#怎么实现一个颜色渐变窗体控件