(C#)Windows Shell 外壳编程系列5 - 获取图标

(本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢~)

有关 PIDL

PIDL亦有“绝对路径”与“相对路径”的概念。表示“相对路径”的PIDL(本文简称为“相对PIDL”)只有一个ITEMIDLIST结构的元素,用于标识相对于父文件夹的“路径”;表示“绝对路径”的PIDL(简称为“绝对PIDL”)有若干个ITEMIDLIST结构的元素,第一个元素表示外壳名字空间根文件夹(“桌面”)下的某一子文件夹A,第二个元素则表示文件夹A下的某一子文件夹B,其余依此类推。这样绝对PIDL就通过保存一条从“桌面”下的直接子文件夹或文件的绝对PIDL与相对PIDL是相同的,而其他的文件夹或文件的相对PIDL就只是其绝对PIDL的最后一部分了。
为什么要说这些呢?因为有些函数,必须使用绝对PIDL,例如图标,如果不使用绝对PIDL,某些图标是无法正常获得的(驱动器、控制面板等)。
    但使用 EnumObjects 获得的,仅仅是相对PIDL,如果通过相对PIDL获取绝对PIDL呢?我参考了开源项目 C# FileBrowser 中的 PIDL 类

ContractedBlock.gifExpandedBlockStart.gifPIDL.cs
None.gifusing System;
None.gifusing System.Collections.Generic;
None.gifusing System.Text;
None.gifusing System.Collections;
None.gifusing System.Runtime.InteropServices;
None.gif
None.gifnamespace WinShell
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif public class PIDL
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif private IntPtr pidl = IntPtr.Zero;
InBlock.gif
InBlock.gif public PIDL(IntPtr pidl, bool clone)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif if (clone)
InBlock.gif this.pidl = ILClone(pidl);
InBlock.gif else
InBlock.gif this.pidl = pidl;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public PIDL(PIDL pidl, bool clone)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif if (clone)
InBlock.gif this.pidl = ILClone(pidl.Ptr);
InBlock.gif else
InBlock.gif this.pidl = pidl.Ptr;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif
InBlock.gif public IntPtr Ptr 
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif get dot.gif{ return pidl; }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public void Insert(IntPtr insertPidl)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif            IntPtr newPidl = ILCombine(insertPidl, pidl);
InBlock.gif
InBlock.gif            Marshal.FreeCoTaskMem(pidl);
InBlock.gif            pidl = newPidl;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public void Free()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif if (pidl != IntPtr.Zero)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif                Marshal.FreeCoTaskMem(pidl);
InBlock.gif                pidl = IntPtr.Zero;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif
InBlock.gif private static int ItemIDSize(IntPtr pidl)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif if (!pidl.Equals(IntPtr.Zero))
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif byte[] buffer = new byte[2];
InBlock.gif                Marshal.Copy(pidl, buffer, 0, 2);
InBlock.gif return buffer[1] * 256 + buffer[0];
ExpandedSubBlockEnd.gif            }
InBlock.gif else
InBlock.gif return 0;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif private static int ItemIDListSize(IntPtr pidl)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif if (pidl.Equals(IntPtr.Zero))
InBlock.gif return 0;
InBlock.gif else
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif int size = ItemIDSize(pidl);
InBlock.gif int nextSize = Marshal.ReadByte(pidl, size) + (Marshal.ReadByte(pidl, size + 1) * 256);
InBlock.gif while (nextSize > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif                    size += nextSize;
InBlock.gif                    nextSize = Marshal.ReadByte(pidl, size) + (Marshal.ReadByte(pidl, size + 1) * 256);
ExpandedSubBlockEnd.gif                }
InBlock.gif
InBlock.gif return size;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public static IntPtr ILClone(IntPtr pidl)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif int size = ItemIDListSize(pidl);
InBlock.gif
InBlock.gif byte[] bytes = new byte[size + 2];
InBlock.gif            Marshal.Copy(pidl, bytes, 0, size);
InBlock.gif
InBlock.gif            IntPtr newPidl = Marshal.AllocCoTaskMem(size + 2);
InBlock.gif            Marshal.Copy(bytes, 0, newPidl, size + 2);
InBlock.gif
InBlock.gif return newPidl;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public static IntPtr ILCombine(IntPtr pidl1, IntPtr pidl2)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif int size1 = ItemIDListSize(pidl1);
InBlock.gif int size2 = ItemIDListSize(pidl2);
InBlock.gif
InBlock.gif            IntPtr newPidl = Marshal.AllocCoTaskMem(size1 + size2 + 2);
InBlock.gif byte[] bytes = new byte[size1 + size2 + 2];
InBlock.gif
InBlock.gif            Marshal.Copy(pidl1, bytes, 0, size1);
InBlock.gif            Marshal.Copy(pidl2, bytes, size1, size2);
InBlock.gif
InBlock.gif            Marshal.Copy(bytes, 0, newPidl, bytes.Length);
InBlock.gif
InBlock.gif return newPidl;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif

该类实现了 PIDL 的复制和结合功能。现在我们修改 ShellItem 类,使它带有父节点的 IShellFolder 以及提供获取绝对 PIDL 的属性:

