执行

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

(C#)Windows Shell 外壳编程系列6 - 执行_ico//存放 PIDL 的数组

(C#)Windows Shell 外壳编程系列6 - 执行_icoIntPtr[] pidls = new IntPtr[1];

(C#)Windows Shell 外壳编程系列6 - 执行_icopidls[0] = pidl;

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

(C#)Windows Shell 外壳编程系列6 - 执行_ico//得到 IContextMenu 接口

(C#)Windows Shell 外壳编程系列6 - 执行_icoIntPtr iContextMenuPtr = IntPtr.Zero;

(C#)Windows Shell 外壳编程系列6 - 执行_icoiContextMenuPtr = IParent.GetUIObjectOf(IntPtr.Zero, (uint)pidls.Length,

(C#)Windows Shell 外壳编程系列6 - 执行_icopidls, ref Guids.IID_IContextMenu, out iContextMenuPtr);

(C#)Windows Shell 外壳编程系列6 - 执行_icoIContextMenu iContextMenu = (IContextMenu)Marshal.GetObjectForIUnknown(iContextMenuPtr);

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

(C#)Windows Shell 外壳编程系列6 - 执行_ico//提供一个弹出式菜单的句柄

(C#)Windows Shell 外壳编程系列6 - 执行_icoIntPtr contextMenu = API.CreatePopupMenu();

(C#)Windows Shell 外壳编程系列6 - 执行_icoiContextMenu.QueryContextMenu(contextMenu, 0,

(C#)Windows Shell 外壳编程系列6 - 执行_icoAPI.CMD_FIRST, API.CMD_LAST, CMF.NORMAL | CMF.EXPLORE);

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

(C#)Windows Shell 外壳编程系列6 - 执行_ico//获取默认的命令项

(C#)Windows Shell 外壳编程系列6 - 执行_icoint defaultCommand = API.GetMenuDefaultItem(contextMenu, false, 0);

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

(C#)Windows Shell 外壳编程系列6 - 执行_icoCMINVOKECOMMANDINFOEX invoke = new CMINVOKECOMMANDINFOEX();

(C#)Windows Shell 外壳编程系列6 - 执行_icoinvoke.cbSize = Marshal.SizeOf(typeof(CMINVOKECOMMANDINFOEX));

(C#)Windows Shell 外壳编程系列6 - 执行_icoinvoke.lpVerb = (IntPtr)(defaultCommand - API.CMD_FIRST);

(C#)Windows Shell 外壳编程系列6 - 执行_icoinvoke.lpDirectory = string.Empty;

(C#)Windows Shell 外壳编程系列6 - 执行_icoinvoke.fMask = 0;

(C#)Windows Shell 外壳编程系列6 - 执行_icoinvoke.ptInvoke = new POINT(MousePosition.X, MousePosition.Y);

(C#)Windows Shell 外壳编程系列6 - 执行_icoinvoke.nShow = 1;

(C#)Windows Shell 外壳编程系列6 - 执行_icoiContextMenu.InvokeCommand(ref invoke);

GetMenuDefaultItem 的原型:

(C#)Windows Shell 外壳编程系列6 - 执行_ico[DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]

(C#)Windows Shell 外壳编程系列6 - 执行_ico 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 - 执行_ico_29

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