展会信息港展会大全

c#构造ColorComboBox的代码分享
来源:互联网   发布日期:2016-01-19 12:58:14   浏览:2122次  

导读:这篇文章主要介绍了c 构造ColorComboBox的代码分享,大家参考使用吧代码如下: class ColorComboBox : ComboBox { summary 当前选中色 summary public ...

这篇文章主要介绍了c#构造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);

}

}

}

赞助本站

人工智能实验室

相关热词: ColorComboBox c

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港