为ComboBox控件添加图片

    技术2022-05-11  57

    using  System; using  System.Windows.Forms; using  System.Windows.Forms.Design; using  System.Drawing; namespace  Mengxianhui.ComboBoxEx {  class ComboBoxEx : ComboBox  {    private ImageList imageList;    public ImageList ImageList    {      get {return imageList;}      set {imageList = value;}    }    public ComboBoxEx()    {      DrawMode = DrawMode.OwnerDrawFixed;    }    protected override void OnDrawItem(DrawItemEventArgs ea)    {      ea.DrawBackground();      ea.DrawFocusRectangle();      ComboBoxExItem item;      Size imageSize = imageList.ImageSize;      Rectangle bounds = ea.Bounds;      try      {        item = (ComboBoxExItem)Items[ea.Index];        if (item.ImageIndex != -1)        {          imageList.Draw(ea.Graphics, bounds.Left, bounds.Top,            item.ImageIndex);          ea.Graphics.DrawString(item.Text, ea.Font, new            SolidBrush(ea.ForeColor), bounds.Left+imageSize.Width, bounds.Top);        }        else        {          ea.Graphics.DrawString(item.Text, ea.Font, new            SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);        }      }      catch      {        if (ea.Index != -1)        {          ea.Graphics.DrawString(Items[ea.Index].ToString(), ea.Font, new            SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);        }        else        {          ea.Graphics.DrawString(Text, ea.Font, new            SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);        }      }      base.OnDrawItem(ea);    }  }  class ComboBoxExItem  {    private string _text;    public string Text    {      get {return _text;}      set {_text = value;}    }    private int _imageIndex;    public int ImageIndex    {      get {return _imageIndex;}      set {_imageIndex = value;}    }    public ComboBoxExItem()      : this("")     {    }    public ComboBoxExItem(string text)      : this(text, -1)     {    }    public ComboBoxExItem(string text, int imageIndex)    {      _text = text;      _imageIndex = imageIndex;    }    public override string ToString()    {      return _text;    }  }} 使用方法 using  System; using  System.Drawing; using  System.Collections; using  System.ComponentModel; using  System.Windows.Forms; using  System.Data; namespace  Mengxianhui.ComboBoxEx {    /// <summary>    /// Summary description for Form1.    /// </summary>    public class Form1 : System.Windows.Forms.Form    {    private ComboBoxEx comboBox1;    private System.ComponentModel.IContainer components;        public Form1()        {            //            // Required for Windows Form Designer support            //            InitializeComponent();            //            // TODO: Add any constructor code after InitializeComponent call            //        }        /// <summary>        /// Clean up any resources being used.        /// </summary>        protected override void Dispose( bool disposing )        {            if( disposing )            {                if (components != null)                 {                    components.Dispose();                }            }            base.Dispose( disposing );        }        Windows Form Designer generated code        /// <summary>        /// The main entry point for the application.        /// </summary>        [STAThread]        static void Main()         {            Application.Run(new Form1());             }    }}  

    最新回复(0)