Programmatically change the icon of the executable

老子叫甜甜 提交于 2019-11-28 23:36:05

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.

 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.

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.

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