(C#)Windows Shell 外壳编程系列6 - 执行

(本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢~)

接上一节:(C#)Windows Shell 外壳编程系列5 - 获取图标 

执行

    许多人都知道 ShellExecute ,用于执行一个外部命令。但对于  IShellFoloder 对象来说,它的执行命令,都在它的 ContextMenu 里面了。记得前几节说过如何直接调用 ContextMenu 里的项,因此,执行一个 IShellFoloder,也无非是调用它的 ContextMenu  里默认的项而已:

(C#)Windows Shell 外壳编程系列6 - 执行//存放 PIDL 的数组
(C#)Windows Shell 外壳编程系列6 - 执行IntPtr[] pidls = new IntPtr[1];
(C#)Windows Shell 外壳编程系列6 - 执行pidls[0] = pidl;
(C#)Windows Shell 外壳编程系列6 - 执行
(C#)Windows Shell 外壳编程系列6 - 执行//得到 IContextMenu 接口
(C#)Windows Shell 外壳编程系列6 - 执行IntPtr iContextMenuPtr = IntPtr.Zero;
(C#)Windows Shell 外壳编程系列6 - 执行iContextMenuPtr = IParent.GetUIObjectOf(IntPtr.Zero, (uint)pidls.Length,
(C#)Windows Shell 外壳编程系列6 - 执行pidls, ref Guids.IID_IContextMenu, out iContextMenuPtr);
(C#)Windows Shell 外壳编程系列6 - 执行IContextMenu iContextMenu = (IContextMenu)Marshal.GetObjectForIUnknown(iContextMenuPtr);
(C#)Windows Shell 外壳编程系列6 - 执行
(C#)Windows Shell 外壳编程系列6 - 执行//提供一个弹出式菜单的句柄
(C#)Windows Shell 外壳编程系列6 - 执行IntPtr contextMenu = API.CreatePopupMenu();
(C#)Windows Shell 外壳编程系列6 - 执行iContextMenu.QueryContextMenu(contextMenu, 0,
(C#)Windows Shell 外壳编程系列6 - 执行API.CMD_FIRST, API.CMD_LAST, CMF.NORMAL | CMF.EXPLORE);
(C#)Windows Shell 外壳编程系列6 - 执行
(C#)Windows Shell 外壳编程系列6 - 执行//获取默认的命令项
(C#)Windows Shell 外壳编程系列6 - 执行int defaultCommand = API.GetMenuDefaultItem(contextMenu, false, 0);
(C#)Windows Shell 外壳编程系列6 - 执行
(C#)Windows Shell 外壳编程系列6 - 执行CMINVOKECOMMANDINFOEX invoke = new CMINVOKECOMMANDINFOEX();
(C#)Windows Shell 外壳编程系列6 - 执行invoke.cbSize = Marshal.SizeOf(typeof(CMINVOKECOMMANDINFOEX));
(C#)Windows Shell 外壳编程系列6 - 执行invoke.lpVerb = (IntPtr)(defaultCommand - API.CMD_FIRST);
(C#)Windows Shell 外壳编程系列6 - 执行invoke.lpDirectory = string.Empty;
(C#)Windows Shell 外壳编程系列6 - 执行invoke.fMask = 0;
(C#)Windows Shell 外壳编程系列6 - 执行invoke.ptInvoke = new POINT(MousePosition.X, MousePosition.Y);
(C#)Windows Shell 外壳编程系列6 - 执行invoke.nShow = 1;
(C#)Windows Shell 外壳编程系列6 - 执行iContextMenu.InvokeCommand(ref invoke);


GetMenuDefaultItem 的原型:

(C#)Windows Shell 外壳编程系列6 - 执行[DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
(C#)Windows Shell 外壳编程系列6 - 执行        public static extern int GetMenuDefaultItem(IntPtr hMenu, bool fByPos, uint gmdiFlags);


一个 IShellFolder 的默认菜单一般都是“打开”,但有些却不是。所以 lpVerb 不应该直接使用 "open"。

资源管理器

    经过把前几节中的例子修改,大致得到一个资源管理器的原型,但它还有很多问题:

1,不会释放资源
2,无法显示快捷方式、共享等图标标志
3,ContextMenu 某些地方没有处理,例如发送到...
4,拖拉没有实现
5,没有实时监控更改

    因此,要做一个完整的资源管理器,是非常麻烦的事情,你可以参考 C# FileBrowser ,它已经做得非常好了。

(C#)Windows Shell 外壳编程系列6 - 执行

源代码:/Files/lemony/WinShell5.rar

以后会讲述一些在资源管理器实现 Shell 操作的内容,希望大家多多支持^_^。