访问 http://www.qqread.com/csharp/j394035.html
因此,我们使用 EnumObjects 函数返回的将是 IEnumIDList 的指针:
int EnumObjects(IntPtr hWnd, SHCONTF flags, out IntPtr enumIDList);
其中 flags 是 SHCONTF 枚举类型,它决定了枚举的内容:
SHCONTF
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
public enum SHCONTF
{
FOLDERS = 0x20,
NONFOLDERS = 0x40,
INCLUDEHIDDEN = 0x80,
INIT_ON_FIRST_NEXT = 0x100,
NETPRINTERSRCH = 0x200,
SHAREABLE = 0x400,
STORAGE = 0x800
}
因此,我们可以通过 flags 的不同来分别列举子文件和子目录。这里会遇到一个问题,怎么获取 PIDL 对象的名称呢。这里编写了2个函数,可以通过 PIDL 或者 IShellFolder 返回对象的名称(详细解释留到下一节):
获取名称
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
/**//// <summary>
/// 获取显示名称
/// </summary>
public static string GetNameByIShell(IShellFolder Root, IntPtr pidlSub)
{
IntPtr strr = Marshal.AllocCoTaskMem(MAX_PATH * 2 + 4);
Marshal.WriteInt32(strr, 0, 0);
StringBuilder buf = new StringBuilder(MAX_PATH);
Root.GetDisplayNameOf(pidlSub, SHGNO.INFOLDER, strr);
API.StrRetToBuf(strr, pidlSub, buf, MAX_PATH);
return buf.ToString();
}
/**//// <summary>
/// 根据 PIDL 获取显示名称
/// </summary>
public static string GetNameByPIDL(IntPtr pidl)
{
SHFILEINFO info = new SHFILEINFO();
API.SHGetFileInfo(pidl, 0, ref info, Marshal.SizeOf(typeof(SHFILEINFO)),
SHGFI.PIDL | SHGFI.DISPLAYNAME | SHGFI.TYPENAME);
return info.szDisplayName;
}
例子二,从“桌面”开始展开
这个例子将使你深入理解之前的内容。它是这样的一个例子,允许你从“桌面”开始,一直展开到最深层的对象。
例2
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
public partial class Sample2 : Form
{
private IShellFolder deskTop;
public Sample2()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//获得桌面 PIDL
IntPtr deskTopPtr;
deskTop = API.GetDesktopFolder(out deskTopPtr);
//添加 桌面 节点
TreeNode tnDesktop = new TreeNode("桌面");
tnDesktop.Tag = deskTop;
tnDesktop.Nodes.Add("
");
//把节点添加到树中
Tree1.Nodes.Add(tnDesktop);
tnDesktop.Expand();
}
private void Tree1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
判断节点是否已经展开#region 判断节点是否已经展开
if (e.Node.Nodes.Count != 1)
{
return;
}
else
{
if (e.Node.FirstNode.Text != "
")
{
return;
}
}
e.Node.Nodes.Clear();
#endregion
IShellFolder root = (IShellFolder)e.Node.Tag;
//循环查找子项
IEnumIDList Enum = null;
IntPtr EnumPtr = IntPtr.Zero;
IntPtr pidlSub;
int celtFetched;
if (root.EnumObjects(this.Handle, SHCONTF.FOLDERS, out EnumPtr) == API.S_OK)
{
Enum = (IEnumIDList)Marshal.GetObjectForIUnknown(EnumPtr);
while (Enum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == API.S_FALSE)
{
string name = API.GetNameByIShell(root, pidlSub);
IShellFolder iSub;
root.BindToObject(pidlSub, IntPtr.Zero, ref Guids.IID_IShellFolder, out iSub);
TreeNode nodeSub = new TreeNode(name);
nodeSub.Tag = iSub;
nodeSub.Nodes.Add("
");
e.Node.Nodes.Add(nodeSub);
}
}
}
private void Sample2_FormClosing(object sender, FormClosingEventArgs e)
{
//释放资源
Marshal.ReleaseComObject(deskTop);
}
}
照例,附图片和源代码:
源代码:WinShell2.rar
下一节将讲述 Shell 编程中的 IContextMenu ,也就是上下文菜单,将使你的应用程序列举 Shell 对象的同时,还能在右键操控它们的菜单。
更多内容请看Windows操作系统安装、Windows权限设置、Windows操作系统安全集专题,或进入讨论组讨论。
因此,我们使用 EnumObjects 函数返回的将是 IEnumIDList 的指针:
其中 flags 是 SHCONTF 枚举类型,它决定了枚举的内容:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
因此,我们可以通过 flags 的不同来分别列举子文件和子目录。这里会遇到一个问题,怎么获取 PIDL 对象的名称呢。这里编写了2个函数,可以通过 PIDL 或者 IShellFolder 返回对象的名称(详细解释留到下一节):
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
例子二,从“桌面”开始展开
这个例子将使你深入理解之前的内容。它是这样的一个例子,允许你从“桌面”开始,一直展开到最深层的对象。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
照例,附图片和源代码:
源代码:WinShell2.rar
下一节将讲述 Shell 编程中的 IContextMenu ,也就是上下文菜单,将使你的应用程序列举 Shell 对象的同时,还能在右键操控它们的菜单。
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- Windows操作系统安装 (15618篇文章)
- Windows权限设置 (10238篇文章)
- Windows操作系统安全集 (18679篇文章)
- Wlan组网----家庭专题 (4206篇文章)
- Windows频道 (9812篇文章)
- C#变得越来越臃肿是不可避免的? (7次浏览)
- C#与EXCEL的数据交互(一) (5次浏览)
- C# 3.0新特性之扩展方法 (1次浏览)
- C#调用QTP自动化对象模型的实例 (0次浏览)
- 利用 C# 实现任务栏通知窗口 (0次浏览)
- 深入C#学习系列之不可小瞧的using关键字 (0次浏览)
- 如何用.NET技术在线生成网站LOGO (0次浏览)
- C#实现遗传算法 模拟花朵的进化 (0次浏览)
- c#里的运算符重载 (0次浏览)
- QTP调用自己开发的.net类库 (0次浏览)




