12
votes

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?

3
@Vivek That doesn't help the problem and is not a solution.Den Delimarsky
You cannot use UpdateResource, the .exe file is locked. Vivek's link is about as good as it is going to get. There's a nice wrapper in the Window API Code Pack: code.msdn.microsoft.com/WindowsAPICodePackHans Passant
I am actually using the Windows API Code Pack to manage JumpLists and the progress in the taskbar, but I see no way (at this moment) to replace the executable icon.Den Delimarsky

3 Answers

4
votes

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.

2
votes
 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.

0
votes

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.