欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C# 实现颜色渐变窗体控件详细讲解

时间:2021-04-16 08:23:41|栏目:.NET代码|点击:

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

this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true); 

2、代码实现

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

3.窗体绘制函数

 private void Form1_Paint(object sender, PaintEventArgs e)
 {
      Graphics g = e.Graphics;
      Rectangle grounRect = new Rectangle(0, 0, this.Width, this.Height);
      System.Drawing.Drawing2D.LinearGradientBrush backGround = new System.Drawing.Drawing2D.LinearGradientBrush(grounRect, Color1, Color2, changeAngle);
      g.FillRectangle(backGround, grounRect);
      backGround.Dispose();
}

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

1、颜色列排列

注:

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

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

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

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

2、颜色行排列

注:

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

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

public MainWindow()
    {
      InitializeComponent();
      Border bord1 = new Border();
      bord1.Width = bord1.Height=200;
      indext.Children.Add(bord1);
      LinearGradientBrush brush = new LinearGradientBrush();
   //颜色行排列位置改变
      brush.StartPoint = new Point(0.5,0);
      brush.EndPoint=new Point(0.5,1);
      brush.GradientStops.Add(new GradientStop(color: Colors.Pink,offset:0.25));
      brush.GradientStops.Add(new GradientStop(color: Colors.IndianRed,offset:0.50));
      brush.GradientStops.Add(new GradientStop(color: Colors.LightSteelBlue,offset:0.75));
      brush.GradientStops.Add(new GradientStop(color: Colors.LightSeaGreen,offset:1.0));
      bord1.Background = brush;
    }

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

注:

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

 public MainWindow()
    {
      InitializeComponent();
      Border bord1 = new Border();
      bord1.Width = bord1.Height=200;
      indext.Children.Add(bord1);
      LinearGradientBrush brush = new LinearGradientBrush();
      brush.GradientStops.Add(new GradientStop(color: Colors.Pink,offset:0.25));
      brush.GradientStops.Add(new GradientStop(color: Colors.IndianRed,offset:0.50));
      brush.GradientStops.Add(new GradientStop(color: Colors.LightSteelBlue,offset:0.75));
      brush.GradientStops.Add(new GradientStop(color: Colors.LightSeaGreen,offset:1.0));
      bord1.Background = brush;
    }

上一篇:C#位运算以及实例计算详解

栏    目:.NET代码

下一篇:Unity3D实现攻击范围检测

本文标题:C# 实现颜色渐变窗体控件详细讲解

本文地址:http://www.codeinn.net/misctech/102207.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有