> thank you for your reply
>
[quoted text clipped - 31 lines]
>>>
>>> Thanks
Hello:
I do not sure is there any direct method to get icon in Dotnet.
However, in my job, I use Pinvoke to call API to do so.
The follow code is in C#,
class ClsGetIcon
{
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet =
System.Runtime.InteropServices.CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);
[System.Runtime.InteropServices.DllImport("shell32.dll", CharSet =
System.Runtime.InteropServices.CharSet.Auto)]
static extern uint SHGetFileInfo(string pszPath, uint
dwFileAttributes, ref SHFILEINFO sfi, uint cbFileInfo, uint uFlags);
/*
DWORD WINAPI SHGetFileInfo(
LPCTSTR pszPath,
DWORD dwFileAttributes,
SHFILEINFO FAR* psfi,
UINT cbFileInfo,
UINT uFlags
);
*/
//#define SHGFI_ICON 0x000000100 // get icon
//#define SHGFI_SMALLICON 0x000000001 // get small icon
const uint SHGFI_ICON = 0x000000100;
const uint SHGFI_SMALLICON = 0x000000001;
[StructLayout(LayoutKind.Sequential)]
struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
/*
typedef struct _SHFILEINFO {
HICON hIcon;
int iIcon;
DWORD dwAttributes;
TCHAR szDisplayName[MAX_PATH];
TCHAR szTypeName[80];
} SHFILEINFO;
*/
static public Icon GetAppIcon(string appPath)
{
SHFILEINFO sfi = new SHFILEINFO();
sfi.hIcon = IntPtr.Zero;
if (0 != SHGetFileInfo(appPath, 0, ref sfi,
(uint)Marshal.SizeOf(sfi), SHGFI_ICON | SHGFI_SMALLICON) &&
sfi.hIcon!=IntPtr.Zero)
{
Icon newIcon = Icon.FromHandle(sfi.hIcon);
Icon returnIcon = (Icon)newIcon.Clone();
DestroyIcon(newIcon.Handle);
return returnIcon;
}
else
return null;
}
}

Signature
Jacky Kwok
jacky@alumni_DOT_cuhk_DOT_edu_DOT_hk
jacky@compose_DOT_com_DOT_hk
Sergei Gnezdov - 22 Nov 2006 18:51 GMT
As far as I know pinvoke is the solution if you want to save space. Thanks
for pinvoke code Ajak.
If you don't want to use pinvoke, then you have to have a second copy of the
icon. It either should be a .NET resource or a standalone icon and you will
have to load it manually just like the pinvoke scenario.
> Hello:
>
[quoted text clipped - 70 lines]
> }
> }
Ajak - 23 Nov 2006 11:55 GMT
Thanks guys
I opted to have a second copy of the icon embedded as resource. Make it to
only contain 16x16 res icon.