颜色组合框

 
 
 
 
 
 
 
 
 
 
 
使用

    private void Form1_Load(object senderEventArgs e)
        {
            colorComboBox1.Initialize();
        }
 
      
 
        private void colorComboBox1_SelectedIndexChanged(object senderEventArgs e)
        {
            panel1.BackColor = colorComboBox1.Color;
        }
 
 
        private void button4_Click(object senderEventArgs e)
        {
            colorComboBox1.Color = Color.Red
        }
 
 

using System.Drawing;
using System.Reflection;
 
namespace System.Windows.Forms
{
    public class ColorComboBox : ComboBox
    {
        public ColorComboBox()
        {
            comboBox1 = this;
            // 
            // comboBox1
            // 
           comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
           comboBox1.DropDownHeight = 400;
           comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
           comboBox1.DropDownWidth = 200;   
           comboBox1.MaxDropDownItems = 20;
           comboBox1.Size = new System.Drawing.Size(134, 24);
           comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(comboBox1_DrawItem);
           comboBox1.SelectedIndexChanged += new System.EventHandler(comboBox1_SelectedIndexChanged);
        }
 
        //public Color Color = Color.Black;
        private void comboBox1_SelectedIndexChanged(object senderEventArgs e)
        {
            if (comboBox1.SelectedItem.ToString()!="")
            __color = Color.FromName(comboBox1.SelectedItem.ToString());
        }
 
        
        public Color Color
        {
            get
            {
                return __color;
            }
            set
            {
                comboBox1.SelectedIndex = comboBox1.Items.IndexOf(value.Name.ToString());
            }
        }
 
        public void Initialize()
        {
            Type type1 = typeof(System.Drawing.Color);
            PropertyInfo[] propertyInfo1 = type1.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
            foreach (PropertyInfo color in propertyInfo1)
            {
                comboBox1.Items.Add(color.Name);
            }
            comboBox1.SelectedIndex = 8;
        }
 
        private void comboBox1_DrawItem(object senderDrawItemEventArgs e)
        {
            if (e.Index >= 0)
            {
                string s = String.Format(" {0}"comboBox1.Items[e.Index]);
 
                Font font = new Font("Arial", 9);
                Color color = Color.FromName(s.Trim());
                Brush brush = new SolidBrush(color);
 
                Rectangle rect = new Rectangle(
                    e.Bounds.X + 4,
                    e.Bounds.Y + 2,
                    40,
                    e.Bounds.Height - 6
                    );
 
                e.DrawBackground();
                e.Graphics.DrawString(sfontBrushes.Blackrect.Xrect.Top);
                e.Graphics.FillRectangle(brushrect);
                e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 1), rect);
                //e.DrawFocusRectangle();
            }
        }
 
 
        private System.Windows.Forms.ComboBox comboBox1;
 
        //最好使用颜色的名字 如 Color.   总之是有名字的颜色
         private Color __color = Color.Black;
    }
}
 
 





原文地址:https://www.cnblogs.com/xe2011/p/3806354.html