日期:2014-05-18  浏览次数:21021 次

C#中自定义颜色库问题!高手指教!
我想自己写一个 "颜色设置对话框 ",不用系统给提供的 "colorDialog ".但不知如何下手,该调用系统的哪个函数才能调用出颜色面板中的那些可选择的颜色,还有系统提供的颜色等等.???

------解决方案--------------------
SystemColors
------解决方案--------------------
可以使用从Control继承的任何一个,比如UserControl,Panel等,但是要自己使用Graphics绘制。比如:

public class ColorPanel:Control
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

Rectangle colorRect = new Rectangle(0, 0, 10, 10);
foreach (KnownColor kc in Enum.GetValues(typeof(KnownColor)))
{
Color color = Color.FromKnownColor(kc);
using (SolidBrush brush = new SolidBrush(color))
{
e.Graphics.FillRectangle(brush, colorRect);
}

if (colorRect.Right+colorRect.Width +2 < this.ClientRectangle.Right)
{
colorRect.X += colorRect.Width + 2;
}
else
{
colorRect.X = 0;
colorRect.Y += colorRect.Height + 2;
}
}
}
public Color GetColor(int x, int y)
{
Rectangle colorRect = new Rectangle(0, 0, 10, 10);
foreach (KnownColor kc in Enum.GetValues(typeof(KnownColor)))
{
if (colorRect.Contains(x, y))
{
return Color.FromKnownColor(kc);
}
if (colorRect.Right + colorRect.Width + 2 < this.ClientRectangle.Right)
{
colorRect.X += colorRect.Width + 2;
}
else
{
colorRect.X = 0;
colorRect.Y += colorRect.Height + 2;
}
}
return Color.Empty;
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
Console.WriteLine(this.GetColor(e.X, e.Y));
}
}