windows 以编程方式更改可执行文件的图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2539095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically change the icon of the executable
提问by Den Delimarsky
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.
我正在开发一个名为WeatherBar的应用程序。它的主要功能基于它与 Windows 7 任务栏的交互——它根据特定位置的天气条件更改图标。
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).
我在应用程序中使用的图标都存储在编译的本机资源文件(.res) 中——我使用它而不是仅用于图标的嵌入资源清单。默认情况下,我修改主窗体的 Icon 属性以相应地更改图标,只要图标未固定到任务栏,它就可以正常工作。当它被固定时,任务栏中的图标会自动切换到可执行文件的默认图标(资源文件中的索引为 0)。
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.
我假设我需要更改可执行文件的图标,因此使用UpdateResource
,但我对此并不完全确定。我的可执行文件没有数字签名,所以修改它应该不是问题。
What would be the way to solve this issue?
解决这个问题的方法是什么?
采纳答案by Paul Williams
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.
如果您想以编程方式执行此操作,我将首先查看Portable Executable 文件格式(维基百科条目)。资源部分(.rsrc,请参阅第 6.9 节)应包含图标。使用这些信息,您可以编写一个工具来修改图标。
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.
如果您只想快速更改现有文件中的图标,您可以在 Visual Studio 资源编辑器中修改它。我通过删除旧图标并添加新图标来使用文件对此进行了测试。资源管理器中的 .exe 图标更改为新图标,当我将它拖到那里时,新图标出现在“开始”菜单上。
-- Edit --
- 编辑 -
Yes, I agree that using UpdateResourceis a good approach. Here is an exampleI found of using C++ functions to do so, and a P/Invoke signature for UpdateResourceand FindResource.
是的,我同意使用UpdateResource是一个好方法。 这是我发现的使用 C++ 函数执行此操作的示例,以及UpdateResource和FindResource的 P/Invoke 签名。
回答by Den Delimarsky
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.
我决定实施一个解决方法 - 窗口缩略图中的图标会发生变化(在 Windows 7 中是可能的)。如果图标被取消固定,用户可以看到图标在变化。如果它被固定,缩略图将根据当前的天气状况而变化。
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.
在我看来,固定图标的结构(实际上是一种快捷方式)不允许动态图标更改。如果我错了,我愿意就此发表评论和想法。
回答by Art W
private void button1_Click(object sender, EventArgs e)
{
String path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
String name = "test";
Shell32.Shell shl = new Shell32.ShellClass();
// Optional code to create the shortcut
System.IO.StreamWriter sw = new System.IO.StreamWriter(path + @"\" + name + ".lnk", false);
sw.Close();
// End optional code
Shell32.Folder dir = shl.NameSpace(path);
Shell32.FolderItem itm = dir.Items().Item(name + ".lnk");
Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink;
// Optional code to create the shortcut
lnk.Path = Environment.GetFolderPath(Environment.SpecialFolder.System)
+ @"\notepad.exe";
lnk.Description = "nobugz was here";
lnk.Arguments = @"c:\sample.txt";
lnk.WorkingDirectory = @"c:\";
// End optional code
lnk.SetIconLocation(Environment.GetFolderPath(Environment.SpecialFolder.System)
+ "cmd.exe", 1);
lnk.Save(null);
}
This was taken from http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/9e23a82c-8bed-4b96-8b9a-4c2b6136a622/
It may help.
它可能会有所帮助。