zoukankan      html  css  js  c++  java
  • 列表框的图标

    介绍 我们都喜欢能控制更多的颜色或图像,我也一样。 在本文中,我为自定义ListBox类中的每个项提供了一个image属性。 注意:我的文章没有源代码,因为它非常简短和容易。 首先:我们为GListBox创建了两个类 隐藏,收缩,复制Code

    // GListBoxItem

      

    class 
    public class GListBoxItem
    {
        private string _myText;
        private int _myImageIndex;
        // properties 
        public string Text
        {
            get {return _myText;}
            set {_myText = value;}
        }
        public int ImageIndex
        {
            get {return _myImageIndex;}
            set {_myImageIndex = value;}
        }
        //constructor
        public GListBoxItem(string text, int index)
        {
            _myText = text;
            _myImageIndex = index;
        }
        public GListBoxItem(string text): this(text,-1){}
        public GListBoxItem(): this(""){}
        public override string ToString()
        {
            return _myText;
        }
    }//End of GListBoxItem class
    
    // GListBox class 
    public class GListBox : ListBox
    {
        private ImageList _myImageList;
        public ImageList ImageList
        {
            get {return _myImageList;}
            set {_myImageList = value;}
        }
        public GListBox()
        {
            // Set owner draw mode
            this.DrawMode = DrawMode.OwnerDrawFixed;
        }
        protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            GListBoxItem item;
            Rectangle bounds = e.Bounds;
            Size imageSize = _myImageList.ImageSize;
            try
            {
                item = (GListBoxItem) Items[e.Index];
                if (item.ImageIndex != -1)
                {
                    imageList.Draw(e.Graphics, bounds.Left,bounds.Top,item.ImageIndex); 
                    e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), 
                        bounds.Left+imageSize.Width, bounds.Top);
                }
                else
                {
                    e.Graphics.DrawString(item.Text, e.Font,new SolidBrush(e.ForeColor),
                        bounds.Left, bounds.Top);
                }
            }
            catch
            {
                if (e.Index != -1)
                {
                    e.Graphics.DrawString(Items[e.Index].ToString(),e.Font, 
                        new SolidBrush(e.ForeColor) ,bounds.Left, bounds.Top);
                }
                else
                {
                    e.Graphics.DrawString(Text,e.Font,new SolidBrush(e.ForeColor),
                        bounds.Left, bounds.Top);
                }
            }
            base.OnDrawItem(e);
        }
    }//End of GListBox class

    在那之后,为了使用我们的代码,我们可以做: 隐藏,复制Code

    GListBox lb = new GListBox();
    lb.ImageList = imageList;
    lb.Items.Add( new GListBoxItem("Image 1",0));
    lb.Items.Add( new GListBoxItem("Image 2",1));
    lb.Items.Add( new GListBoxItem("Image 3",2));

    以上就是全部内容,谢谢大家的阅读。 本文转载于:http://www.diyabc.com/frontweb/news336.html

  • 相关阅读:
    ExtJS小技巧
    Oracle 表的行数、表占用空间大小,列的非空行数、列占用空间大小 查询
    NPM 私服
    IDEA 不编译java以外的文件
    SQL 引号中的问号在PrepareStatement 中不被看作是占位符
    Chrome 浏览器自动填表呈现淡黄色解决
    批量删除Maven 仓库未下载成功.lastupdate 的文件
    Oracle 11g 监听很慢,由于监听日志文件太大引起的问题(Windows 下)
    Hibernate 自动更新表出错 建表或添加列,提示标识符无效
    Hibernate 自动更新表出错 More than one table found in namespace
  • 原文地址:https://www.cnblogs.com/Dincat/p/13437691.html
Copyright © 2011-2022 走看看