gpt4 book ai didi

来自 argb 的 C# 颜色名称

转载 作者:行者123 更新时间:2023-12-02 05:41:39 25 4
gpt4 key购买 nike

我有 2 个 ownerdrawn 组合框。一个是线条颜色选择器——在图片中它不是组合框。另一个是线型选择器。一旦用户在第一个组合中选择了一种颜色,线型选择器组合(参见第二张图片,最后一个组合(一个没有名称)) 必须有相同颜色的线条。

enter image description here

目前是这样显示的

enter image description here这是两个组合的代码线条颜色选择器

    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) 方法返回一个颜色对象,但返回“00fff00”类型的名称值。如果您需要更多解释,请告诉我

`

谢谢你

最佳答案

我会使用反射来填充来自 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。

关于来自 argb 的 C# 颜色名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10875874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com