[ACCEPTED]-Set same icon for all my Forms-icons

Accepted answer
Score: 59
  1. In the project properties > Application 3 > Icon and Manifest > browse for a *.ico 2 file and add it there.

  2. In the constructor 1 or _Load event of a Form, simply add:

    this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
    
Score: 26

One option would be to inherit from a common 9 base-Form that sets the Icon in the constructor 8 (presumably from a resx). Another option 7 might be PostSharp - it seems like it should be possible 6 to do this (set .Icon) via AOP; not trivial, though. Finally, you 5 could use a simple utility method (perhaps 4 an extension method) to do the same.

Best 3 of all, with the first option, you could 2 probably risk a Ctrl+H (replace all) from : Form or 1 : System.Windows.Forms.Form to : MyCustomForm.

Score: 10

In additional to Marc's recommendation, you 5 may want your forms to automatically inherit 4 the icon of the executing assembly that 3 contains/calls them.
This can be done by 2 adding the following code to your inherited 1 form:

public MyCustomForm()
{
    Icon = GetExecutableIcon();
}

public Icon GetExecutableIcon()
{
    IntPtr large;
    IntPtr small;
    ExtractIconEx(Application.ExecutablePath, 0, out large, out small, 1);
    return Icon.FromHandle(small);
}

[DllImport("Shell32")]
public static extern int ExtractIconEx(
    string sFile,
    int iIndex,
    out IntPtr piLargeVersion,
    out IntPtr piSmallVersion,
    int amountIcons);
Score: 7

This is the way to set the same icon to 6 all forms without having to change one by 5 one. This is what I coded in my applications.

FormUtils.SetDefaultIcon();

Here 4 a full example ready to use.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        //Here it is.
        FormUtils.SetDefaultIcon();

        Application.Run(new Form());
    }
}

Here is the 3 class FormUtils:

using System.Drawing;
using System.Windows.Forms;

public static class FormUtils
{
    public static void SetDefaultIcon()
    {
        var icon = Icon.ExtractAssociatedIcon(EntryAssemblyInfo.ExecutablePath);
        typeof(Form)
            .GetField("defaultIcon", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
            .SetValue(null, icon);
    }
}

And here the class EntryAssemblyInfo. This 2 is truncated for this example. It is my 1 custom coded class taken from System.Winforms.Application.

using System.Security;
using System.Security.Permissions;
using System.Reflection;
using System.Diagnostics;

public static class EntryAssemblyInfo
{
    private static string _executablePath;

    public static string ExecutablePath
    {
        get
        {
            if (_executablePath == null)
            {
                PermissionSet permissionSets = new PermissionSet(PermissionState.None);
                permissionSets.AddPermission(new FileIOPermission(PermissionState.Unrestricted));
                permissionSets.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));
                permissionSets.Assert();

                string uriString = null;
                var entryAssembly = Assembly.GetEntryAssembly();

                if (entryAssembly == null)
                    uriString = Process.GetCurrentProcess().MainModule.FileName;
                else
                    uriString = entryAssembly.CodeBase;

                PermissionSet.RevertAssert();

                if (string.IsNullOrWhiteSpace(uriString))
                    throw new Exception("Can not Get EntryAssembly or Process MainModule FileName");
                else
                {
                    var uri = new Uri(uriString);
                    if (uri.IsFile)
                        _executablePath = string.Concat(uri.LocalPath, Uri.UnescapeDataString(uri.Fragment));
                    else
                        _executablePath = uri.ToString();
                }
            }

            return _executablePath;
        }
    }
}
Score: 2

I wasn't sure if I wanted to use inheritance 4 here, so I used an extension method:

public static class MyExtensionMethods
{
    public static void SetAppIcon(this Form form)
    {
        form.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
    }
}

Then 3 in any form's constructor:

this.SetAppIcon();

Note: this will 2 cause a crash if you try to run the application 1 from a network location

Score: 1

Alternative to setting in the constructor 2 is overriding the Owner property, and taking 1 the icon of the owner form.

public new Form Owner {
    set {
        this.Icon = (value == null ? null : value.Icon);
        base.Owner = value;
    }

    get {
        return base.Owner;
    }
}
Score: 1

It is an old question, but this method worked 24 for me to get the same icon in all forms 23 without having to add it in every form's 22 properties.

First I added a new icon in the 21 'Resources' node under 'Properties' (add 20 resource => New Icon).

By default some icon 19 is created and stored in your resources 18 location (look for the 'Filename' property 17 on the icon).

Assuming you have a .ico file 16 ready, you can copy it to that folder.

Delete 15 the one Visual Studio created in that folder 14 and rename your own to the exact same name.

Visual 13 Studio will prompt you if you want to reload 12 the resource since it was modified. (click 11 'Yes')

That way you have your Logo available 10 under resources.

Now I have made a general 9 method to change the form title to my default 8 and set the form icon and call that in every 7 form right after InitializeComponent();

How 6 that looks in the form cs (m is the class 5 that holds the general method):

m.set_form_defaults(this, "Title here");

And this 4 is the method itself:

    public void set_form_defaults(Form frm,string frmTitle)
    {

        frm.Icon = ((System.Drawing.Icon)(Properties.Resources.Logo_V2));
        frm.Text = frmTitle + " " + show_current_server();

    }

Of course you don't 3 need to add the form title here, but that 2 is just because I want to show some stored 1 properties to the user (current server)

More Related questions