重用.NET应用程序图标

时间:2009-08-29 18:11:17

标签: .net assemblies icons embedded-resource reusability

如何从我的应用程序中重用应用程序图标,这样我就不必嵌入两次(一次用于应用程序图标,一次用于内部使用)?

2 个答案:

答案 0 :(得分:6)

答案 1 :(得分:4)

您可以通过P / Interop通话回读。它是这样的:

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);
}