Change Default Winform Icon Across Entire App

后端 未结 5 555
甜味超标
甜味超标 2020-12-17 16:32

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 d

相关标签:
5条回答
  • 2020-12-17 17:16

    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.

    0 讨论(0)
  • 2020-12-17 17:19

    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...

    0 讨论(0)
  • 2020-12-17 17:20

    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.

    0 讨论(0)
  • 2020-12-17 17:20

    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.

    0 讨论(0)
  • 2020-12-17 17:28

    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.

    0 讨论(0)
提交回复
热议问题