None.gifprivate ShellItem m_ParentItem;
None.gif
None.gifpublic ShellItem ParentItem
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif get dot.gif{ return m_ParentItem; }
ExpandedSubBlockStart.gifContractedSubBlock.gif set dot.gif{ m_ParentItem = value; }
ExpandedBlockEnd.gif}
None.gif
ExpandedBlockStart.gifContractedBlock.gif/** <summary>
InBlock.gif/// 绝对 PIDL
ExpandedBlockEnd.gif/// </summary>
None.gifpublic PIDL PIDLFull
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif get
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif        PIDL pidlFull = new PIDL(PIDL, true);
InBlock.gif        ShellItem current = ParentItem;
InBlock.gif while (current != null)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif            pidlFull.Insert(current.PIDL);
InBlock.gif            current = current.ParentItem;
ExpandedSubBlockEnd.gif        }
InBlock.gif return pidlFull;
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}

获取图标
    言归正传,既然已经获得绝对 PIDL,那么获取图标就是很简单的事情了,我们使用的是 SHGetFileInfo 这个API:

None.gif[DllImport("shell32", EntryPoint = "SHGetFileInfo", ExactSpelling = false, 
None.gif    CharSet = CharSet.Auto, SetLastError = true)]
None.gifpublic static extern IntPtr SHGetFileInfo(
None.gif    IntPtr ppidl, 
None.gif    FILE_ATTRIBUTE dwFileAttributes, 
None.gif ref SHFILEINFO sfi, 
None.gif int cbFileInfo, 
None.gif    SHGFI uFlags);
None.gif
None.gif[DllImport("Shell32.dll", CharSet = CharSet.Auto)]
None.gifpublic static extern IntPtr SHGetFileInfo(
None.gif string Path, 
None.gif    FILE_ATTRIBUTE fileAttributes, 
None.gif out SHFILEINFO sfi, 
None.gif int cbFileInfo, SHGFI flags);
None.gif

这里提供了一个重载,你可以选择是通过 PIDL 还是 路径 获取图标(如果是路径,那么仅仅能获取 文件夹/文件 的图标)。

ExpandedBlockStart.gifContractedBlock.gif/** <summary>
InBlock.gif/// 获取小图标索引
ExpandedBlockEnd.gif/// </summary>
None.gifpublic static int GetSmallIconIndex(string strFilename)
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif    SHFILEINFO psfi = new SHFILEINFO();
InBlock.gif    IntPtr ipIcon = SHGetFileInfo(strFilename, 0, out psfi, Marshal.SizeOf(psfi),
InBlock.gif        SHGFI.ICON | SHGFI.SMALLICON | SHGFI.SYSICONINDEX);
InBlock.gif
InBlock.gif return psfi.iIcon;
ExpandedBlockEnd.gif}
None.gif
None.gifpublic static int GetSmallIconIndex(IntPtr ipIDList)
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif    SHFILEINFO psfi = new SHFILEINFO();
InBlock.gif    IntPtr ipIcon = SHGetFileInfo(ipIDList, 0, ref psfi, Marshal.SizeOf(psfi),
InBlock.gif        SHGFI.ICON | SHGFI.PIDL | SHGFI.SMALLICON | SHGFI.SYSICONINDEX);
InBlock.gif
InBlock.gif return psfi.iIcon;
ExpandedBlockEnd.gif}

大家也许会觉得奇怪,GetSmallIconIndex 返回的是 int ,到底要怎么使用?
其实没错,GetSmallIconIndex 仅仅是返回该图标在系统图像列表(System ImageList)的索引(Index)而已。我们只要获取系统图像列表的指针,再把它关联到你的 TreeView 或 ListView ,即可通过 Icon Index 来显示图标了。

None.gifIntPtr m_ipSmallSystemImageList;
None.gifIntPtr m_ipLargeSystemImageList;
None.gif
None.gif//获取系统 ImageList
None.gifSHFILEINFO shfi = new SHFILEINFO();
None.gif
None.gifm_ipSmallSystemImageList = API.SHGetFileInfo("", 0, out shfi, Marshal.SizeOf(typeof(SHFILEINFO)),
None.gif    SHGFI.SYSICONINDEX | SHGFI.SMALLICON | SHGFI.USEFILEATTRIBUTES);
None.gif
None.gifm_ipLargeSystemImageList = API.SHGetFileInfo("", 0, out shfi, Marshal.SizeOf(typeof(SHFILEINFO)),
None.gif    SHGFI.SYSICONINDEX | SHGFI.LARGEICON | SHGFI.USEFILEATTRIBUTES);
None.gif
None.gif//把系统 ImageList 关联到 TreeView 和 ListView
None.gifAPI.SendMessage(Tree1.Handle, API.TVM_SETIMAGELIST, API.TVSIL_NORMAL, m_ipSmallSystemImageList);
None.gifAPI.SendMessage(lvFile.Handle, API.LVM_SETIMAGELIST, API.LVSIL_NORMAL, m_ipLargeSystemImageList);

OK,我们修改以往的例子,就可以在 Tree 节点上显示图标了:

None.gifdot.gifdot.gif
None.gifShellItem shellItem=new ShellItem(pidlSub, iSub, sItem);
None.gifint imgIndex = API.GetSmallIconIndex(shellItem.PIDLFull.Ptr);
None.gifTreeNode nodeSub = new TreeNode(name, imgIndex, imgIndex);
None.gifdot.gifdot.gif

(注:关于文中出现的一些结构体或常量,读者可以自行查阅 MSDN,精力有限实在不能一一说明。)
我们来看一下效果:
shell5.jpg
事实上,这个代码改了很多,也涉及到下一节的部分内容,因此代码将在下一节中抛出...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值