日本国产亚洲-日本国产一区-日本国产一区二区三区-日本韩国欧美一区-日本韩国欧美在线-日本韩国欧美在线观看

當(dāng)前位置:雨林木風(fēng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁(yè)面

帶圖標(biāo)與自定義顏色的ListBox

帶圖標(biāo)與自定義顏色的ListBox

更新時(shí)間:2021-11-16 文章作者:未知 信息來(lái)源:網(wǎng)絡(luò) 閱讀次數(shù):

自定義控件的實(shí)現(xiàn)

--------帶圖標(biāo)和自定義顏色的ListBox

在一個(gè)點(diǎn)對(duì)點(diǎn)文件傳輸?shù)捻?xiàng)目中,我需要顯示文件傳輸?shù)膶?shí)時(shí)信息:傳輸?shù)奈募斜砗彤?dāng)前傳輸?shù)奈募?dāng)時(shí)我想到了用ListBox,但是但我用了ListBox后,我發(fā)現(xiàn)它不能改變控件中文本想的顏色,于是我就想擴(kuò)展一下ListBox控件------ListBoxEx。

我的目標(biāo)是給空間加上圖標(biāo),還要能時(shí)時(shí)改變控件文本顏色。于是從ListBox派生類

public class ListBoxEx : ListBox {…}

為了操作方便我為L(zhǎng)istBoxEx的每一項(xiàng)設(shè)計(jì)專門的類ListBoxExItem

public class ListBoxExItem {…}

為了保持我這個(gè)控件與WinForm的標(biāo)準(zhǔn)控件的操作借口一致,我又重新設(shè)計(jì)了兩個(gè)集合類



public class ListBoxExItemCollection : IList, ICollection, IEnumerator {}//這個(gè)類相對(duì)于標(biāo)準(zhǔn)ListBox中的ObjectCollection,這個(gè)類作為L(zhǎng)istBoxEx中的Items屬性的類型



public class SelectedListBoxExItemCollection : : IList, ICollection, IEnumerator{}//這個(gè)類相對(duì)于標(biāo)準(zhǔn)ListBox中的SelectedObjectCollection,這個(gè)類作為L(zhǎng)istBoxEx中的SelectedItems屬性的類型



下面看兩個(gè)集合類的實(shí)現(xiàn):

ListBoxExItemCollection的實(shí)現(xiàn):為了做到對(duì)集合(Items)的操作能夠及時(shí)反映到ListBoxEx的控件中所以,此類只是對(duì)ListBox中Items(ObjectCollection類型)作了一層包裝,就是把ListBox中Items屬性的所有方法的只要是object類型的參數(shù)都轉(zhuǎn)換成ListBoxExItem,比如:

public void Remove(ListBoxExItem item)

{

this._Items.Remove(item); //_Items為ObjectCollection類型

}

public void Insert(int index, ListBoxExItem item)

{

this._Items.Insert(index, item);

}

public int Add(ListBoxExItem item)

{

return this._Items.Add(item);

}

由上可知,ListBoxExItemCollection中有一個(gè)構(gòu)造函數(shù)來(lái)傳遞ListBox中的Items對(duì)象

private ObjectCollection _Items;

public ListBoxExItemCollection(ObjectCollection baseItems)

{

this._Items = baseItems;

}

而SelectedListBoxExItemCollection類的實(shí)現(xiàn)也用同樣的方法,只不過(guò)是對(duì)SelectedObjectCollection包裝罷了。



集合實(shí)現(xiàn)后,再來(lái)看ListBoxExItem的實(shí)現(xiàn):

為了使它支持圖標(biāo)和多種顏色添加如下成員

private int _ImageIndex;

public int ImageIndex

{

get { return this._ImageIndex; }

set { this._ImageIndex = value;}

}

private Color _ForeColor;

public Color ForeColor

{

get{ return this._ForeColor;}

set

{

this._ForeColor = value;

this.Parent.Invalidate();

}

}

當(dāng)然還有

private string _Text;

public string Text

{

get { return this._Text; }

set { this._Text = value; }

}

為了控件能正確顯示此項(xiàng)的文本,還必須重寫ToString()方法

public override string ToString()

{

return this._Text;

}



再看ListBoxEx的實(shí)現(xiàn):

為了使控件能夠自我繪制,所以DrawMode = DrawMode.OwnerDrawFixed;

為了覆蓋基類的Items等相關(guān)屬性添加



private ListBoxExItemCollection _Items; //在構(gòu)造函數(shù)中創(chuàng)建

同時(shí)還需要重寫屬性Items

new public ListBoxExItemCollection Items

{

get

{

return this._Items;

}

}

new public ListBoxExItem SelectedItem //強(qiáng)制轉(zhuǎn)換為L(zhǎng)istBoxExItem

{

get{ return base.SelectedItem as ListBoxExItem;}

set{ base.SelectedItem = value;}

}

new public SelectedListBoxExItemCollection SelectedItems //重新包裝SelectedItems

{

get

{

return new SelectedListBoxExItemCollection(base.SelectedItems);

}

}

為了支持圖標(biāo),添加一個(gè)圖像列表imagelist

private ImageList imageList;

public ImageList ImageList

{

get { return this.imageList; }

set

{

this.imageList = value;

this.Invalidate();//圖像列表改變后馬上更新控件

}

}

而此控件的核心卻在一個(gè)方法OnDrawItem,這個(gè)方法每當(dāng)控件的項(xiàng)需要重繪時(shí)就被調(diào)用

protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs pe)

{

pe.DrawBackground(); //畫背景

pe.DrawFocusRectangle(); //畫邊框

Rectangle bounds = pe.Bounds;

// Check whether the index is valid

if(pe.Index >= 0 && pe.Index < base.Items.Count)

{

ListBoxExItem item = this.Items[pe.Index]; //取得需要繪制項(xiàng)的引用

int iOffset = 0;

// If the image list is present and the image index is set, draw the image

if(this.imageList != null)

{

if (item.ImageIndex > -1 && item.ImageIndex < this.imageList.Images.Count)

{

this.imageList.Draw(pe.Graphics, bounds.Left, bounds.Top, bounds.Height, bounds.Height, item.ImageIndex); //繪制圖標(biāo)

}

iOffset += bounds.Height;//this.imageList.ImageSize.Width;

}

// Draw item text

pe.Graphics.DrawString(item.Text, pe.Font, new SolidBrush(item.ForeColor),

bounds.Left + iOffset, bounds.Top); //根據(jù)項(xiàng)的顏色繪制文本

}

base.OnDrawItem(pe);

}

}



到此為止,ListBoxEx以完整的實(shí)現(xiàn),并且支持可視化設(shè)計(jì)。

溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!

本類教程下載

系統(tǒng)下載排行

主站蜘蛛池模板: 中文字幕在线有码高清视频 | 亚洲国产成a人v在线 | 波多野一区二区三区在线 | 最近国语版中文字幕 | 成人免费国产gav视频在线 | 久久一次| 亚洲欧美日韩在线观看看另类 | 国产中文字幕在线视频 | 国产成人精品亚洲2020 | 狠狠操视频网站 | igao视频网为爱 | 亚洲欧美另类日本 | 精品免费tv久久久久久久 | 亚洲国产综合精品中文第一区 | 日本中文字幕在线视频 | 在线免费观看国产 | 青青草娱乐在线 | 亚洲欧美精品 | 日韩在线视频一区 | 一区二区三区舞蹈区 | 国产三级日产三级韩国三级 | 97看看| 国产精品久久久久无毒 | 超级在线牛碰碰视频 | 久久网站在线观看 | 久久精品国产精品亚洲 | 天天操天天射天天爽 | 狠狠色综合色综合网络 | 久久精品国产免费 | 日韩视频网址 | 国产精品夜夜春夜夜爽 | 国产精选视频在线观看 | 不卡视频免费在线观看 | 成人午夜免费视频毛片 | 国产色在线观看 | 热久久久 | 清纯唯美亚洲综合日韩第 | 99国产精品 | 成人影视频 | 久久国产精彩视频 | 青青草国拍2019 |