(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 类

ContractedBlock.gif ExpandedBlockStart.gif PIDL.cs
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Collections;
None.gif
using System.Runtime.InteropServices;
None.gif
None.gif
namespace WinShell
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class PIDL
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private IntPtr pidl = IntPtr.Zero;
InBlock.gif
InBlock.gif        
public PIDL(IntPtr pidl, bool clone)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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.gif        
dot.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.gif        
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn pidl; } 
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Insert(IntPtr insertPidl)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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.gif        
dot.gif{
InBlock.gif            
if (pidl != IntPtr.Zero)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gif        
dot.gif{
InBlock.gif            
if (!pidl.Equals(IntPtr.Zero))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte[] buffer = new byte[2];
InBlock.gif                Marshal.Copy(pidl, buffer, 
02);
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.gif        
dot.gif{
InBlock.gif            
if (pidl.Equals(IntPtr.Zero))
InBlock.gif                
return 0;
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.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.gif                
dot.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.gif        
dot.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.gif        
dot.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.gif private  ShellItem m_ParentItem;
None.gif
None.gif
public  ShellItem ParentItem
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
get dot.gifreturn 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.gif public  PIDL PIDLFull
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
get
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        PIDL pidlFull 
= new PIDL(PIDL, true);
InBlock.gif        ShellItem current 
= ParentItem;
InBlock.gif        
while (current != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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.gif
public   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.gif
public   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.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 获取小图标索引
ExpandedBlockEnd.gif
/// </summary>

None.gif public   static   int  GetSmallIconIndex( string  strFilename)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    SHFILEINFO psfi 
= new SHFILEINFO();
InBlock.gif    IntPtr ipIcon 
= SHGetFileInfo(strFilename, 0out psfi, Marshal.SizeOf(psfi),
InBlock.gif        SHGFI.ICON 
| SHGFI.SMALLICON | SHGFI.SYSICONINDEX);
InBlock.gif
InBlock.gif    
return psfi.iIcon;
ExpandedBlockEnd.gif}

None.gif
None.gif
public   static   int  GetSmallIconIndex(IntPtr ipIDList)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    SHFILEINFO psfi 
= new SHFILEINFO();
InBlock.gif    IntPtr ipIcon 
= SHGetFileInfo(ipIDList, 0ref 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.gif IntPtr m_ipSmallSystemImageList;
None.gifIntPtr m_ipLargeSystemImageList;
None.gif
None.gif
// 获取系统 ImageList
None.gif
SHFILEINFO 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.gif
API.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.gif dot.gifdot.gif
None.gifShellItem shellItem
= new  ShellItem(pidlSub, iSub, sItem);
None.gif
int  imgIndex  =  API.GetSmallIconIndex(shellItem.PIDLFull.Ptr);
None.gifTreeNode nodeSub 
=   new  TreeNode(name, imgIndex, imgIndex);
None.gifdot.gifdot.gif

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

我们来看一下效果:



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

Windows Shell扩展编程完全指南 第一节 - 一步步教你如何编写Shell扩展 第二节 - 如何编写一次操作多个文件对象的Shell扩展 第三节-如何编写为文件对象弹出提示信息框的Shell扩展 第四节 - 如何编写提供定制拖放功能的Shell扩展 第五节-如何编写添加属性页到文件属性对话框中的Shell扩展 第六节-如何编写定制”发送到”菜单的Shell扩展 第七节-如何编写自画上下文菜单项的Shell扩展, 以及如何使上下文菜单扩展响应文件夹窗口背景上的鼠标右击事件 第八节-如何使用信息栏扩展添加定制的信息栏到资源浏览器详细资料列表中 Windows Shell扩展编程完全指南 目录与资料简介 第一节 - 一步步教你如何编写Shell扩展 简要概述了Shell扩展及如何对之进行调试. 所附的例子演示了如何为文本文件对象添加上下文菜单项。 -------------------------------------------------------------------------------- 第二节 - 如何编写一次操作多个文件对象的Shell扩展 示范了如何编写一次操作多个被选择文件的上下文菜单扩展。所附的例子为DLL文件的上下文菜单项添加”注册”和”注销”两项以方便DLL服务器的注册操作. -------------------------------------------------------------------------------- 第三节-如何编写为文件对象弹出提示信息框的Shell扩展 示范了如何使用QueryInfo 扩展为文本文件对象提供提示信息框,同时还解释了如何在Shell扩展中使用 MFC. -------------------------------------------------------------------------------- 第四节 - 如何编写提供定制拖放功能的Shell扩展 示范了如何添加菜单项到 用户用右键拖放文件对象时弹出的上下文菜单. 所附的例子为文件对象生成硬链接(hard link). (注: 该扩展只在Windows 2000下起作用, 但你可以在以前版本的Windows中编译并运行该扩展(具体使用请见文章内容) -------------------------------------------------------------------------------- 第六节-如何编写定制”发送到”菜单的Shell扩展 讨论了如何使用放置目标处理器扩展添加菜单项到”发送到”菜单. 所附的例子实现了将文件发送到任一文件夹的功能。 第五节-如何编写添加属性页到文件属性对话框中的Shell扩展 示范了如何添加新定制的属性页到文件属性对话框中. 所附的例子添加一个定制的属性页,使用它你可以编辑文件对象的创建,修改和最后访问时间. -------------------------------------------------------------------------------- 第七节-如何编写自画上下文菜单项的Shell扩展, 以及如何使上下文菜单扩展响应文件夹窗口背景上的鼠标右击事件 解决了读者提出的两个问题: 自画菜单项和文件夹窗口背景上的上下文菜单. 所附的例子包含两个扩展: 位图预览 (如上图) 在上下文菜单上显示BMP文件的缩略图;另一个扩展添加菜单项到文件夹窗口背景上下文菜单. -------------------------------------------------------------------------------- 第八节-如何使用信息栏扩展添加定制的信息栏到资源浏览器详细资料列表中 示范了如何添加定制信息栏到Windows 2000资源浏览器的详细信息列表. 所附的例子添加若干个信息栏以显示MP3文件的资料标签数据 (改扩展仅用于Windows 2000.) -------------------------------------------------------------------------------- 第九节-如何编写定制文件类型显示图标Shell扩展
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值