溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用C#怎么實現(xiàn)一個顏色漸變窗體控件

發(fā)布時間:2021-01-15 15:02:26 來源:億速云 閱讀:403 作者:Leah 欄目:開發(fā)技術

本篇文章為大家展示了利用C#怎么實現(xiàn)一個顏色漸變窗體控件,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

1.建議設置窗體為雙緩沖繪圖,可有效避免界面刷時引起的閃爍

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

2、代碼實現(xiàn)

 private Color Color1 = Color.Gray; //起始顏色
 private Color Color2 = Color.White ; //目標顏色
 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();
}

補充:WPS中 LinearGradientBrush線性漸變的使用

1、顏色列排列

注:

(1)列排列的起始坐標為(0,0.5)終止坐標為(1,0.5)

(2)其中offset放置的位置參數(shù)是需要計算的

例如:一共四個顏色,那么就是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第一個參數(shù)color是設置顏色,第二個參數(shù)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;
    }

上述內(nèi)容就是利用C#怎么實現(xiàn)一個顏色漸變窗體控件,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI