欧美特黄不卡,涩涩视频在线,成人国产精品一区二区网站,亚洲一区二区三区欧美

當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

帶圖標與自定義顏色的ListBox

帶圖標與自定義顏色的ListBox

更新時間:2021-11-16 文章作者:未知 信息來源:網絡 閱讀次數:

自定義控件的實現

--------帶圖標和自定義顏色的ListBox

在一個點對點文件傳輸的項目中,我需要顯示文件傳輸的實時信息:傳輸的文件列表和當前傳輸的文件,當時我想到了用ListBox,但是但我用了ListBox后,我發現它不能改變控件中文本想的顏色,于是我就想擴展一下ListBox控件------ListBoxEx。

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

public class ListBoxEx : ListBox {…}

為了操作方便我為ListBoxEx的每一項設計專門的類ListBoxExItem

public class ListBoxExItem {…}

為了保持我這個控件與WinForm的標準控件的操作借口一致,我又重新設計了兩個集合類



public class ListBoxExItemCollection : IList, ICollection, IEnumerator {}//這個類相對于標準ListBox中的ObjectCollection,這個類作為ListBoxEx中的Items屬性的類型



public class SelectedListBoxExItemCollection : : IList, ICollection, IEnumerator{}//這個類相對于標準ListBox中的SelectedObjectCollection,這個類作為ListBoxEx中的SelectedItems屬性的類型



下面看兩個集合類的實現:

ListBoxExItemCollection的實現:為了做到對集合(Items)的操作能夠及時反映到ListBoxEx的控件中所以,此類只是對ListBox中Items(ObjectCollection類型)作了一層包裝,就是把ListBox中Items屬性的所有方法的只要是object類型的參數都轉換成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中有一個構造函數來傳遞ListBox中的Items對象

private ObjectCollection _Items;

public ListBoxExItemCollection(ObjectCollection baseItems)

{

this._Items = baseItems;

}

而SelectedListBoxExItemCollection類的實現也用同樣的方法,只不過是對SelectedObjectCollection包裝罷了。



集合實現后,再來看ListBoxExItem的實現:

為了使它支持圖標和多種顏色添加如下成員

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();

}

}

當然還有

private string _Text;

public string Text

{

get { return this._Text; }

set { this._Text = value; }

}

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

public override string ToString()

{

return this._Text;

}



再看ListBoxEx的實現:

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

為了覆蓋基類的Items等相關屬性添加



private ListBoxExItemCollection _Items; //在構造函數中創建

同時還需要重寫屬性Items

new public ListBoxExItemCollection Items

{

get

{

return this._Items;

}

}

new public ListBoxExItem SelectedItem //強制轉換為ListBoxExItem

{

get{ return base.SelectedItem as ListBoxExItem;}

set{ base.SelectedItem = value;}

}

new public SelectedListBoxExItemCollection SelectedItems //重新包裝SelectedItems

{

get

{

return new SelectedListBoxExItemCollection(base.SelectedItems);

}

}

為了支持圖標,添加一個圖像列表imagelist

private ImageList imageList;

public ImageList ImageList

{

get { return this.imageList; }

set

{

this.imageList = value;

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

}

}

而此控件的核心卻在一個方法OnDrawItem,這個方法每當控件的項需要重繪時就被調用

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]; //取得需要繪制項的引用

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); //繪制圖標

}

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); //根據項的顏色繪制文本

}

base.OnDrawItem(pe);

}

}



到此為止,ListBoxEx以完整的實現,并且支持可視化設計。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

主站蜘蛛池模板: 泗洪县| 张北县| 池州市| 南华县| 潍坊市| 正蓝旗| 时尚| 桃江县| 阆中市| 新竹市| 遂宁市| 张北县| 元阳县| 平安县| 南城县| 华蓥市| 青海省| 安溪县| 怀柔区| 萝北县| 镇江市| 桐乡市| 五莲县| 铁岭市| 治县。| 交城县| 芦山县| 济源市| 五指山市| 枞阳县| 元谋县| 太和县| 商水县| 灵宝市| 体育| 常州市| 三穗县| 会宁县| 东山县| 沙湾县| 蒙城县|