C#實(shí)現(xiàn)自定義ListBox背景的示例詳解
更新時(shí)間:2022年12月16日 10:41:37 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)自定義ListBox背景,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實(shí)踐過(guò)程
效果

代碼
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ù)組用來(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
#region 事件
/// <summary>
/// 鼠標(biāo)移出控件的可見(jiàn)區(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);//用指定的畫(huà)刷填充列表項(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)文章
DevExpress實(shí)現(xiàn)為T(mén)extEdit設(shè)置水印文字的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)為T(mén)extEdit設(shè)置水印文字的方法,對(duì)C#程序設(shè)計(jì)人員來(lái)說(shuō)是一個(gè)很實(shí)用的技巧,需要的朋友可以參考下2014-08-08
C#多線程同步:Mutex與Semaphore的區(qū)別及使用場(chǎng)景詳解
這篇文章主要介紹了C#多線程同步:Mutex與Semaphore的區(qū)別及使用場(chǎng)景,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
C#反射之基礎(chǔ)應(yīng)用實(shí)例總結(jié)
這篇文章主要介紹了C#反射之基礎(chǔ)應(yīng)用實(shí)例總結(jié),包括了反射的基本原理與用法實(shí)例,需要的朋友可以參考下2014-10-10
WinForm實(shí)現(xiàn)自定義右下角提示效果的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)自定義右下角提示效果的方法,涉及WinForm自定義提示效果的實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C# Guid長(zhǎng)度雪花簡(jiǎn)單生成器的示例代碼
這篇文章主要介紹了C# Guid長(zhǎng)度雪花簡(jiǎn)單生成器的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-12-12

