This turned out to be a tricky job, because the icons are stored in the resource as an unmanaged item. So the icon has to be loaded unmanaged, and then converted into the managed realm. The icon was loaded by opening the Resource View, and right-clicking on the Icon folder to insert a new icon.
The following code snippet then loads the Icon from the resources into a managed object:
#include <windows.h>
#include "resource.h"
using namespace System::Reflection::Assembly;
using namespace System::Runtime::InteropServices;
using namespace System::Drawing;
// Get the primary module
Module ^ mod = GetExecutingAssembly()->GetModules()[0];
// Get the instance handle
IntPtr hinst = Marshal::GetHINSTANCE(mod);
// Get the icon as unmanaged
HICON hic = LoadIcon((HINSTANCE) hinst.ToPointer(), MAKEINTRESOURCE(IDI_ICON1));
// import the unmanaged icon into the managed side
Icon^ ic = Icon->FromHandle(IntPtr(hic));
// destroy the unmanaged icon
DestroyIcon(static_cast<HICON>(ic->Handle.ToPointer() ));
Print | posted on Monday, March 17, 2008 8:36 AM