Set same icon for all my Forms

前端 未结 8 1506
一向
一向 2020-12-08 19:31

Is there any way to set the same icon to all my forms without having to change one by one? Something like when you setup GlobalAssemblyInfo for all your project

相关标签:
8条回答
  • 2020-12-08 19:53

    In additional to Marc's recommendation, you may want your forms to automatically inherit the icon of the executing assembly that contains/calls them.
    This can be done by adding the following code to your inherited 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);
    
    0 讨论(0)
  • 2020-12-08 19:53

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

    FormUtils.SetDefaultIcon();
    

    Here 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 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 is truncated for this example. It is my 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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 19:54

    I'm not sure if the MS VS designer can deal with Forms that don't derive directly from Form. If not then you may try to copy the main form's icon to all other forms: for each form in Forms collection

    form.icon = MainFrom.Icon
    

    Or perhaps in each Form's _Loaded event:

    Icon = MainFrom.Icon
    
    0 讨论(0)
  • 2020-12-08 19:59

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

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

    0 讨论(0)
  • 2020-12-08 19:59

    Alternative to setting in the constructor is overriding the Owner property, and taking 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;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 20:07
    1. In the project properties > Application > Icon and Manifest > browse for a *.ico file and add it there.

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

      this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
      
    0 讨论(0)
提交回复
热议问题