views:

1095

answers:

5

Can I change the default icon used on a Winform?

Most of my forms have their icon property set to a custom icon. For the few forms that slip through the cracks, I don't want the generic "hey look, he made this in visual studio" icon.

One solution is to tediously check every one of my forms to make sure they either have a custom icon set or have ShowIcon set to False.

Another solution is to have every one of my forms inherit from a base class that sets a custom icon in the constructor.

Aside from those solutions, what other options do I have?

EDIT: I was hoping there would be a way to replace the source of the stock icon with my own. Is it in a resource file somewhere? Or is it embedded in a .NET dll that I can't (or really, really shouldn't) modify?

BOUNTY EDIT: Is there a way to accomplish this without editing or writing a single line of code? I don't care how impractical, complicated, waste-of-time the solution is... I just want to know if it's possible. I need to satisfy my curiosity.

+3  A: 

The base class option is the one that we use.

If you are looking for an alternative (not necessarily good ones), you could: 1. Use IOC to instantiate all of your forms and modify the IOC container to set the application icon. 2. Use AOP to insert code into all of the forms that sets the application icon.

Personally, I'd just use the base class...

consultutah
A: 

If all your forms are in just one project then you can take the dll of the project and use reflection to get every type in the dll. If the type derives from Form you can set the type's Icon property to whatever you want. I am not sure what the performance overhead will be if the project is very big.

IordanTanev
+1  A: 

Hello,

If you want to update all the icons by another one, you can build a small app that edits all the *.Designer.vb files (in vb.net) and adding the folowing line to InitializeComponent:

Me.Icon = New System.Drawing.Icon("C:\PathTo\icon.ico")

Hope it helps.

Sebastian
A: 

My useful answer:

No

Would be a nice feature for microsoft to implement though, since most apps use the same icon across the entire application.

Jage
+8  A: 

The default icon is embedded in the winforms dll - looking at reflector (DefaultIcon) it is:

defaultIcon = new Icon(typeof(Form), "wfc.ico");

There is no magic in there that checks another common location, so you can't do it without changing code.

You could always embrace the forces of darkness with field-based reflection? Note: this is hacky and brittle. On your own head! But it works:

[STAThread]
static void Main() {
    // pure evil
    typeof(Form).GetField("defaultIcon",
            BindingFlags.NonPublic | BindingFlags.Static)
        .SetValue(null, SystemIcons.Shield);

    // all forms now default to a shield
    using (Form form = new Form()) {
        Application.Run(form);
    }
}

To do it properly; two common options;

  • a base Form class which has the icon set
  • a factory Form method - perhaps something like:

code:

public static T CreateForm<T>() where T : Form, new() {
    T frm = new T();
    frm.Icon = ...
    // any other common code
    return frm;
}

Then instead of:

using(var frm = new MySpecificForm()) {
    // common init code
}

Something like:

using(var frm = Utils.CreateForm<MySpecificForm>()) {

}

Of course - that isn't much prettier! Another option might be a C# 3.0 extension method, perhaps as a fluent API:

public static T CommonInit<T>(this T form) where T : Form {
    if(form != null) {
        form.Icon = ...
        //etc
    }
    return form;
}

and

using(var frm = new MySpecificForm().CommonInit()) {
    // ready to use
}

This is then just a .CommonInit() away from your existing code.

Marc Gravell
Accepted for actually finding the icon... close enough
Kyle Gagnet
This is absolute mega-cool. Just saved me from running on 50 forms...
Daniel Mošmondor