且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-01-10 03:46:48

原文 C#)Windows Shell 编程系列5 - 获取图标

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

接上一节:(C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令

有关 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 - 获取图标using System;
C#)Windows Shell 编程系列5 - 获取图标
using System.Collections.Generic;
C#)Windows Shell 编程系列5 - 获取图标
using System.Text;
C#)Windows Shell 编程系列5 - 获取图标
using System.Collections;
C#)Windows Shell 编程系列5 - 获取图标
using System.Runtime.InteropServices;
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标
namespace WinShell
C#)Windows Shell 编程系列5 - 获取图标
{
C#)Windows Shell 编程系列5 - 获取图标    
public class PIDL
C#)Windows Shell 编程系列5 - 获取图标    
{
C#)Windows Shell 编程系列5 - 获取图标        
private IntPtr pidl = IntPtr.Zero;
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标        
public PIDL(IntPtr pidl, bool clone)
C#)Windows Shell 编程系列5 - 获取图标        
{
C#)Windows Shell 编程系列5 - 获取图标            
if (clone)
C#)Windows Shell 编程系列5 - 获取图标                
this.pidl = ILClone(pidl);
C#)Windows Shell 编程系列5 - 获取图标            
else
C#)Windows Shell 编程系列5 - 获取图标                
this.pidl = pidl;
C#)Windows Shell 编程系列5 - 获取图标        }

C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标        
public PIDL(PIDL pidl, bool clone)
C#)Windows Shell 编程系列5 - 获取图标        
{
C#)Windows Shell 编程系列5 - 获取图标            
if (clone)
C#)Windows Shell 编程系列5 - 获取图标                
this.pidl = ILClone(pidl.Ptr);
C#)Windows Shell 编程系列5 - 获取图标            
else
C#)Windows Shell 编程系列5 - 获取图标                
this.pidl = pidl.Ptr;
C#)Windows Shell 编程系列5 - 获取图标        }

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

C#)Windows Shell 编程系列5 - 获取图标            
get return pidl; } 
C#)Windows Shell 编程系列5 - 获取图标        }

C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标        
public void Insert(IntPtr insertPidl)
C#)Windows Shell 编程系列5 - 获取图标        
{
C#)Windows Shell 编程系列5 - 获取图标            IntPtr newPidl 
= ILCombine(insertPidl, pidl);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标            Marshal.FreeCoTaskMem(pidl);
C#)Windows Shell 编程系列5 - 获取图标            pidl 
= newPidl;
C#)Windows Shell 编程系列5 - 获取图标        }

C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标        
public void Free()
C#)Windows Shell 编程系列5 - 获取图标        
{
C#)Windows Shell 编程系列5 - 获取图标            
if (pidl != IntPtr.Zero)
C#)Windows Shell 编程系列5 - 获取图标            
{
C#)Windows Shell 编程系列5 - 获取图标                Marshal.FreeCoTaskMem(pidl);
C#)Windows Shell 编程系列5 - 获取图标                pidl 
= IntPtr.Zero;
C#)Windows Shell 编程系列5 - 获取图标            }

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

C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标        
private static int ItemIDSize(IntPtr pidl)
C#)Windows Shell 编程系列5 - 获取图标        
{
C#)Windows Shell 编程系列5 - 获取图标            
if (!pidl.Equals(IntPtr.Zero))
C#)Windows Shell 编程系列5 - 获取图标            
{
C#)Windows Shell 编程系列5 - 获取图标                
byte[] buffer = new byte[2];
C#)Windows Shell 编程系列5 - 获取图标                Marshal.Copy(pidl, buffer, 
02);
C#)Windows Shell 编程系列5 - 获取图标                
return buffer[1* 256 + buffer[0];
C#)Windows Shell 编程系列5 - 获取图标            }

C#)Windows Shell 编程系列5 - 获取图标            
else
C#)Windows Shell 编程系列5 - 获取图标                
return 0;
C#)Windows Shell 编程系列5 - 获取图标        }

C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标        
private static int ItemIDListSize(IntPtr pidl)
C#)Windows Shell 编程系列5 - 获取图标        
{
C#)Windows Shell 编程系列5 - 获取图标            
if (pidl.Equals(IntPtr.Zero))
C#)Windows Shell 编程系列5 - 获取图标                
return 0;
C#)Windows Shell 编程系列5 - 获取图标            
else
C#)Windows Shell 编程系列5 - 获取图标            
{
C#)Windows Shell 编程系列5 - 获取图标                
int size = ItemIDSize(pidl);
C#)Windows Shell 编程系列5 - 获取图标                
int nextSize = Marshal.ReadByte(pidl, size) + (Marshal.ReadByte(pidl, size + 1* 256);
C#)Windows Shell 编程系列5 - 获取图标                
while (nextSize > 0)
C#)Windows Shell 编程系列5 - 获取图标                
{
C#)Windows Shell 编程系列5 - 获取图标                    size 
+= nextSize;
C#)Windows Shell 编程系列5 - 获取图标                    nextSize 
= Marshal.ReadByte(pidl, size) + (Marshal.ReadByte(pidl, size + 1* 256);
C#)Windows Shell 编程系列5 - 获取图标                }

C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标                
return size;
C#)Windows Shell 编程系列5 - 获取图标            }

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

C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标        
public static IntPtr ILClone(IntPtr pidl)
C#)Windows Shell 编程系列5 - 获取图标        
{
C#)Windows Shell 编程系列5 - 获取图标            
int size = ItemIDListSize(pidl);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标            
byte[] bytes = new byte[size + 2];
C#)Windows Shell 编程系列5 - 获取图标            Marshal.Copy(pidl, bytes, 
0, size);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标            IntPtr newPidl 
= Marshal.AllocCoTaskMem(size + 2);
C#)Windows Shell 编程系列5 - 获取图标            Marshal.Copy(bytes, 
0, newPidl, size + 2);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标            
return newPidl;
C#)Windows Shell 编程系列5 - 获取图标        }

C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标        
public static IntPtr ILCombine(IntPtr pidl1, IntPtr pidl2)
C#)Windows Shell 编程系列5 - 获取图标        
{
C#)Windows Shell 编程系列5 - 获取图标            
int size1 = ItemIDListSize(pidl1);
C#)Windows Shell 编程系列5 - 获取图标            
int size2 = ItemIDListSize(pidl2);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标            IntPtr newPidl 
= Marshal.AllocCoTaskMem(size1 + size2 + 2);
C#)Windows Shell 编程系列5 - 获取图标            
byte[] bytes = new byte[size1 + size2 + 2];
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标            Marshal.Copy(pidl1, bytes, 
0, size1);
C#)Windows Shell 编程系列5 - 获取图标            Marshal.Copy(pidl2, bytes, size1, size2);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标            Marshal.Copy(bytes, 
0, newPidl, bytes.Length);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标            
return newPidl;
C#)Windows Shell 编程系列5 - 获取图标        }

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

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

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


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

C#)Windows Shell 编程系列5 - 获取图标private ShellItem m_ParentItem;
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标
public ShellItem ParentItem
C#)Windows Shell 编程系列5 - 获取图标
{
C#)Windows Shell 编程系列5 - 获取图标    
get return m_ParentItem; }
C#)Windows Shell 编程系列5 - 获取图标    
set { m_ParentItem = value; }
C#)Windows Shell 编程系列5 - 获取图标}

C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标
/// <summary>
C#)Windows Shell 编程系列5 - 获取图标
/// 绝对 PIDL
C#)Windows Shell 编程系列5 - 获取图标
/// </summary>

C#)Windows Shell 编程系列5 - 获取图标public PIDL PIDLFull
C#)Windows Shell 编程系列5 - 获取图标
{
C#)Windows Shell 编程系列5 - 获取图标    
get
C#)Windows Shell 编程系列5 - 获取图标    
{
C#)Windows Shell 编程系列5 - 获取图标        PIDL pidlFull 
= new PIDL(PIDL, true);
C#)Windows Shell 编程系列5 - 获取图标        ShellItem current 
= ParentItem;
C#)Windows Shell 编程系列5 - 获取图标        
while (current != null)
C#)Windows Shell 编程系列5 - 获取图标        
{
C#)Windows Shell 编程系列5 - 获取图标            pidlFull.Insert(current.PIDL);
C#)Windows Shell 编程系列5 - 获取图标            current 
= current.ParentItem;
C#)Windows Shell 编程系列5 - 获取图标        }

C#)Windows Shell 编程系列5 - 获取图标        
return pidlFull;
C#)Windows Shell 编程系列5 - 获取图标    }

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


获取图标

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

C#)Windows Shell 编程系列5 - 获取图标[DllImport("shell32", EntryPoint = "SHGetFileInfo", ExactSpelling = false
C#)Windows Shell 编程系列5 - 获取图标    CharSet 
= CharSet.Auto, SetLastError = true)]
C#)Windows Shell 编程系列5 - 获取图标
public static extern IntPtr SHGetFileInfo(
C#)Windows Shell 编程系列5 - 获取图标    IntPtr ppidl, 
C#)Windows Shell 编程系列5 - 获取图标    FILE_ATTRIBUTE dwFileAttributes, 
C#)Windows Shell 编程系列5 - 获取图标    
ref SHFILEINFO sfi, 
C#)Windows Shell 编程系列5 - 获取图标    
int cbFileInfo, 
C#)Windows Shell 编程系列5 - 获取图标    SHGFI uFlags);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标[DllImport(
"Shell32.dll", CharSet = CharSet.Auto)]
C#)Windows Shell 编程系列5 - 获取图标
public static extern IntPtr SHGetFileInfo(
C#)Windows Shell 编程系列5 - 获取图标    
string Path, 
C#)Windows Shell 编程系列5 - 获取图标    FILE_ATTRIBUTE fileAttributes, 
C#)Windows Shell 编程系列5 - 获取图标    
out SHFILEINFO sfi, 
C#)Windows Shell 编程系列5 - 获取图标    
int cbFileInfo, SHGFI flags);
C#)Windows Shell 编程系列5 - 获取图标


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

C#)Windows Shell 编程系列5 - 获取图标/// <summary>
C#)Windows Shell 编程系列5 - 获取图标
/// 获取小图标索引
C#)Windows Shell 编程系列5 - 获取图标
/// </summary>

C#)Windows Shell 编程系列5 - 获取图标public static int GetSmallIconIndex(string strFilename)
C#)Windows Shell 编程系列5 - 获取图标
{
C#)Windows Shell 编程系列5 - 获取图标    SHFILEINFO psfi 
= new SHFILEINFO();
C#)Windows Shell 编程系列5 - 获取图标    IntPtr ipIcon 
= SHGetFileInfo(strFilename, 0out psfi, Marshal.SizeOf(psfi),
C#)Windows Shell 编程系列5 - 获取图标        SHGFI.ICON 
| SHGFI.SMALLICON | SHGFI.SYSICONINDEX);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标    
return psfi.iIcon;
C#)Windows Shell 编程系列5 - 获取图标}

C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标
public static int GetSmallIconIndex(IntPtr ipIDList)
C#)Windows Shell 编程系列5 - 获取图标
{
C#)Windows Shell 编程系列5 - 获取图标    SHFILEINFO psfi 
= new SHFILEINFO();
C#)Windows Shell 编程系列5 - 获取图标    IntPtr ipIcon 
= SHGetFileInfo(ipIDList, 0ref psfi, Marshal.SizeOf(psfi),
C#)Windows Shell 编程系列5 - 获取图标        SHGFI.ICON 
| SHGFI.PIDL | SHGFI.SMALLICON | SHGFI.SYSICONINDEX);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标    
return psfi.iIcon;
C#)Windows Shell 编程系列5 - 获取图标}


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

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

C#)Windows Shell 编程系列5 - 获取图标IntPtr m_ipSmallSystemImageList;
C#)Windows Shell 编程系列5 - 获取图标IntPtr m_ipLargeSystemImageList;
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标
//获取系统 ImageList
C#)Windows Shell 编程系列5 - 获取图标
SHFILEINFO shfi = new SHFILEINFO();
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标m_ipSmallSystemImageList 
= API.SHGetFileInfo(""0out shfi, Marshal.SizeOf(typeof(SHFILEINFO)),
C#)Windows Shell 编程系列5 - 获取图标    SHGFI.SYSICONINDEX 
| SHGFI.SMALLICON | SHGFI.USEFILEATTRIBUTES);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标m_ipLargeSystemImageList 
= API.SHGetFileInfo(""0out shfi, Marshal.SizeOf(typeof(SHFILEINFO)),
C#)Windows Shell 编程系列5 - 获取图标    SHGFI.SYSICONINDEX 
| SHGFI.LARGEICON | SHGFI.USEFILEATTRIBUTES);
C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标
//把系统 ImageList 关联到 TreeView 和 ListView
C#)Windows Shell 编程系列5 - 获取图标
API.SendMessage(Tree1.Handle, API.TVM_SETIMAGELIST, API.TVSIL_NORMAL, m_ipSmallSystemImageList);
C#)Windows Shell 编程系列5 - 获取图标API.SendMessage(lvFile.Handle, API.LVM_SETIMAGELIST, API.LVSIL_NORMAL, m_ipLargeSystemImageList);


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

C#)Windows Shell 编程系列5 - 获取图标C#)Windows Shell 编程系列5 - 获取图标C#)Windows Shell 编程系列5 - 获取图标
C#)Windows Shell 编程系列5 - 获取图标ShellItem shellItem
=new ShellItem(pidlSub, iSub, sItem);
C#)Windows Shell 编程系列5 - 获取图标
int imgIndex = API.GetSmallIconIndex(shellItem.PIDLFull.Ptr);
C#)Windows Shell 编程系列5 - 获取图标TreeNode nodeSub 
= new TreeNode(name, imgIndex, imgIndex);
C#)Windows Shell 编程系列5 - 获取图标C#)Windows Shell 编程系列5 - 获取图标C#)Windows Shell 编程系列5 - 获取图标


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

我们来看一下效果:

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

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