12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- namespace OTSIncAReportApp
- {
- /// <summary>
- /// 自定义颜色对话框窗体
- /// </summary>
- public partial class myColorComboBox : ComboBox
- {
- #region 构造函数
- public myColorComboBox()
- {
- InitializeComponent();
- InitItems();
- }
- public myColorComboBox(IContainer container)
- {
- container.Add(this);
- InitializeComponent();
- InitItems();
- }
- #endregion
- #region 初始化项
- 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;
- }
- #endregion
- #region 绘制方法
- 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);//绘制文字
- }
- }
- #endregion
- #region 自定义控件相关属性
- /// <summary>
- /// 选择的颜色名称
- /// </summary>
- public string SelectColorName
- {
- get { return this.Text; }
- }
- /// <summary>
- /// 选择的颜色
- /// </summary>
- public Color SelectColor
- {
- get { return Color.FromName(this.Text); }
- }
- #endregion
- }
- }
|