本篇文章为大家展示了利用C#怎么实现一个颜色渐变窗体控件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
private Color Color1 = Color.Gray; //起始颜色
private Color Color2 = Color.White ; //目标颜色
private float changeAngle = 0f; //渐变角度
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)列排列的起始坐标为(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控件
}
注:
行排列的时候,起始位置和终止位置只是改变了位置
列排列的起始坐标为(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;
}
注:
如果说要斜着排列,那么它的起始位置和终止位置不用设置计算,默认排列,只需要计算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#怎么实现一个颜色渐变窗体控件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。