我有2个所有者拉出的组合框。 一个是线条颜色选择器 - 在图片中它不是一个组合框。 另一种是线型选择器。 一旦用户在第一个组合中选择一种颜色,就可以使用linestyle选择器组合(参见第二张图片,最后一个组合(一个带有noname)) 必须有相同的彩色线条。
目前显示如下
这是两个组合的代码 线色选择器
public partial class ColorPicker : ComboBox
{
// Data for each color in the list
public class ColorInfo
{
public string Text { get; set; }
public Color Color { get; set; }
public ColorInfo(string text, Color color)
{
Text = text;
Color = color;
}
}
public ColorPicker()
{
InitializeComponent();
DropDownStyle = ComboBoxStyle.DropDownList;
DrawMode = DrawMode.OwnerDrawFixed;
DrawItem += OnDrawItem;
}
// Populate control with standard colors
public void AddStandardColors()
{
Items.Clear();
Items.Add(new ColorInfo("Black", Color.Black));
Items.Add(new ColorInfo("Blue", Color.Blue));
Items.Add(new ColorInfo("Lime", Color.Lime));
Items.Add(new ColorInfo("Cyan", Color.Cyan));
Items.Add(new ColorInfo("Red", Color.Red));
Items.Add(new ColorInfo("Fuchsia", Color.Fuchsia));
Items.Add(new ColorInfo("Yellow", Color.Yellow));
Items.Add(new ColorInfo("White", Color.White));
Items.Add(new ColorInfo("Navy", Color.Navy));
Items.Add(new ColorInfo("Green", Color.Green));
Items.Add(new ColorInfo("Teal", Color.Teal));
Items.Add(new ColorInfo("Maroon", Color.Maroon));
Items.Add(new ColorInfo("Purple", Color.Purple));
Items.Add(new ColorInfo("Olive", Color.Olive));
Items.Add(new ColorInfo("Gray", Color.Gray));
}
// Draw list item
protected void OnDrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index >= 0)
{
// Get this color
ColorInfo color = (ColorInfo)Items[e.Index];
// Fill background
e.DrawBackground();
// Draw color box
Rectangle rect = new Rectangle();
rect.X = e.Bounds.X + 2;
rect.Y = e.Bounds.Y + 2;
rect.Width = 18;
rect.Height = e.Bounds.Height - 5;
e.Graphics.FillRectangle(new SolidBrush(color.Color), rect);
e.Graphics.DrawRectangle(SystemPens.WindowText, rect);
// Write color name
Brush brush;
if ((e.State & DrawItemState.Selected) != DrawItemState.None)
brush = SystemBrushes.HighlightText;
else
brush = SystemBrushes.WindowText;
e.Graphics.DrawString(color.Text, Font, brush,
e.Bounds.X + rect.X + rect.Width + 2,
e.Bounds.Y + ((e.Bounds.Height - Font.Height) / 2));
// Draw the focus rectangle if appropriate
if ((e.State & DrawItemState.NoFocusRect) == DrawItemState.None)
e.DrawFocusRectangle();
}
}
/// <summary>
/// Gets or sets the currently selected item.
/// </summary>
public new ColorInfo SelectedItem
{
get
{
return (ColorInfo)base.SelectedItem;
}
set
{
base.SelectedItem = value;
}
}
/// <summary>
/// Gets the text of the selected item, or sets the selection to
/// the item with the specified text.
/// </summary>
public new string SelectedText
{
get
{
if (SelectedIndex >= 0)
return SelectedItem.Text;
return String.Empty;
}
set
{
for (int i = 0; i < Items.Count; i++)
{
if (((ColorInfo)Items[i]).Text == value)
{
SelectedIndex = i;
break;
}
}
}
}
/// <summary>
/// Gets the value of the selected item, or sets the selection to
/// the item with the specified value.
/// </summary>
public new int SelectedValue
{
get
{
if (SelectedIndex >= 0)
return SelectedItem.Color.ToArgb();
return Color.White.ToArgb();
}
set
{
for (int i = 0; i < Items.Count; i++)
{
if (((ColorInfo)Items[i]).Color.ToArgb() == value)
{
SelectedIndex = i;
break;
}
}
}
}
}
}
Code for lineselector combo. This is the last combo(the one with no name) on my second picture. : `
public partial class comboBoxLineStyle : ComboBox
{
public comboBoxLineStyle()
{
InitializeComponent();
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DropDownStyle = ComboBoxStyle.DropDownList;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (e.Index < 0) { return; }
e.DrawBackground();
ComboBoxItem item = (ComboBoxItem)this.Items[e.Index];
e.Graphics.DrawImage(item.Picture,new Point(e.Bounds.X, e.Bounds.Y));
}
public new Image SelectedItem
{
get
{
return (Image)base.SelectedItem;
}
set
{
base.SelectedItem = value;
}
}
public new Image SelectedValue
{
get
{
return (Image)base.SelectedValue;
}
set
{
base.SelectedValue = value;
}
}
}
public class ComboBoxItem
{
public string text;
public Image Picture;
public int foreColor;
//public override string ToString()
//{
// return text;
//}
public ComboBoxItem() { }
public ComboBoxItem(string pText, Image pValue)
{
text = pText;
Picture = pValue;
}
public ComboBoxItem(string pText, Image pValue, int pColor)
{
text = pText; Picture = pValue; foreColor = pColor;
}
}
}
以我正在访问的形式如下:
private void DlgGraphOptions_Load(object sender, EventArgs e)
{
Color selCol = m_SerOpts.GetColor(idxSeries);
int selcolARGB = selCol.ToArgb();
Color color = Color.FromArgb(selcolARGB);
ComboBoxItem item1Solid = new ComboBoxItem("Solid Line",Properties.Resources.Solidline,selcolARGB);
ComboBoxItem item1dash = new ComboBoxItem("Dashed Line", Properties.Resources.dashedline,selcolARGB);
ComboBoxItem item1dashed = new ComboBoxItem("Dashed Line", Properties.Resources.dashdash,selcolARGB);
comboBoxLineStyle1.Items.Add(item1Solid);
comboBoxLineStyle1.Items.Add(item1dash);
comboBoxLineStyle1.Items.Add(item1dashed);
comboBoxLineStyle1.SelectedIndex = 0;
//I prefer doing something like this
comboBoxLineStyle1.ForeColor.ToArgb() = selcolARGB .
}
m_SerOpts.GetColor(idxSeries)方法返回一个颜色对象,但返回一个&#39; 00fff00&#39;名字的价值。 请知道你是否需要更多解释
`
谢谢你
答案 0 :(得分:3)
我会使用反射来填充Color类中所有颜色的列表(尽管您可以在代码中复制它):
Dictionary<String, Color> colors = typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.Public).ToDictionary(p => p.Name, p => (Color)p.GetValue(null, null) );
然后使用方法在此字典中搜索与传递的颜色匹配的颜色:
public String GetColorName(Color color)
{
return colors.Where(c => c.Value.A == color.A && c.Value.R == color.R && c.Value.G == color.G && c.Value.B == color.B).FirstOrDefault().Key;
}
如果没有匹配的颜色,则返回null。
答案 1 :(得分:2)
简单地说,
System.Drawing.ColorTranslator.FromHtml("#FFFFFF").Name