5
如何從應用程序內重用應用程序圖標,以便我不必將其嵌入兩次(一次是應用程序圖標,一次是內部使用)?重用.NET應用程序圖標
如何從應用程序內重用應用程序圖標,以便我不必將其嵌入兩次(一次是應用程序圖標,一次是內部使用)?重用.NET應用程序圖標
您可以通過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);
}