原文(C#)Windows Shell 外壳编程系列6 - 执行
(本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢~)
接上一节:(C#)Windows Shell 外壳编程系列5 - 获取图标
执行
许多人都知道 ShellExecute ,用于执行一个外部命令。但对于 IShellFoloder 对象来说,它的执行命令,都在它的 ContextMenu 里面了。记得前几节说过如何直接调用 ContextMenu 里的项,因此,执行一个 IShellFoloder,也无非是调用它的 ContextMenu 里默认的项而已:
//
存放 PIDL 的数组
IntPtr[] pidls
=
new
IntPtr[
1
];
pidls[
0
]
=
pidl;
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
//
得到 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);
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
//
提供一个弹出式菜单的句柄
IntPtr contextMenu
=
API.CreatePopupMenu();
iContextMenu.QueryContextMenu(contextMenu,
0
,
API.CMD_FIRST, API.CMD_LAST, CMF.NORMAL
|
CMF.EXPLORE);
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
//
获取默认的命令项
int
defaultCommand
=
API.GetMenuDefaultItem(contextMenu,
false
,
0
);
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
CMINVOKECOMMANDINFOEX invoke
=
new
CMINVOKECOMMANDINFOEX();
invoke.cbSize
=
Marshal.SizeOf(
typeof
(CMINVOKECOMMANDINFOEX));
invoke.lpVerb
=
(IntPtr)(defaultCommand
-
API.CMD_FIRST);
invoke.lpDirectory
=
string
.Empty;
invoke.fMask
=
0
;
invoke.ptInvoke
=
new
POINT(MousePosition.X, MousePosition.Y);
invoke.nShow
=
1
;
iContextMenu.InvokeCommand(
ref
invoke);
GetMenuDefaultItem 的原型:
[DllImport(
"
user32
"
, SetLastError
=
true
, CharSet
=
CharSet.Auto)]
public
static
extern
int
GetMenuDefaultItem(IntPtr hMenu,
bool
fByPos,
uint
gmdiFlags);
一个 IShellFolder 的默认菜单一般都是“打开”,但有些却不是。所以 lpVerb 不应该直接使用 "open"。
资源管理器
经过把前几节中的例子修改,大致得到一个资源管理器的原型,但它还有很多问题:
1,不会释放资源
2,无法显示快捷方式、共享等图标标志
3,ContextMenu 某些地方没有处理,例如发送到...
4,拖拉没有实现
5,没有实时监控更改
因此,要做一个完整的资源管理器,是非常麻烦的事情,你可以参考 C# FileBrowser ,它已经做得非常好了。
![](https://yqfile.alicdn.com/img_c053d994cd9b76cf6258aa3ba72af93f.jpg?x-oss-process=image/resize,w_1400/format,webp)
源代码:/Files/lemony/WinShell5.rar
以后会讲述一些在资源管理器实现 Shell 操作的内容,希望大家多多支持^_^。