• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

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

        帶圖標與自定義顏色的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以完整的實現,并且支持可視化設計。

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

        本類教程下載

        系統下載排行

        主站蜘蛛池模板: 国产免费播放一区二区| 亚洲人成伊人成综合网久久| 国产精品无码亚洲精品2021| 99精品国产免费久久久久久下载| 亚洲综合亚洲国产尤物| 中文字幕在线免费观看| 亚洲美女大bbbbbbbbb| 5g影院5g天天爽永久免费影院 | 一本久久免费视频| 亚洲国产精品成人一区| 一级美国片免费看| 久久精品国产亚洲AV网站| 一级毛片成人免费看免费不卡| 久久亚洲精品无码VA大香大香| 1000部拍拍拍18勿入免费凤凰福利| 亚洲国产成人九九综合| 麻豆国产入口在线观看免费| 污污视频网站免费观看| 久久久久亚洲av无码专区蜜芽| 91免费国产在线观看| 亚洲第一成年网站视频| 浮力影院亚洲国产第一页| 无码国产精品一区二区免费3p| 亚洲AV无码国产精品色| 国产免费观看黄AV片 | 色费女人18女人毛片免费视频| 老司机亚洲精品影视www| 99精品视频免费观看| 亚洲国产午夜精品理论片在线播放| 亚洲A∨精品一区二区三区| 大地资源在线资源免费观看| 亚洲jjzzjjzz在线观看| 亚洲av高清在线观看一区二区| a级片免费观看视频| 亚洲免费福利在线视频| 久久精品国产亚洲5555| 亚洲国产精品免费观看| 久久久久久久久久免免费精品| 亚洲一区二区三区深夜天堂| 亚洲精品99久久久久中文字幕| 24小时在线免费视频|