Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
780 views
in Technique[技术] by (71.8m points)

visual studio 2008 - Change Default Winform Icon Across Entire App

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...