在大韩民国,我即将对如何做到这一点作出长篇回答,但经过一些研究,我描述了你试图做什么!
首先,使用静态的舱面储存你的颜色,是储存你的共同颜色的一种共同和可接受的方式。 你们可以继续采用这种办法。
这里,你需要做些什么才能在设计人中暴露这些颜色:
- Create a
CustomColors
class that encapsulates your custom colors.
- Create a
TypeConverter
that can convert between a string
and CustomColor
- Create an
IExtenderProvider
that adds a CustomForeColor
and CustomBackColor
to all of the controls on your form.
- Anywhere you want to use your custom colors, you just need to add your extender provider to your form (it will appear in the toolbox after you compile), and then CustomForeColor and CustomBackColor will appear as virtual properties for all controls, with a nice drop down.
最终结果:
The sky s the limit and if you want, you can create a custom UITypeEditor to actually paint the color in the propertygrid, but that s probably not necessary since you can inspect the ForeColor and BackColor properties.
Lots of helpful information on how to do this was found here:
这部法律:
<>CustomColor.cs
[TypeConverter(typeof(CustomColorTypeConverter))]
public class CustomColor
{
public static CustomColor Stop = new CustomColor { Color = Color.Red };
public static CustomColor Go = new CustomColor { Color = Color.Green };
public static CustomColor Yield = new CustomColor { Color = Color.Yellow };
public Color Color { get; private set; }
internal static CustomColor Find(Color color)
{
if (color == CustomColor.Stop.Color)
return CustomColor.Stop;
else if (color == CustomColor.Go.Color)
return CustomColor.Go;
else if (color == CustomColor.Yield.Color)
return CustomColor.Yield;
return new CustomColor { Color = Color.Transparent };
}
}
<>CustomColorTypeConverter.cs
public class CustomColorTypeConverter : StringConverter
{
static Dictionary<CustomColor, string> _nameIndex = InitializeNameIndex();
static Dictionary<string, CustomColor> _colorIndex = InitializeColorIndex();
private static Dictionary<string, CustomColor> InitializeColorIndex()
{
return typeof(CustomColor)
.GetFields(BindingFlags.Public | BindingFlags.Static)
.ToDictionary(f => f.Name, f => (CustomColor)f.GetValue(null));
}
private static Dictionary<CustomColor, string> InitializeNameIndex()
{
return typeof(CustomColor)
.GetFields(BindingFlags.Public | BindingFlags.Static)
.ToDictionary(f => (CustomColor)f.GetValue(null), f => f.Name);
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new System.ComponentModel.TypeConverter.StandardValuesCollection(_nameIndex.Values.ToList());
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(CustomColor))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
CustomColor result;
if (_colorIndex.TryGetValue((string)value, out result))
return result;
else
return new CustomColor();
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is CustomColor)
{
string result;
if (_nameIndex.TryGetValue((CustomColor)value, out result))
return result;
else
return String.Empty;
}
else
{
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
<>CustomColorExtenderProvider.cs
[ProvideProperty("CustomForeColor", typeof(Control))]
[ProvideProperty("CustomBackColor", typeof(Control))]
public class CustomColorExtenderProvider : Component, IExtenderProvider
{
public CustomColor GetCustomForeColor(Control control)
{
return CustomColor.Find(control.ForeColor);
}
public CustomColor GetCustomBackColor(Control control)
{
return CustomColor.Find(control.BackColor);
}
public void SetCustomBackColor(Control control, CustomColor value)
{
control.BackColor = value.Color;
}
public void SetCustomForeColor(Control control, CustomColor value)
{
control.ForeColor = value.Color;
}
public bool ShouldSerializeCustomForeColor(Control control)
{
return false;
}
public bool ShouldSerializeCustomBackColor(Control control)
{
return false;
}
#region IExtenderProvider Members
public bool CanExtend(object extendee)
{
return (extendee is Control);
}
#endregion
}