亲宝软件园·资讯

展开

C# 构造ColorComboBox c#构造ColorComboBox(颜色下拉框)

人气:0
想了解c#构造ColorComboBox(颜色下拉框)的相关内容吗,在本文为您仔细讲解C# 构造ColorComboBox的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:ColorComboBox,颜色下拉框,下面大家一起来学习吧。

复制代码 代码如下:

    class ColorComboBox : ComboBox
    {
        /// <summary>
        /// 当前选中色
        /// </summary>
        public Color SelectedColor
        {
            get { return Color.FromName(this.Text); }
        }
        /// <summary>
        /// 构造函数,构造颜色下拉列表
        /// </summary>
        public ColorComboBox()
        {
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            this.ItemHeight = 25;

            PropertyInfo[] propInfoList = typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
            foreach (PropertyInfo c in propInfoList)
            {
                this.Items.Add(c.Name);
            }
            this.Text = "Black"; //设置默认色
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Rectangle rect = e.Bounds;

            if (e.Index >= 0)
            {
                string colorName = this.Items[e.Index].ToString();
                Color c = Color.FromName(colorName);
                using (Brush b = new SolidBrush(c)) //预留下拉项间距
                {
                    e.Graphics.FillRectangle(b, rect.X, rect.Y + 2, rect.Width, rect.Height - 4);
                }
            }
        }

加载全部内容

相关教程
猜你喜欢
用户评论