12

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?

ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
Den
  • 16,686
  • 4
  • 47
  • 87
  • 1
    http://stackoverflow.com/questions/969033/change-pinned-taskbar-icon-windows-7 – Vivek Mar 29 '10 at 15:29
  • 1
    @Vivek That doesn't help the problem and is not a solution. – Den Mar 29 '10 at 15:30
  • 3
    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: http://code.msdn.microsoft.com/WindowsAPICodePack – Hans Passant Mar 29 '10 at 15:48
  • 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 Mar 29 '10 at 15:49
  • http://msdn.microsoft.com/en-us/magazine/dd942846.aspx#id0420051 http://windowsteamblog.com/blogs/developers/archive/2009/07/28/windows-7-taskbar-dynamic-overlay-icons-and-progress-bars.aspx Look into OverlayImage in the APICodePack. – JohnForDummies Mar 29 '10 at 19:28
  • OverlayIcon is a bit of a different thing. The problem with it is that it shows a minimized icon (16x16) in the corner, while in my case it should be displayed as the main application icon. As the last resort - I will use that. – Den Mar 29 '10 at 20:50

3 Answers3

4

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.

Paul Williams
  • 16,585
  • 5
  • 47
  • 82
  • Interesting approach. Although there is a problem - the Resource Editor is a good tool and I used it for a while, but I need to change the icon directly in code. The end-user won't be able to use the resource editor to change the icon every minute. – Den Apr 07 '10 at 10:59
2
 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.

Art W
  • 2,040
  • 1
  • 20
  • 23
1

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.

Den
  • 16,686
  • 4
  • 47
  • 87