以编程方式更改可执行文件的图标
我正在开发一个名为 WeatherBar 的应用程序。其主要功能基于与 Windows 7 任务栏的交互 - 它根据特定位置的天气条件更改图标。
我在应用程序中使用的图标都存储在编译的本机资源文件 (.res) 中 - 我使用它而不是仅用于图标的嵌入式资源清单。默认情况下,我修改主窗体的 Icon 属性来相应地更改图标,只要图标未固定到任务栏,它就可以正常工作。当它被固定时,任务栏中的图标会自动切换到可执行文件的默认图标(资源文件中的索引为 0)。
经过一些研究后,我发现更改图标的一种方法是更改快捷方式图标(因为所有固定的应用程序实际上都是存储在用户文件夹中的快捷方式)。但这没有用。
我假设我需要更改可执行文件的图标,因此使用 UpdateResource
,但我对此并不完全确定。我的可执行文件没有经过数字签名,因此修改它应该不成问题。
解决这个问题的方法是什么?
I am developing an application called WeatherBar. Its main functionality is based on its interaction with the Windows 7 taskbar — it changes the icon depending on the weather conditions in a specific location.
The icons I am using in the application are all stored in a compiled native resource file (.res) — I am using it instead of the embedded resource manifest for icons only. By default, I modify the Icon property of the main form to change the icons accordingly and it works fine, as long as the icon is not pinned to the taskbar. When it gets pinned, the icon in the taskbar automatically switches to the default one for the executable (with index 0 in the resource file).
After doing a little bit of research, I figured that a way to change the icon would be changing the shortcut icon (as all pinned applications are actually shortcuts stored in the user folder). But it didn't work.
I assume that I need to change the icon for the executable, and therefore use UpdateResource
, but I am not entirely sure about this. My executable is not digitally signed, so it shouldn't be an issue modifying it.
What would be the way to solve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想以编程方式执行此操作,我将首先查看 便携式可执行文件格式(维基百科条目)。资源部分(.rsrc,请参阅第 6.9 节)应包含该图标。使用此信息,您可以编写一个工具来修改图标。
如果您只想快速更改现有文件中的图标,您可以在 Visual Studio 资源编辑器中对其进行修改。我通过删除旧图标并添加新图标来使用文件对此进行测试。 .exe 图标在资源管理器中更改为新图标,并且当我将其拖动到“开始”菜单时,新图标出现在“开始”菜单上。
-- 编辑 --
是的,我同意使用 UpdateResource 是一个很好的方法。 这是一个示例我发现使用 C++ 函数来执行此操作,以及 UpdateResource 和 FindResource。
If you want to do this programatically, I would start by looking at the Portable Executable file format (Wikipedia entry). The resources section (.rsrc, see section 6.9) should contain the icon. Using this information, you can write a tool to modify the icon.
If you just want to quickly change an icon in an existing file, you might be able to hack it up in the Visual Studio resource editor. I tested this with a file by deleting the old icon and adding a new one. The .exe icon changed in Explorer to the new icon, and the new icon appeared on the Start menu when I dragged it there.
-- Edit --
Yes, I agree that using UpdateResource is a good approach. Here is an example I found of using C++ functions to do so, and a P/Invoke signature for UpdateResource and FindResource.
这是取自 http ://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/9e23a82c-8bed-4b96-8b9a-4c2b6136a622/
可能会有所帮助。
This was taken from http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/9e23a82c-8bed-4b96-8b9a-4c2b6136a622/
It may help.
我决定实施一个解决方法 - 窗口缩略图中的图标将发生变化(在 Windows 7 中是可能的)。如果取消固定图标,用户可以看到图标发生变化。如果它被固定,缩略图将根据当前的天气状况而变化。
在我看来,固定图标的结构(实际上是快捷方式)不允许动态图标更改。如果我错了,我愿意就此发表评论和想法。
I decided to implement a workaround - the icon will change in the thumbnail for the window (it is possible in Windows 7). If the icon is unpinned, the user can see the icon changing. In case it is pinned, the thumbnail will change according to the current weather conditions.
Seems to me like the structure of pinned icons (being a shortcut, in fact) doesn't allow dynamic icon change. If I am wrong, I am open for comments and ideas on this.