简体   繁体   中英

Using the same icon for .exe and Form

In my WinForms app, if I set an icon for my app and an icon for my Form, the icon appears twice in my executable. Is it possible to avoid this?

(This question has been asked before, here , but the answers didn't seem to solve the problem. vanmelle's answer appears to extract only one icon (eg, 16x16), Sunlight's answer extracts only the 32x32, and lc's answer doesn't solve the problem: there is still a duplicated icon in the executable.)

If it's not possible to accomplish this task, why is this? What is it about using the same icon for an executable and a Form that's so hard in WinForms?

This is an inevitable consequence of running managed code on a completely unmanaged operating system. Windows Explorer only knows how to read unmanaged resources. You can see what they look like, use File + Open + File in Visual Studio and select your .exe. You'll typically see three resource groups listed there:

  • RT_MANIFEST, contains the manifest that tells Windows that your program is Vista aware
  • Version, contains the file version resource with values derived from your AssemblyInfo.cs file. You see its content when you use Properties + Details tab in Explorer. Note how the super important [AssemblyVersion] isn't visible in Vista and up
  • Icon, contains the icon you added.

This unmanaged resource data is separate from the managed resources you added. Managed resources are compiled into the assembly manifest. Unmanaged resources are stored in the .rsrc section of the image file. You can override the auto-generated version with the /win32res command line option. Which requires a .res file, a binary file that is generated from a .rc resource script by the rc.exe resource compiler. An age old Windows SDK tool.

This may change some day, the super-secret Midori project is a rumored to focus on a managed operating system. For now, we'll have to make do with the glue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM