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);
}
}
}C# 构造ColorComboBox
最新推荐文章于 2023-08-30 11:47:54 发布
本文介绍了一个自定义的颜色选择下拉框控件的实现方法。该控件使用C#编写,通过OwnerDraw方式绘制每个颜色选项,并利用反射获取所有预定义颜色名称。文章详细展示了如何设置默认颜色及响应绘制事件。


393

被折叠的 条评论
为什么被折叠?



