以编程方式更改可执行文件的图标 [英] Programmatically change the icon of the executable

查看:135
本文介绍了以编程方式更改可执行文件的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个名为 WeatherBar 的应用程序。它的主要功能是基于它与Windows 7任务栏的交互 - 它根据特定位置的天气条件更改图标。

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.

我正在使用的图标应用程序都存储在已编译的本机资源文件(.res)中 - 我使用它而不是仅用于图标的嵌入式资源清单。默认情况下,我修改主窗体的Icon属性以相应地更改图标,只要图标没有固定到任务栏,它就可以正常工作。当它被固定时,任务栏中的图标会自动切换到可执行文件的默认图标(资源文件中的索引为0)。

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.

我认为我需要更改可执行文件的图标,因此使用 UpdateResource ,但我对此并不完全确定。我的可执行文件没有经过数字签名,所以不应该修改它。

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?

推荐答案

如果你想以编程方式执行此操作,我首先要查看可移植可执行文件格式维基百科条目)。资源部分(.rsrc,请参见第6.9节)应包含图标。使用此信息,您可以编写一个工具来修改图标。

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.

如果您只想快速更改现有文件中的图标,您可能能够破解它在Visual Studio资源编辑器中。我通过删除旧图标并添加新图标来测试此文件。在Explorer中将.exe图标更改为新图标,并在我将其拖动到开始菜单时出现新图标。

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.

- 编辑 -

是的,我同意使用 UpdateResource 是一个很好的方法。 以下是我发现使用C ++函数执行此操作的示例,以及 UpdateResource FindResource

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.

这篇关于以编程方式更改可执行文件的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