实例介绍
【实例截图】C#AE案例
【核心代码】
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using WinShell; using System.Windows.Forms; namespace Demo { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private IShellFolder iDeskTop; private System.Windows.Forms.Control control; private static IntPtr m_ipSmallSystemImageList; private static IntPtr m_ipLargeSystemImageList; //private System.Windows.Forms.TreeView Tree1; public MainWindow() { InitializeComponent(); control = new System.Windows.Forms.Control(); mytreeview.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); mytreeview.HideSelection = false; mytreeview.HotTracking = true; mytreeview.BeforeExpand = new System.Windows.Forms.TreeViewCancelEventHandler(Tree1_BeforeExpand); mytreeview.MouseDown = new System.Windows.Forms.MouseEventHandler(Tree1_MouseDown); mytreeview.MouseUp = new System.Windows.Forms.MouseEventHandler(Tree1_MouseUp); SHFILEINFO shfi = new SHFILEINFO(); m_ipSmallSystemImageList = API.SHGetFileInfo("", 0, out shfi, Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI.SYSICONINDEX | SHGFI.SMALLICON | SHGFI.USEFILEATTRIBUTES); m_ipLargeSystemImageList = API.SHGetFileInfo("", 0, out shfi, Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI.SYSICONINDEX | SHGFI.LARGEICON | SHGFI.USEFILEATTRIBUTES); //把系统 ImageList 关联到 TreeView 和 ListView API.SendMessage(mytreeview.Handle, API.TVM_SETIMAGELIST, API.TVSIL_NORMAL, m_ipSmallSystemImageList); API.SendMessage(mytreeview.Handle, API.LVM_SETIMAGELIST, API.LVSIL_NORMAL, m_ipLargeSystemImageList); //获得桌面 PIDL IntPtr deskTopPtr; iDeskTop = API.GetDesktopFolder(out deskTopPtr); API.SHGetSpecialFolderLocation(IntPtr.Zero, CSIDL.DESKTOP, out deskTopPtr); //添加 桌面 节点 int imgIndex = API.GetSmallIconIndex(deskTopPtr); TreeNode tnDesktop = new TreeNode("桌面", imgIndex, imgIndex); tnDesktop.Tag = new ShellItem(deskTopPtr, iDeskTop); tnDesktop.Nodes.Add("..."); //把节点添加到树中 mytreeview.Nodes.Add(tnDesktop); mytreeview.SelectedNode = tnDesktop; tnDesktop.Expand(); } private void Tree1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Right) { mytreeview.SelectedNode = mytreeview.GetNodeAt(e.X, e.Y); } } private void Tree1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Right) { if (mytreeview.SelectedNode != null) { //获得当前节点的 PIDL ShellItem sItem = (ShellItem)mytreeview.SelectedNode.Tag; IntPtr PIDL = sItem.PIDL; //获得父节点的 IShellFolder 接口 IShellFolder IParent = iDeskTop; if (mytreeview.SelectedNode.Parent != null) { IParent = ((ShellItem)mytreeview.SelectedNode.Parent.Tag).ShellFolder; } else { //桌面的真实路径的 PIDL string path = API.GetSpecialFolderPath(control.Handle, ShellSpecialFolders.DESKTOPDIRECTORY); API.GetShellFolder(iDeskTop, path, out PIDL); } //存放 PIDL 的数组 IntPtr[] pidls = new IntPtr[1]; pidls[0] = PIDL; //得到 IContextMenu 接口 IntPtr iContextMenuPtr = IntPtr.Zero; iContextMenuPtr = IParent.GetUIObjectOf(IntPtr.Zero, (uint)pidls.Length, pidls, ref Guids.IID_IContextMenu, out iContextMenuPtr); IContextMenu iContextMenu = (IContextMenu)Marshal.GetObjectForIUnknown(iContextMenuPtr); //提供一个弹出式菜单的句柄 IntPtr contextMenu = API.CreatePopupMenu(); iContextMenu.QueryContextMenu(contextMenu, 0, API.CMD_FIRST, API.CMD_LAST, CMF.NORMAL | CMF.EXPLORE); //增加一个自定义菜单 string topInvoke = mytreeview.SelectedNode.IsExpanded ? "折叠(&A)" : "展开(&A)"; MFT extraFlag = (mytreeview.SelectedNode.Nodes.Count > 0) ? 0 : MFT.GRAYED; API.InsertMenu(contextMenu, 0, MFT.BYPOSITION | extraFlag, (int)(API.CMD_LAST 1), topInvoke); //增加分隔线 //API.InsertMenu(contextMenu, 1, MFT.BYPOSITION | MFT.SEPARATOR, 0, "-"); //把第一项菜单设置为默认菜单,也就是加粗 API.SetMenuDefaultItem(contextMenu, 0, true); ///////////////////////// //弹出菜单 uint cmd = API.TrackPopupMenuEx(contextMenu, TPM.RETURNCMD, System.Windows.Forms.Control.MousePosition.X, System.Windows.Forms.Control.MousePosition.Y, control.Handle, IntPtr.Zero); //获取命令序号,执行菜单命令 if (cmd >= API.CMD_FIRST) { CMINVOKECOMMANDINFOEX invoke = new CMINVOKECOMMANDINFOEX(); invoke.cbSize = Marshal.SizeOf(typeof(CMINVOKECOMMANDINFOEX)); invoke.lpVerb = (IntPtr)(cmd - 1); invoke.lpDirectory = string.Empty; invoke.fMask = 0; invoke.ptInvoke = new POINT(System.Windows.Forms.Control.MousePosition.X, System.Windows.Forms.Control.MousePosition.Y); invoke.nShow = 1; iContextMenu.InvokeCommand(ref invoke); //自定义菜单命令 if (cmd == API.CMD_LAST 1) { if (mytreeview.SelectedNode.IsExpanded) mytreeview.SelectedNode.Collapse(); else mytreeview.SelectedNode.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 ShellItem sItem = (ShellItem)e.Node.Tag; IShellFolder root = sItem.ShellFolder; if (root == null) { return; } //循环查找子项 IEnumIDList Enum = null; IntPtr EnumPtr = IntPtr.Zero; IntPtr pidlSub; int celtFetched; if (root.EnumObjects(control.Handle, SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN , 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); ShellItem shellItem = new ShellItem(pidlSub, iSub, sItem); int imgIndex = API.GetSmallIconIndex(shellItem.PIDLFull.Ptr); TreeNode nodeSub = new TreeNode(name, imgIndex, imgIndex); nodeSub.Tag = shellItem; nodeSub.Nodes.Add("..."); e.Node.Nodes.Add(nodeSub); } } if (root.EnumObjects(control.Handle, SHCONTF.NONFOLDERS | SHCONTF.INCLUDEHIDDEN, out EnumPtr) == API.S_OK) { Enum = (IEnumIDList)Marshal.GetObjectForIUnknown(EnumPtr); while (Enum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == API.S_FALSE) { SFGAO attribs = SFGAO.FOLDER; root.GetAttributesOf(1, new IntPtr[] { pidlSub }, ref attribs); if ((attribs & SFGAO.FOLDER) == 0) { string name = API.GetNameByIShell(root, pidlSub); string path = API.GetPathByIShell(root, pidlSub); ShellItem shellItem = new ShellItem(pidlSub, null, sItem); int imgIndex = API.GetSmallIconIndex(shellItem.PIDLFull.Ptr); TreeNode nodeSub = new TreeNode(name, imgIndex, imgIndex); nodeSub.Tag = shellItem; e.Node.Nodes.Add(nodeSub); } } } } } }
标签: demo
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论