控件的CustomColor属性如下定义:
private Color[] m_CustomColor = new Color[0];
[Category("Appearance"),
TypeConverter(typeof(ColorConverter)),
Description("自定义字符随机颜色。")]
public Color[] CustomColor
{
get{return m_CustomColor;}
set{m_CustomColor = value;}
}
[Category("Appearance"),
TypeConverter(typeof(ColorConverter)),
Description("自定义字符随机颜色。")]
public Color[] CustomColor
{
get{return m_CustomColor;}
set{m_CustomColor = value;}
}
然后定义ColorConverter类:
using System;
using System.Drawing;
using System.Globalization;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
namespace FaibClass.WebControls
{
internal class ColorConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
else if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
//从字符串中解析出十六进制格式的颜色值,并转成Color
string[] value1 = value.ToString().Split(';');
Color[] col = new Color[value1.Length];
for(int i = 0; i < value1.Length; i++)
{
col[i] = Util.FromHexColor(value1[i]);
}
return col;
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
//将Color数组转换成 #ffffff;#ccddaa;#00ddcc 这样格式的字符串保存
Color[] col = (Color[])value;
string[] sto = new string[col.Length];
for(int i = 0; i < col.Length; i++)
{
sto[i] = Util.ConvertToHexColor(col[i]);
}
return string.Join(";", sto);
}
else if (destinationType == typeof(InstanceDescriptor) && (value is Color[]))
{
Color[] col = (Color[])value;
string[] sto = new string[col.Length];
for(int i = 0; i < col.Length; i++)
{
sto[i] = Util.ConvertToHexColor(col[i]);
}
return new InstanceDescriptor(typeof(Color[]).GetConstructor(new Type[] { typeof(string) }), new string[] { string.Join(";", sto) } );
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
using System.Drawing;
using System.Globalization;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
namespace FaibClass.WebControls
{
internal class ColorConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
else if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
//从字符串中解析出十六进制格式的颜色值,并转成Color
string[] value1 = value.ToString().Split(';');
Color[] col = new Color[value1.Length];
for(int i = 0; i < value1.Length; i++)
{
col[i] = Util.FromHexColor(value1[i]);
}
return col;
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
//将Color数组转换成 #ffffff;#ccddaa;#00ddcc 这样格式的字符串保存
Color[] col = (Color[])value;
string[] sto = new string[col.Length];
for(int i = 0; i < col.Length; i++)
{
sto[i] = Util.ConvertToHexColor(col[i]);
}
return string.Join(";", sto);
}
else if (destinationType == typeof(InstanceDescriptor) && (value is Color[]))
{
Color[] col = (Color[])value;
string[] sto = new string[col.Length];
for(int i = 0; i < col.Length; i++)
{
sto[i] = Util.ConvertToHexColor(col[i]);
}
return new InstanceDescriptor(typeof(Color[]).GetConstructor(new Type[] { typeof(string) }), new string[] { string.Join(";", sto) } );
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
以上来用到了两个方法,分别是将颜色转成十六进制格式的值,另一个是反转:
using System.Drawing;
using System.Web.UI.WebControls;
internal class Util
{
//将颜色转换为十六进制
internal static string ConvertToHexColor(Color c)
{
WebColorConverter wcc = new WebColorConverter();
return wcc.ConvertToString(c);
}
//将颜色转换为十六进制
internal static Color FromHexColor(string c)
{
WebColorConverter wcc = new WebColorConverter();
return (Color)wcc.ConvertFromString(c);
}
}
using System.Web.UI.WebControls;
internal class Util
{
//将颜色转换为十六进制
internal static string ConvertToHexColor(Color c)
{
WebColorConverter wcc = new WebColorConverter();
return wcc.ConvertToString(c);
}
//将颜色转换为十六进制
internal static Color FromHexColor(string c)
{
WebColorConverter wcc = new WebColorConverter();
return (Color)wcc.ConvertFromString(c);
}
}