Réutilisant L'icône d'application .NET
-
20-09-2019 - |
Question
Comment puis-je réutiliser l'icône de l'application à partir de mon application, je n'ai pas intégré deux fois (une fois pour l'icône de l'application et une fois pour usage interne)?
La solution
Vous pouvez le lire en retour via les appels P / Interop. Il va quelque chose comme ceci:
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
}
En outre, les signatures natives sont:
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);
}
Autres conseils
Il semble plus facile à utiliser Icon.ExtractAssociatedIcon , comme indiqué dans cette question connexe: Éviter les ressources en double icône dans un projet .NET (C #)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow