English 中文(简体)
C#: 所有解决方案项目的习俗科
原标题:C#: Custom Colors class accessible to all projects in solution

我有一份清单,列出我用在我解决办法的所有要素(包括多个项目)中的各种习俗。 我可以在其中一个项目中设立一个静态的班子,在这些项目中,我可以向我的色人提供定制名称,然后在不同的地点查阅,但是否为多个项目找到更好的解决办法?

static class MyColors
{
   public static myColor1 = Color.FromArgb(239, 247, 172);
   .
   .
   . 
}

能否将这些习俗的颜色纳入控制的财产网? 例如,我的大多数背景都有单一的习俗颜色,而不是复制欧洲-联合国达尔富尔混合行动的价值观,我希望能够选择我的习俗颜色。 我理解,我可以把它放在方案上,使用MyColors类别,但如果能够把它添加到房地产网中的话,那将是空洞的。

谢谢!

最佳回答

在大韩民国,我即将对如何做到这一点作出长篇回答,但经过一些研究,我描述了你试图做什么!

首先,使用静态的舱面储存你的颜色,是储存你的共同颜色的一种共同和可接受的方式。 你们可以继续采用这种办法。

这里,你需要做些什么才能在设计人中暴露这些颜色:

  1. Create a CustomColors class that encapsulates your custom colors.
  2. Create a TypeConverter that can convert between a string and CustomColor
  3. Create an IExtenderProvider that adds a CustomForeColor and CustomBackColor to all of the controls on your form.
  4. 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.

最终结果:

“entergraph

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
}
问题回答

我只是这样说:固定等级的定义是固定的。

public static class GraphicalProperties
{
    public static readonly Color    ControlBackColor    = Color.FromArgb(128, 32, 48);
    public static readonly Color    ControlForeColor    = Color.White;
    public static readonly Font     ControlFont         = new Font("Consolas", 10);
}

然后,在每一控制方的构造中,你只是补充说:

public MyForm()
{
    InitializeComponent();

    BackColor   = GraphicalProperties.ControlBackColor;
    ForeColor   = GraphicalProperties.ControlForeColor;
    Font        = GraphicalProperties.ControlFont;
}

这很少提供自动处理,也没有将财产格里德包括在内,但我认为非常简单,而且实际可行。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签