C# 实现颜色渐变窗体控件详细讲解
作者:牧羊君 时间:2021-12-31 07:10:28
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;
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/muyangjun/article/details/107152970
标签:C#,颜色,渐变,窗体
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java多线程Atomic包操作原子变量与原子类详解
2023-08-18 07:46:25
Java接口的作用_动力节点Java学院整理
2021-12-23 20:46:14
![](https://img.aspxhome.com/file/2023/0/61570_0s.png)
C#中的应用程序接口介绍及实现,密封类与密封方法
2023-10-24 07:37:41
C# PropertyGrid使用案例详解
2022-03-18 20:51:51
![](https://img.aspxhome.com/file/2023/8/80048_0s.png)
探讨Java中的深浅拷贝问题
2023-01-07 01:12:44
![](https://img.aspxhome.com/file/2023/7/72547_0s.jpg)
java实战小技巧之字符串与容器互转详解
2023-09-04 10:56:01
Android开发之自定义加载动画详解
2023-07-27 01:41:05
![](https://img.aspxhome.com/file/2023/0/85980_0s.png)
基于Unity3D实现仿真时钟详解
2021-05-24 15:11:34
![](https://img.aspxhome.com/file/2023/5/79065_0s.gif)
浅谈JVM内存溢出原因和解决思路
2023-11-23 12:24:15
![](https://img.aspxhome.com/file/2023/1/59781_0s.jpg)
Java try-catch-finally异常处理机制详解
2023-10-02 20:29:00
![](https://img.aspxhome.com/file/2023/5/62125_0s.png)
详解spring cloud Feign使用中遇到的问题总结
2023-12-13 19:03:48
Java 实战项目之在线点餐系统的实现流程
2022-08-05 04:11:35
![](https://img.aspxhome.com/file/2023/2/64802_0s.png)
android6.0权限动态申请框架permissiondispatcher的方法
2023-07-31 10:51:57
SpringBoot使用邮箱发送验证码实现注册功能
2022-03-30 10:13:36
Java 中解决Unsupported major.minor version 51.0的问题
2022-07-22 03:53:08
![](https://img.aspxhome.com/file/2023/6/62516_0s.jpg)
C#中面向对象编程机制之多态学习笔记
2023-12-06 20:16:33
C# FileStream简单介绍和使用
2023-07-05 23:04:05
android TextView中识别多个url并分别点击跳转方法详解
2023-06-21 04:42:32
Java实现的并发任务处理实例
2023-05-22 17:51:10
Springboot中Aspect切面的实现方式(以记录日志为例)
2022-09-15 20:58:25