C# WinForm實現(xiàn)窗體漸變色效果的方法步驟
在winform項目開發(fā)中,需要實現(xiàn)漸變色。本文就詳細介紹如何實現(xiàn)。效果如下:

實現(xiàn)方案
利用Color類中FromArgb屬性,通過for循環(huán),修改color顏色,并使用Graphics類繪制
知識點
Color
FromArgb:
public static System.Drawing.Color FromArgb (int red, int green, int blue);
參數(shù):
- red :Int32 新Color的紅色分量值。 有效值為 0 到 255。
- green Int32 新Color的綠色組件值。 有效值為 0 到 255。
- blue Int32 新Color的藍色分量值。 有效值為 0 到 255。
返回
Color
Color color1 = new Color();
for(int i=0;i<=255;i++)
{
color1.FromArgb(1,i,100);
}
SolidBrush
定義單色畫筆。 畫筆用于填充圖形形狀,如矩形、橢圓、扇形、多邊形和封閉路徑。 此類不能被繼承。
SolidBrush(Color) 初始化指定顏色的新 SolidBrush 對象。
Color color1 = new Color();
for(int i=0;i<=255;i++)
{
color1.FromArgb(1,i,100);
SolidBrush SBrush = new SolidBrush(color);//實例化一個單色畫筆類對象SBrush
}
Pen
定義用于繪制直線和曲線的對象。 此類不能被繼承。
Pen(Brush, Single): 用指定的 Pen 和 Color 屬性初始化 Width 類的新實例。
參數(shù)
- brush :Brush.一個 Brush,決定此 Pen 的特征。
- width :Single。新 Pen 的寬度。
Color color1 = new Color();
for(int i=0;i<=255;i++)
{
color1.FromArgb(1,i,100);
SolidBrush SBrush = new SolidBrush(color);//實例化一個單色畫筆類對象SBrush
Pen pen = new Pen(SBrush, 1);//實例化一個用于繪制直線和曲線的對象pen
}
Graphics
封裝一個 GDI+ 繪圖圖面。 此類不能被繼承。
方法
DrawRectangle:繪制由坐標(biāo)對、寬度和高度指定的矩形。
DrawRectangle(Pen, Int32, Int32, Int32, Int32):繪制由坐標(biāo)對、寬度和高度指定的矩形。
參數(shù)
- pen :Pen。確定矩形的顏色、寬度和樣式的 Pen。
- x : Int32,要繪制的矩形左上角的 x 坐標(biāo)。
- y : Int32,要繪制的矩形左上角的 y 坐標(biāo)。
- width: Int32 ,要繪制的矩形的寬度。
- height: Int32.要繪制的矩形的高度。
OnPaintBackground(PaintEventArgs)
Control.OnPaintBackground(PaintEventArgs) 方法
繪制控件的背景。
代碼展示
protected override void OnPaintBackground(PaintEventArgs e)
{
int intLocation, intHeight;//定義兩個int型的變量intLocation、intHeight
intLocation = this.ClientRectangle.Location.Y;//為變量intLocation賦值
intHeight = this.ClientRectangle.Height / 200;//為變量intHeight賦值
for (int i =255; i >= 0; i--)
{
Color color = new Color();//定義一個Color類型的實例color
//為實例color賦值
color = Color.FromArgb(1, i, 100);
SolidBrush SBrush = new SolidBrush(color);//實例化一個單色畫筆類對象SBrush
Pen pen = new Pen(SBrush, 1);//實例化一個用于繪制直線和曲線的對象pen
e.Graphics.DrawRectangle(pen, this.ClientRectangle.X, intLocation, this.Width, intLocation + intHeight);//繪制圖形
intLocation = intLocation + intHeight;//重新為變量intLocation賦值
}
}
到此這篇關(guān)于C# WinForm實現(xiàn)窗體漸變色效果的方法步驟的文章就介紹到這了,更多相關(guān)C# WinForm窗體漸變色內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# Winform 調(diào)用系統(tǒng)接口操作 INI 配置文件的代碼
封裝了一小段代碼, 調(diào)用系統(tǒng)接口, 操作配置文件. 一般用于 .ini 文件, 或者其它鍵值對格式的配置文件2011-05-05
C# http系列之以form-data方式上傳多個文件及鍵值對集合到遠程服務(wù)器
這篇文章主要介紹了C# http系列之以form-data方式上傳多個文件及鍵值對集合到遠程服務(wù)器,需要的朋友可以參考下2019-08-08
C#中控制反轉(zhuǎn)和依賴注入原理及實現(xiàn)
本文深入探討了IoC(控制反轉(zhuǎn))和DI(依賴注入)的概念,及在面向?qū)ο缶幊讨械膽?yīng)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-11-11

