View Full Version : SHGetFileInfo()
PeaceWorld
10th November 2006, 12:39 PM
How to Use the SHGetFileInfo Function to Get the Icons That Are Associated with Files in Visual C#/vb .NET Compact Framework ( in PocketPC, SmartPhone, and WinCE Devices )
Thanx.
M. GANESAN
dotfred
10th November 2006, 01:32 PM
How to Use the SHGetFileInfo Function to Get the Icons That Are Associated with Files in Visual C#/vb .NET Compact Framework ( in PocketPC, SmartPhone, and WinCE Devices )
Thanx.
M. GANESAN
I would suggest you to either changing the language (program in c++) or use wrapped unmanaged code to achieve that.
I'm sure you will find some code on http://www.codeproject.com that encapsulate the SHGetFileInfo call.
Cheers,
.Fred
bepe
10th November 2006, 02:53 PM
This works for me
Just call: GetIcon(".txt") and you'll get the Picture index inside
packageView->ImageList->Images
#pragma region GetIcon
public:
[StructLayout(LayoutKind::Sequential)]
value struct SHFILEINFO
{
IntPtr hIcon;
int iIcon;
UInt32 dwAttributes;
[MarshalAs(UnmanagedType::ByValTStr,SizeConst=260)]
String^ szDisplayName;
[MarshalAs(UnmanagedType::ByValTStr,SizeConst=80)]
String^ szTypeName;
};
static UInt32 SHGFI_ICON = 0x100;
static UInt32 SHGFI_SMALLICON = 0x1;
static UInt32 SHGFI_USEFILEATTRIBUTES = 0x000000010;
static UInt32 FILE_ATTRIBUTE_NORMAL = 0x00000080;
[DllImport("shell32.dll")] static IntPtr SHGetFileInfo(String^ pszPath, UInt32 dwFileAttributes, IntPtr psfi, UInt32 cbSizeFileInfo, UInt32 uFlags);
[DllImport("User32.dll")] static int DestroyIcon(IntPtr hIcon);
int GetIcon(String^ file)
{
if(packageView->ImageList->Images->ContainsKey(file)) return packageView->ImageList->Images->IndexOfKey(file);
IntPtr pShinfo = Marshal::AllocHGlobal(Marshal::SizeOf(SHFILEINFO:: typeid));
SHGetFileInfo(file, FILE_ATTRIBUTE_NORMAL, pShinfo, Marshal::SizeOf(SHFILEINFO::typeid), SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_SMALLICON);
SHFILEINFO^ i = (SHFILEINFO^)Marshal::PtrToStructure(pShinfo, SHFILEINFO::typeid);
Marshal::FreeHGlobal(pShinfo);
packageView->ImageList->Images->Add(file, System::Drawing::Icon::FromHandle(i->hIcon)->ToBitmap());
DestroyIcon(i->hIcon);
return packageView->ImageList->Images->IndexOfKey(file);
}
#pragma endregion
BTW:
packageView is a TreeView...
and yes, you should use c++ :)
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.