基本信息
源码名称:C# 资源管理器(支持右键菜单)
源码大小:0.10M
文件格式:.zip
开发语言:C#
更新时间:2020-10-29
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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);
}
}
}
}
}
}