C#實(shí)現(xiàn)ComboBox變色的示例代碼
更新時(shí)間:2023年01月05日 09:58:23 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)ComboBox變色的效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實(shí)踐過(guò)程
效果

代碼
public partial class B_ComboBox : ComboBox
{
public B_ComboBox()
{
InitializeComponent();
this.DrawMode = DrawMode.OwnerDrawFixed;
//this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ListBox_DrawItem);
}
#region 變量
private static Brush[] listBoxBrushes; //該數(shù)組用來(lái)存儲(chǔ)繪制listBox1背景的Brush對(duì)象
private static int place = -1; //顏色位置的取值
private static bool naught = true; //判斷是否重繪
#endregion
#region 屬性
private bool TGradualC = false;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("判斷是否進(jìn)行漸變色的設(shè)置")] //在“屬性”窗口中顯示DataStyle屬性
public bool GradualC
{
get { return TGradualC; }
set
{
TGradualC = value;
this.Invalidate();
}
}
private Color TColorSelect = Color.Gainsboro;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("項(xiàng)被選中后的高亮度顏色")] //在“屬性”窗口中顯示DataStyle屬性
public Color ColorSelect
{
get { return TColorSelect; }
set
{
TColorSelect = value;
this.Invalidate();
}
}
private Color TColor1 = Color.CornflowerBlue;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("第一個(gè)顏色的設(shè)置")] //在“屬性”窗口中顯示DataStyle屬性
public Color Color1
{
get { return TColor1; }
set
{
TColor1 = value;
this.Invalidate();
}
}
private Color TColor1Gradual = Color.Thistle;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("第一個(gè)顏色的漸變色設(shè)置")] //在“屬性”窗口中顯示DataStyle屬性
public Color Color1Gradual
{
get { return TColor1Gradual; }
set
{
TColor1Gradual = value;
this.Invalidate();
}
}
private Color TColor2 = Color.PaleGreen;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("第二個(gè)顏色的設(shè)置")] //在“屬性”窗口中顯示DataStyle屬性
public Color Color2
{
get { return TColor2; }
set
{
TColor2 = value;
this.Invalidate();
}
}
private Color TColor2Gradual = Color.DarkKhaki;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("第二個(gè)顏色的漸變色設(shè)置")] //在“屬性”窗口中顯示DataStyle屬性
public Color Color2Gradual
{
get { return TColor2Gradual; }
set
{
TColor2Gradual = value;
this.Invalidate();
}
}
#endregion
private void B_ComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
Rectangle r = new Rectangle(0, 0, this.Width, this.Height); //設(shè)置重繪的區(qū)域
SolidBrush SolidB1 = new SolidBrush(this.Color1); //設(shè)置上一行顏色
SolidBrush SolidB2 = new SolidBrush(this.Color2); //設(shè)置下一行顏色
//設(shè)置上一行的漸變色
LinearGradientBrush LinearG1 = new LinearGradientBrush(r, this.Color1, this.Color1Gradual,
LinearGradientMode.BackwardDiagonal);
//設(shè)置下一行的漸變色
LinearGradientBrush LinearG2 = new LinearGradientBrush(r, this.Color2, this.Color2Gradual,
LinearGradientMode.BackwardDiagonal);
//將單色與漸變色存入Brush數(shù)組中
listBoxBrushes = new Brush[] {SolidB1, LinearG1, SolidB2, LinearG2};
if (this.Items.Count <= 0) //如果當(dāng)前控件為空
return;
if (e.Index == (this.Items.Count - 1)) //如果繪制的是最后一個(gè)項(xiàng)
{
bool tem_bool = true;
if (e.Index == 0 && tem_bool) //如果當(dāng)前繪制的是第一個(gè)或最后一個(gè)項(xiàng)
naught = false; //不進(jìn)行重繪
}
if (naught) //對(duì)控件進(jìn)行重繪
{
//獲取當(dāng)前繪制的顏色值
Brush brush =
listBoxBrushes[
place = (GradualC) ? (((e.Index % 2) == 0) ? 1 : 3) : (((e.Index % 2) == 0) ? 0 : 2)];
//用指定的畫(huà)刷填充列表項(xiàng)范圍所形成的矩形
e.Graphics.FillRectangle(brush, e.Bounds);
//判斷當(dāng)前項(xiàng)是否被選取中
bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? true : false;
if (selected) //如果當(dāng)前項(xiàng)被選中
{
e.Graphics.FillRectangle(new SolidBrush(ColorSelect), e.Bounds); //繪制當(dāng)前項(xiàng)
}
//繪制當(dāng)前項(xiàng)中的文本
e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, Brushes.Black, e.Bounds);
}
e.DrawFocusRectangle(); //繪制聚焦框
}
}
到此這篇關(guān)于C#實(shí)現(xiàn)ComboBox變色的示例代碼的文章就介紹到這了,更多相關(guān)C# ComboBox變色內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c# 利用易福門(mén)振動(dòng)模塊VSE002采集振動(dòng)數(shù)據(jù)的方法
這篇文章主要介紹了c# 利用易福門(mén)振動(dòng)模塊VSE002采集振動(dòng)數(shù)據(jù)的方法,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Unity相機(jī)移動(dòng)之屏幕邊緣檢測(cè)
這篇文章主要為大家詳細(xì)介紹了Unity相機(jī)移動(dòng)之屏幕邊緣檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
C# WPF開(kāi)源UI控件庫(kù)MaterialDesign介紹
這篇文章介紹了C# WPF開(kāi)源UI控件庫(kù)MaterialDesign,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
深入c# 類(lèi)和結(jié)構(gòu)的區(qū)別總結(jié)詳解
本篇文章是對(duì)c#中類(lèi)和結(jié)構(gòu)的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C# 中 Array和 ArrayList詳解及區(qū)別
這篇文章主要介紹了C# 中 Array和 ArrayList詳解及區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-01-01

