C# 實(shí)現(xiàn)顏色漸變窗體控件詳細(xì)講解
1.建議設(shè)置窗體為雙緩沖繪圖,可有效避免界面刷時(shí)引起的閃爍
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
2、代碼實(shí)現(xiàn)
private Color Color1 = Color.Gray; //起始顏色 private Color Color2 = Color.White ; //目標(biāo)顏色 private float changeAngle = 0f; //漸變角度
3.窗體繪制函數(shù)
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();
}
補(bǔ)充:WPS中 LinearGradientBrush線性漸變的使用
1、顏色列排列
注:
(1)列排列的起始坐標(biāo)為(0,0.5)終止坐標(biāo)為(1,0.5)
(2)其中offset放置的位置參數(shù)是需要計(jì)算的
例如:一共四個(gè)顏色,那么就是1/4=0.25;表示一個(gè)顏色0.25,第一個(gè)顏色為0.25,第二個(gè)就是再加上0.25=0.5,第三個(gè)就是0.75,第四個(gè)就是1
public MainWindow()
{
InitializeComponent();
//實(shí)例化一個(gè)Border控件,來(lái)設(shè)置這個(gè)背景線性漸變
Border bord1 = new Border();
bord1.Width = bord1.Height=200;
indext.Children.Add(bord1);
//線性漸變?cè)O(shè)置開(kāi)始
LinearGradientBrush brush = new LinearGradientBrush();//實(shí)例化線性漸變對(duì)象
//列排列的起始坐標(biāo)為(0,0.5)終止坐標(biāo)為(1,0.5)
brush.StartPoint = new Point(0, 0.5);//設(shè)置線性漸變的二維起始坐標(biāo)
brush.EndPoint=new Point(1,0.5);//設(shè)置線性漸變的二維終止坐標(biāo)
brush.GradientStops.Add(new GradientStop(color: Colors.Pink,offset:0.25));
//GradientStops表示設(shè)置漸變的終止點(diǎn)
//GradientStop第一個(gè)參數(shù)color是設(shè)置顏色,第二個(gè)參數(shù)offset是設(shè)置的位置
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;
//最后將設(shè)置好的漸變背景賦值給Border控件
}
2、顏色行排列
注:
行排列的時(shí)候,起始位置和終止位置只是改變了位置
列排列的起始坐標(biāo)為(0.5,0)終止坐標(biāo)為(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、左上角到右下角斜著排列
注:
如果說(shuō)要斜著排列,那么它的起始位置和終止位置不用設(shè)置計(jì)算,默認(rèn)排列,只需要計(jì)算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;
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- C# 實(shí)現(xiàn)顏色的梯度漸變案例
- C# Color.FromArgb()及系統(tǒng)顏色對(duì)照表一覽
- C# 如何設(shè)置label(標(biāo)簽)控件的背景顏色為透明
- C#使用RichTextBox實(shí)現(xiàn)替換文字及改變字體顏色功能示例
- C#利用Label標(biāo)簽控件模擬窗體標(biāo)題的移動(dòng)及窗體顏色不斷變換效果
- C# 根據(jù)表格偶數(shù)、奇數(shù)加載不同顏色
- C#更改tabControl選項(xiàng)卡顏色的方法
- C#及WPF獲取本機(jī)所有字體和顏色的方法
- C#實(shí)現(xiàn)更改MDI窗體背景顏色的方法
- c# 顏色選擇控件的實(shí)現(xiàn)代碼
相關(guān)文章
Unity計(jì)時(shí)器功能實(shí)現(xiàn)示例
計(jì)時(shí)器在很多地方都可以使用,本文主要介紹了Unity計(jì)時(shí)器功能實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
C#/VB.NET實(shí)現(xiàn)創(chuàng)建PDF/UA文件的示例代碼
PDF/UA,即Universally?Accessible?PDF,該格式的PDF文件是于2012年8月以ISO標(biāo)準(zhǔn)14289-1發(fā)布的、具有普遍可訪問(wèn)的PDF文檔標(biāo)準(zhǔn)。本文將用C#實(shí)現(xiàn)DF/UA文件的創(chuàng)建,需要的可以參考一下2022-08-08
c# 通過(guò)代碼開(kāi)啟或關(guān)閉防火墻
這篇文章主要介紹了c# 通過(guò)代碼開(kāi)啟或關(guān)閉防火墻的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-10-10
在WCF數(shù)據(jù)訪問(wèn)中使用緩存提高Winform字段中文顯示速度的方法
這篇文章主要介紹了在WCF數(shù)據(jù)訪問(wèn)中使用緩存提高Winform字段中文顯示速度的方法,是非常實(shí)用的功能,需要的朋友可以參考下2014-09-09
C#使用timer實(shí)現(xiàn)的簡(jiǎn)單鬧鐘程序
這篇文章主要介紹了C#使用timer實(shí)現(xiàn)的簡(jiǎn)單鬧鐘程序,涉及timer控件的使用及音頻文件的操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
C#/.Net 中快速批量給SQLite數(shù)據(jù)庫(kù)插入測(cè)試數(shù)據(jù)
這篇文章主要介紹了C#/.Net 中快速批量給SQLite數(shù)據(jù)庫(kù)插入測(cè)試數(shù)據(jù),本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-06-06

