C#實(shí)現(xiàn)自定義ListBox背景的示例詳解
更新時(shí)間:2022年12月16日 10:41:37 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)自定義ListBox背景,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實(shí)踐過程
效果
代碼
public partial class DrawListBox : ListBox { public DrawListBox() { InitializeComponent(); this.DrawMode = DrawMode.OwnerDrawFixed; this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ListBox_DrawItem); } #region 變量 private static Brush[] listBoxBrushes;//該數(shù)組用來存儲(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 #region 事件 /// <summary> /// 鼠標(biāo)移出控件的可見區(qū)域時(shí)觸發(fā) /// </summary> protected virtual void ListBox_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 }; e.DrawBackground(); 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)]; e.Graphics.FillRectangle(brush, e.Bounds);//用指定的畫刷填充列表項(xiàng)范圍所形成的矩形 bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? true : false;//判斷當(dāng)前項(xiàng)是否被選取中 if (selected)//如果當(dāng)前項(xiàng)被選中 { e.Graphics.FillRectangle(new SolidBrush(ColorSelect), e.Bounds);//繪制當(dāng)前項(xiàng) } e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, Brushes.Black, e.Bounds);//繪制當(dāng)前項(xiàng)中的文本 } e.DrawFocusRectangle();//繪制聚焦框 } #endregion }
到此這篇關(guān)于C#實(shí)現(xiàn)自定義ListBox背景的示例詳解的文章就介紹到這了,更多相關(guān)C#自定義ListBox背景內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用linq語句查詢數(shù)組中以特定字符開頭元素的方法
這篇文章主要介紹了C#使用linq語句查詢數(shù)組中以特定字符開頭元素的方法,涉及C#使用linq進(jìn)行查詢的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04C#實(shí)現(xiàn)多文件打包壓縮(.Net?Core)
本文詳細(xì)講解了.Net?Core框架下C#實(shí)現(xiàn)多文件打包壓縮的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12C#實(shí)現(xiàn)員工ID卡的識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)識(shí)別員工ID卡的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01