How does Vista generate the icon for documents associated to my application?

耗尽温柔 提交于 2019-12-04 10:11:48

The icons and file associations for file extensions are listed in the registry. More specifically, HKEY_CLASSES_ROOT\.ext entries contain the content type, perceived type and in the (Default) value is the actual association. For example, .cs files have default value of VisualStudio.cs.9.0 (I've got VS 2008). You can check the HKEY_CLASSES_ROOT\VisualStudio.cs.9.0 to see the actual icon, program and commands associated with this file type. In particular, the HKEY_CLASSES_ROOT\VisualStudio.cs.9.0\DefaultIcon is the entry that tells Explorer which icon to show for files of this type. It points to a binary and a resource ID in that binary.

When you associate a file type with a program through the right click -> Open With..., you don't specify a default icon, so Explorer takes the icon of your app and overlays it over a generic document icon.

The right approach would be to include as part of your setup the appropriate registry entries to associate the file type with your application and your icon. The exact registry values you need to include depend on the commands you want added to the context menu for that file type, but at the very least you want the Open command. In the case of .cs files, you can see that there is an entry HKEY_CLASSES_ROOT\VisualStudio.cs.9.0\Shell\Commands\Open with (Default) value containing the application to start when the Open command is invoked. (You can ignore the ddeexec part for now)

Keep in mind that the HKEY_CLASSES_ROOT is a mapped view of two registry branches: HKEY_CURRENT_USER\Software\Classes and HKEY_LOCAL_MACHINE\Software\Classes. If you want your file association to be for all users, you need to write to HKEY_LOCAL_MACHINE. Your setup needs to run as admin to do that. Otherwise, attempting to write to HKEY_CLASSES_ROOT will either fail with access denied or write to HKEY_CURRENT_USER and do the association only for the current user. (Which of the two exactly will happen depends on several things, like what the OS is, whether the user is admin but not elevated and so on)

You can read about all this in Customizing File Types (Files Associations) section on MSDN. In particular, File Types and Registering Programs with Client Programs would give you the basics of how exactly to do this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!