2012-01-28 39 views
1

我能夠使用下面包含的API成功提取文件系統驅動器,文件夾和文件的圖標。有關DLL導入等其他信息,幫助我得到這一點,可以發現here。通過調用方法GetExtraLargeIconForFolder,我在圖標中獲得了48x48大小的圖像。如何確定用於SHGetImageList的桌面和網絡的圖標索引?

public enum ImageListIconSize : int 
{ 
    Large = 0x0, 
    Small = 0x1, 
    ExtraLarge = 0x2, 
    Jumbo = 0x4 
} 

private static IImageList GetSystemImageListHandle(ImageListIconSize size) 
{ 
    IImageList iImageList; 
    Guid imageListGuid = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); 
    int ret = SHGetImageList(
     (int)size, 
     ref imageListGuid, 
     out iImageList 
     ); 
    return iImageList; 
} 

public static Icon GetExtraLargeIconForFolder(string path) 
{ 
    SHFILEINFO shinfo = new SHFILEINFO(); 
    IntPtr retVal = SHGetFileInfo(
     path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), 
     (int)(SHGetFileInfoConstants.SHGFI_SYSICONINDEX | 
       SHGetFileInfoConstants.SHGFI_ICON)); 

    int iconIndex = shinfo.iIcon; 
    IImageList iImageList = 
     (IImageList)GetSystemImageListHandle(ImageListIconSize.ExtraLarge); 
    IntPtr hIcon = IntPtr.Zero; 
    if (iImageList != null) 
    { 
     iImageList.GetIcon(iconIndex, 
      (int)ImageListDrawItemConstants.ILD_TRANSPARENT, ref hIcon); 
    } 

    Icon icon = null; 
    if (hIcon != IntPtr.Zero) 
    { 
     icon = Icon.FromHandle(hIcon).Clone() as Icon; 
     DestroyIcon(shinfo.hIcon); 
    } 
    return icon; 
} 

在Windows資源管理器中,可以看到桌面,網絡和計算機的圖標。如何獲得這些文件系統節點的正確圖標索引?

回答

3

你快到了。您仍然使用SHGetFileInfo,但您需要在flags參數中傳遞SHGFI_PIDL

然後,您需要通過傳遞PIDL而不是路徑來指定感興趣的shell對象。致電SHGetSpecialFolderLocation獲取PIDL。向該例程傳遞CSIDL值,例如, CSIDL_DESKTOP,CSIDL_DRIVES,CSIDL_NETWORK

+0

如何通過CSIDL?我把它作爲一個字符串或像Shell32.CSIDL?它甚至沒有初始化 – 2012-01-29 23:50:32

+0

@MurHafSoz我不能在評論中回答。這聽起來像你試圖挑戰SHGetSpecialFolderLocation並陷入困境。如果無法解決問題,請提出問題。 – 2012-01-30 11:14:22

相關問題