有关 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 类

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_02PIDL.cs

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03using System;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03using System.Collections.Generic;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03using System.Text;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03using System.Collections;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03using System.Runtime.InteropServices;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03namespace WinShell

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_02(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 private IntPtr pidl = IntPtr.Zero;

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 public PIDL(IntPtr pidl, bool clone)

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 this.pidl = ILClone(pidl);

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 this.pidl = pidl;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27        }

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 public PIDL(PIDL pidl, bool clone)

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 this.pidl = ILClone(pidl.Ptr);

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 this.pidl = pidl.Ptr;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27        }

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

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

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15 get (C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{ return pidl; }

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27        }

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 public void Insert(IntPtr insertPidl)

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            IntPtr newPidl = ILCombine(insertPidl, pidl);

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            Marshal.FreeCoTaskMem(pidl);

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            pidl = newPidl;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27        }

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

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 if (pidl != IntPtr.Zero)

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13                Marshal.FreeCoTaskMem(pidl);

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13                pidl = IntPtr.Zero;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27            }

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27        }

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

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 private static int ItemIDSize(IntPtr pidl)

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 if (!pidl.Equals(IntPtr.Zero))

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 byte[] buffer = new byte[2];

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13                Marshal.Copy(pidl, buffer, 0, 2);

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 return buffer[1] * 256 + buffer[0];

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27            }

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

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27        }

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 private static int ItemIDListSize(IntPtr pidl)

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 if (pidl.Equals(IntPtr.Zero))

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

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 int size = ItemIDSize(pidl);

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 int nextSize = Marshal.ReadByte(pidl, size) + (Marshal.ReadByte(pidl, size + 1) * 256);

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 while (nextSize > 0)

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13                    size += nextSize;

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13                    nextSize = Marshal.ReadByte(pidl, size) + (Marshal.ReadByte(pidl, size + 1) * 256);

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27                }

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

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27            }

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27        }

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 public static IntPtr ILClone(IntPtr pidl)

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 int size = ItemIDListSize(pidl);

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 byte[] bytes = new byte[size + 2];

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            Marshal.Copy(pidl, bytes, 0, size);

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            IntPtr newPidl = Marshal.AllocCoTaskMem(size + 2);

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            Marshal.Copy(bytes, 0, newPidl, size + 2);

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

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27        }

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 public static IntPtr ILCombine(IntPtr pidl1, IntPtr pidl2)

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 int size1 = ItemIDListSize(pidl1);

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 int size2 = ItemIDListSize(pidl2);

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            IntPtr newPidl = Marshal.AllocCoTaskMem(size1 + size2 + 2);

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 byte[] bytes = new byte[size1 + size2 + 2];

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            Marshal.Copy(pidl1, bytes, 0, size1);

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            Marshal.Copy(pidl2, bytes, size1, size2);

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            Marshal.Copy(bytes, 0, newPidl, bytes.Length);

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

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27        }

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27    }

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03private ShellItem m_ParentItem;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03public ShellItem ParentItem

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_02(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15 get (C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{ return m_ParentItem; }

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15 set (C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{ m_ParentItem = value; }

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_02(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径/**//// <summary>

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13/// 绝对 PIDL

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_146/// </summary>

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03public PIDL PIDLFull

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_02(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13        PIDL pidlFull = new PIDL(PIDL, true);

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13        ShellItem current = ParentItem;

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 while (current != null)

(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径_14(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_15(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            pidlFull.Insert(current.PIDL);

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13            current = current.ParentItem;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27        }

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_27    }

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

获取图标

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03[DllImport("shell32", EntryPoint = "SHGetFileInfo", ExactSpelling = false, 

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03    CharSet = CharSet.Auto, SetLastError = true)]

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03public static extern IntPtr SHGetFileInfo(

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03    IntPtr ppidl, 

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03    FILE_ATTRIBUTE dwFileAttributes, 

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03 ref SHFILEINFO sfi, 

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03 int cbFileInfo, 

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03    SHGFI uFlags);

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03[DllImport("Shell32.dll", CharSet = CharSet.Auto)]

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03public static extern IntPtr SHGetFileInfo(

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03 string Path, 

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03    FILE_ATTRIBUTE fileAttributes, 

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03 out SHFILEINFO sfi, 

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03 int cbFileInfo, SHGFI flags);

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_02(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径/**//// <summary>

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13/// 获取小图标索引

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_146/// </summary>

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03public static int GetSmallIconIndex(string strFilename)

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_02(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13    SHFILEINFO psfi = new SHFILEINFO();

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13    IntPtr ipIcon = SHGetFileInfo(strFilename, 0, out psfi, Marshal.SizeOf(psfi),

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13        SHGFI.ICON | SHGFI.SMALLICON | SHGFI.SYSICONINDEX);

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 return psfi.iIcon;

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03public static int GetSmallIconIndex(IntPtr ipIDList)

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_02(C#)Windows Shell 外壳编程系列5 - 获取图标_相对路径(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12{

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13    SHFILEINFO psfi = new SHFILEINFO();

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13    IntPtr ipIcon = SHGetFileInfo(ipIDList, 0, ref psfi, Marshal.SizeOf(psfi),

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13        SHGFI.ICON | SHGFI.PIDL | SHGFI.SMALLICON | SHGFI.SYSICONINDEX);

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_ico_13 return psfi.iIcon;

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

大家也许会觉得奇怪,GetSmallIconIndex 返回的是 int ,到底要怎么使用?

其实没错,GetSmallIconIndex 仅仅是返回该图标在系统图像列表(System ImageList)的索引(Index)而已。我们只要获取系统图像列表的指针,再把它关联到你的 TreeView 或 ListView ,即可通过 Icon Index 来显示图标了。

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03IntPtr m_ipSmallSystemImageList;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03IntPtr m_ipLargeSystemImageList;

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03//获取系统 ImageList

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03SHFILEINFO shfi = new SHFILEINFO();

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03m_ipSmallSystemImageList = API.SHGetFileInfo("", 0, out shfi, Marshal.SizeOf(typeof(SHFILEINFO)),

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03    SHGFI.SYSICONINDEX | SHGFI.SMALLICON | SHGFI.USEFILEATTRIBUTES);

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03m_ipLargeSystemImageList = API.SHGetFileInfo("", 0, out shfi, Marshal.SizeOf(typeof(SHFILEINFO)),

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03    SHGFI.SYSICONINDEX | SHGFI.LARGEICON | SHGFI.USEFILEATTRIBUTES);

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03//把系统 ImageList 关联到 TreeView 和 ListView

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03API.SendMessage(Tree1.Handle, API.TVM_SETIMAGELIST, API.TVSIL_NORMAL, m_ipSmallSystemImageList);

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03API.SendMessage(lvFile.Handle, API.LVM_SETIMAGELIST, API.LVSIL_NORMAL, m_ipLargeSystemImageList);

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

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03ShellItem shellItem=new ShellItem(pidlSub, iSub, sItem);

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03int imgIndex = API.GetSmallIconIndex(shellItem.PIDLFull.Ptr);

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03TreeNode nodeSub = new TreeNode(name, imgIndex, imgIndex);

(C#)Windows Shell 外壳编程系列5 - 获取图标_绝对路径_03(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12(C#)Windows Shell 外壳编程系列5 - 获取图标_小图标_12

(注:关于文中出现的一些结构体或常量,读者可以自行查阅 MSDN,精力有限实在不能一一说明。)

我们来看一下效果:

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

事实上,这个代码改了很多,也涉及到下一节的部分内容,因此代码将在下一节中抛出...