asp.net C#自定义控件一下拉颜色框方法

作者:袖梨 2022-06-25

asp教程.net C#自定义控件一下拉颜色框方法

通过继承ComboBox可以设计出类似C#控件属性栏中的颜色下拉选择框。

添加组件命名为myColorComboBox.cs

 

第一步:继承ComboBox,public partial class myColorComboBox : ComboBox

 

第二步:构造下拉颜色选择框

private void InitItems()

        {

            this.DrawMode = DrawMode.OwnerDrawFixed;//手动绘制所有元素

            this.DropDownStyle = ComboBoxStyle.DropDownList;//下拉框样式设置为不能编辑

            this.Items.Clear();//清空原有项

            Array allColors = Enum.GetValues(typeof(KnownColor));//获取系统颜色名存入列表

            foreach (KnownColor var in allColors)

            {

                this.Items.Add(var.ToString()); //加载该选项框的子项

            }

            this.SelectedIndex = 0;

        }

在两个构造函数中加入InitItems()

 

第三步:重写OnDrawItem方法

protected override void OnDrawItem(DrawItemEventArgs e)

        {

            if (e.Index >= 0)//判断是否需要重绘

            {

                string colorName = this.Items[e.Index].ToString();//获取颜色名

                SolidBrush brush = new SolidBrush(Color.FromName(colorName));//定义画刷

                Font font = new Font("宋体", 9);//定义字体

                Rectangle rect = e.Bounds;

                rect.Inflate(-2, -2);

 

                Rectangle rectColor = new Rectangle(rect.Location, new Size(20, rect.Height));

                e.Graphics.FillRectangle(brush, rectColor);//填充颜色

                e.Graphics.DrawRectangle(Pens.Black, rectColor);//绘制边框

                e.Graphics.DrawString(colorName, font, Brushes.Black, (rect.X + 22), rect.Y);//绘制文字

            }

        }

 

第四步:增加控件属性

 

///

        /// 选择的颜色名称

        ///

        public string SelectColorName

        {

            get { return this.Text; }

        }

 

        ///

        /// 选择的颜色

        ///

        public Color SelectColor

        {

            get { return Color.FromName(this.Text); }

        }

 

用法:

直接从控件栏中找到自定义控件myColorComboBox,拖过去自动命名为myColorCombBox1,可以通过myColorCombBox1.SelectColor获取颜色,类型为Color,通过myColorCombBox1.SelectColorName获取用户选定的颜色名。

 

下面贴出全部代码:

//控件名:myColorComboBox

//作者:刘典武

//时间:2011-06-01

 

using System;

using System.ComponentModel;

using System.Collections.Generic;

using System.Diagnostics;

using System.Text;

using System.Windows.Forms;

using System.Drawing;

 

namespace myControl

{

    public partial class myColorComboBox : ComboBox

    {

        public myColorComboBox()

        {

            InitializeComponent();

            InitItems();

        }

 

        public myColorComboBox(IContainer container)

        {

            container.Add(this);

 

            InitializeComponent();

            InitItems();

        }

 

        private void InitItems()

        {

            this.DrawMode = DrawMode.OwnerDrawFixed;//手动绘制所有元素

            this.DropDownStyle = ComboBoxStyle.DropDownList;//下拉框样式设置为不能编辑

            this.Items.Clear();//清空原有项

            Array allColors = Enum.GetValues(typeof(KnownColor));//获取系统颜色名存入列表

            foreach (KnownColor var in allColors)

            {

                this.Items.Add(var.ToString()); //加载该选项框的子项

            }

            this.SelectedIndex = 0;

        }

 

        protected override void OnDrawItem(DrawItemEventArgs e)

        {

            if (e.Index >= 0)//判断是否需要重绘

            {

                string colorName = this.Items[e.Index].ToString();//获取颜色名

                SolidBrush brush = new SolidBrush(Color.FromName(colorName));//定义画刷

                Font font = new Font("宋体", 9);//定义字体

                Rectangle rect = e.Bounds;

                rect.Inflate(-2, -2);

 

                Rectangle rectColor = new Rectangle(rect.Location, new Size(20, rect.Height));

                e.Graphics.FillRectangle(brush, rectColor);//填充颜色

                e.Graphics.DrawRectangle(Pens.Black, rectColor);//绘制边框

                e.Graphics.DrawString(colorName, font, Brushes.Black, (rect.X + 22), rect.Y);//绘制文字

            }

        }

 

        ///

        /// 选择的颜色名称

        ///

        public string SelectColorName

        {

            get { return this.Text; }

        }

 

        ///

        /// 选择的颜色

        ///

        public Color SelectColor

        {

            get { return Color.FromName(this.Text); }

        }

    }

}

相关文章

精彩推荐