tags:

views:

704

answers:

6

Hi, im on to write a large C# Application. The point is that the colors of the controls should be adjustable by the user of the application. It would be really nice if there are any solution to override(only for the context of this application) the System.Drawing.SystemColors, so that i do not have to set the value of every single control by hand. Do anybody know an solution for my problem which is that simple? Thanks

A: 

You shouldn't need to override the system defaults but you are able to define your own colours.

Color NastyColour = Color.FromArgb(1, 2, 3);

1 = Red 2 = Green 3 = Blue

Ian Roke
+1  A: 

Look at Application Setting bindings. Not sure how you would do this for all controls, but simply recursing through the control tree should be sufficient.

leppie
A: 

Unfortunately it's not possible to modify the Windows colour scheme just for your application.

Winforms makes it possible to change things like the background colour for all controls on a form, but for many areas (such as the bevel colours on buttons, or window title bars), you'll probably need to resort to painting the control yourself.

Tim Robinson
+1  A: 

I think your best approach would be to inherit each control and set its default display properties. This would give you a library of the standard WinForms controls that you could easily customize and re-use. More information here (in VB, I couldn't find examples in C#).

Jamie Ide
A: 

I wrote the code below to do something like this. I'm not particularly happy with it as it needs specialized handling for any controls that are out of the ordinary, but it did the job. I keep an instance of Painter around, and call Apply every time I create a form, passing the form as the argument. It recurses through all the child controls, altering their appearance

public class Painter
{
    Color foreColor;
    Color backColor;
    Color altBackColor;
    Color buttonColor;
    Font font;

    public Painter(Color foreColor, Color backColor, Color altBackColor, Color buttonColor, Font font)
    {
        this.foreColor=foreColor;
        this.backColor=backColor;
        this.altBackColor=altBackColor;
        this.buttonColor=buttonColor;
        this.font=font;
    }    

    public void Apply(Control c)
    {
        if(c==null)
            return;

        c.ForeColor = foreColor;

        c.BackColor = (c is Button ) ? buttonColor
                                     : backColor;

        if (c is DataGridView)
        {
            var dgv = (DataGridView) c;
            dgv.BackgroundColor = BackColor;
            dgv.AlternatingRowsDefaultCellStyle.BackColor = altBackColor;
            dgv.ColumnHeadersDefaultCellStyle.BackColor = buttonColor;
        }

        c.Font = font;

        foreach(Control child in c.Controls)
            Apply(child);
    }
}
Lee
A: 

Spend $1000 and get a copy of DevExpress. We're writing a large application using their framework, and the skinning ability is great.

I know this doesn't sound like the best answer, but if you're looking for application wide skinning ability, a third-party library may be appropriate.

Joe