|
This is an example how to retrieve an icon for a file class without a file.
Reference: MSDN/HOWTO: Retrieve an Icon for a File Class Without a File
Downloads
IconExtractor.cs - C# source file
IconExtractor.zip - C# demo soulution
Explanation
For retrieving an icon for a file class without a file we use a Shell32.dll library.
First of all we should to import a function that will return a handle to the required icon.
[DllImport("Shell32.dll")]
private static extern IntPtr SHGetFileInfo(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags );
where SHFILEINFO is a structure that has the next definition:
[StructLayout(LayoutKind.Sequential)]
private enum SHGFI
{
SHGFI_ICON = 0x000000100,
SHGFI_DISPLAYNAME = 0x000000200,
SHGFI_TYPENAME = 0x000000400,
SHGFI_ATTRIBUTES = 0x000000800,
SHGFI_ICONLOCATION = 0x000001000,
SHGFI_EXETYPE = 0x000002000,
SHGFI_SYSICONINDEX = 0x000004000,
SHGFI_LINKOVERLAY = 0x000008000,
SHGFI_SELECTED = 0x000010000,
SHGFI_ATTR_SPECIFIED = 0x000020000,
SHGFI_LARGEICON = 0x000000000,
SHGFI_SMALLICON = 0x000000001,
SHGFI_OPENICON = 0x000000002,
SHGFI_SHELLICONSIZE = 0x000000004,
SHGFI_PIDL = 0x000000008,
SHGFI_USEFILEATTRIBUTES = 0x000000010
}
Below is the implemenntation of wraaper for WINAPI for a getting icon:
public static Icon GetIcon( string strPath, bool small, bool selected )
{
SHFILEINFO shfi = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(shfi);
SHGFI flags = SHGFI.SHGFI_USEFILEATTRIBUTES|SHGFI.SHGFI_ICON;
if( selected )
flags |= SHGFI.SHGFI_OPENICON;
if( small )
flags |= SHGFI.SHGFI_SMALLICON;
SHGetFileInfo( strPath,
256,
out shfi,
(uint)cbFileInfo,
flags );
return Icon.FromHandle( shfi.hIcon );
}
Thanks: Oleg Axenow
ALL CODES AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
|