IconReference – Using Own Icons
Since the release of Windows 7, lots of .NET developers felt the need to use the new features introduced by the new OS. One of these features is the possibility to have jump lists for the application icon in the system taskbar.
Natively, using the new taskbar capabilities is only possible by using the Win32 API. But there is also a managed library for .NET developers – the Windows API Code Pack for .NET Framework, that allows the use of the new Windows 7 capabilities (not only limted to the taskbar) from a .NET application without using the Win32 API directly.
Although this library isn’t very complicated when it using it’s capabilities, there are some serious limitations. One of them is the possibility to assign icons to a JumpListLink (or a JumpListItem).
When creating a JumpListLink, I was thinking that referencing an icon to it will be fairly easy. I did not want to use icons included in a pre-built executable or DLL (as it is shown in most samples online), but rather use my own icons. Doesn’t seem like it is going to be really hard, right?
Well it was a bit tricky. I tried using this code to reference an icon to the JumpListLink:
JumpListLink jll = new JumpListLink ("about:blank", "JumpListLink");
jll.IconReference = new IconReference ("C:\\Users\\Dennis\\Documents\\Visual Studio 2008\\Projects\\IconLib\\32.ico");
But this wasn’t the right thing to do, since I was getting an exception that stated - Reference path is invalid. After checking the path to make sure that the icon is there I set a breakpoint, so I can track the actual path that was used. It was used correctly, but the icon cannot be loaded anyway.
After a few days of searching, I finally found a solution. What is needed, is a Native Resource Template, that can be easily created in Visual Studio from the File > New > File… menu.
Once the file opens, right click on the designer area and select the Add Resource… option. From the dialog that opens, select the Icon option.
When you click Import…, you can select as many icons as you want that will be added to the resource file. Once added, save the resource file as a 32-bit Resource File (*.res). Now, all you have to do is reference the newly created resource file in your project.
By default, in every new Windows Forms C# application, the application resources are managed through an embedded manifest. I referenced the resource file instead. To do this, open the project properties (the Properties item in the Solution Explorer) and select the Application tab. Find the Resources box and select the Resource File option instead of the default Icon and Manifest. Browse and select the resource file you created.
Once you build the project, you can reference the icons from the resource file:
JumpListLink jll = new JumpListLink ("about:blank", "JumpListLink");
jll.IconReference = new IconReference (Assembly.GetEntryAssembly().Location, 0);