代码之家  ›  专栏  ›  技术社区  ›  Zach Johnson

重复使用。NET应用程序图标

  •  5
  • Zach Johnson  · 技术社区  · 16 年前

    2 回复  |  直到 16 年前
        1
  •  4
  •   Josip Medved    16 年前
        2
  •  6
  •   Community Mohan Dere    9 年前

    static Icon GetAppIcon() {
        var fileName = Assembly.GetEntryAssembly().Location
        System.IntPtr hLibrary = NativeMethods.LoadLibrary(fileName);
        if (!hLibrary.Equals(System.IntPtr.Zero)) {
            System.IntPtr hIcon = NativeMethods.LoadIcon(hLibrary, "#32512");
            if (!hIcon.Equals(System.IntPtr.Zero)) {
                return Icon.FromHandle(hIcon);
            }
        }
        return null; //no icon was retrieved
    }
    

    此外,本机签名包括:

    private static class NativeMethods {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern internal IntPtr LoadIcon(IntPtr hInstance, string lpIconName);
    
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
         static extern internal IntPtr LoadLibrary(string lpFileName);
    }