public void ShowContextMenuForCSIDL(IntPtr handleOwner, ShellAPI.CSIDL csidl, Point pointScreen) { // Release all resources first. ReleaseAll(); IntPtr pMenu = IntPtr.Zero; var hook = new LocalWindowsHook(HookType.WH_CALLWNDPROC); hook.HookInvoked += WindowsHookInvoked; try { IntPtr tempPidl; ShellAPI.SHGetSpecialFolderLocation(IntPtr.Zero, csidl, out tempPidl); _arrPIDLs = new[] { tempPidl }; _oParentFolder = GetDesktopFolder(); if (false == GetContextMenuInterfaces(_oParentFolder, _arrPIDLs)) { ReleaseAll(); return; } pMenu = CreatePopupMenu(); _oContextMenu.QueryContextMenu( pMenu, 0, CMD_FIRST, CMD_LAST, CMF.EXPLORE | CMF.NORMAL | ((Control.ModifierKeys & Keys.Shift) != 0 ? CMF.EXTENDEDVERBS : 0)); hook.Install(); uint nSelected = TrackPopupMenuEx( pMenu, TPM.RETURNCMD, pointScreen.X, pointScreen.Y, handleOwner, IntPtr.Zero); DestroyMenu(pMenu); pMenu = IntPtr.Zero; if (nSelected != 0) { var strParentFolder = string.Empty; InvokeCommand(_oContextMenu, nSelected, strParentFolder, pointScreen); } } finally { hook.Uninstall(); if (pMenu != IntPtr.Zero) { DestroyMenu(pMenu); } ReleaseAll(); } }
//private void InvokeContextMenuDefault(FileInfo[] arrayOfFileInfo) //{ // // Release all resources first. // ReleaseAll(); // IntPtr pMenu = IntPtr.Zero; // try // { // _arrPIDLs = GetPIDL(arrayOfFileInfo); // if (null == _arrPIDLs) // { // ReleaseAll(); // return; // } // if (false == GetContextMenuInterfaces(_oParentFolder, _arrPIDLs)) // { // ReleaseAll(); // return; // } // pMenu = CreatePopupMenu(); // int nResult = _oContextMenu.QueryContextMenu( // pMenu, // 0, // CMD_FIRST, // CMD_LAST, // CMF.DEFAULTONLY | // ((Control.ModifierKeys & Keys.Shift) != 0 ? CMF.EXTENDEDVERBS : 0)); // uint nDefaultCmd = (uint)GetMenuDefaultItem(pMenu, false, 0); // if (nDefaultCmd >= CMD_FIRST) // { // InvokeCommand(_oContextMenu, nDefaultCmd, arrayOfFileInfo[0].DirectoryName, Control.MousePosition); // } // DestroyMenu(pMenu); // pMenu = IntPtr.Zero; // } // catch // { // throw; // } // finally // { // if (pMenu != IntPtr.Zero) // { // DestroyMenu(pMenu); // } // ReleaseAll(); // } //} #endregion #region ShowContextMenu() /// <summary> /// Shows the context menu /// </summary> /// <param name="handleOwner">Window that will get messages</param> /// <param name="arrFileInfo">FileInfos (should all be in same directory)</param> /// <param name="pointScreen">Where to show the menu</param> public void ShowContextMenu(IntPtr handleOwner, FileInfo[] arrFileInfo, Point pointScreen) { // Release all resources first. ReleaseAll(); IntPtr pMenu = IntPtr.Zero; var hook = new LocalWindowsHook(HookType.WH_CALLWNDPROC); hook.HookInvoked += WindowsHookInvoked; try { //Application.AddMessageFilter(this); _arrPIDLs = GetPIDL(arrFileInfo); if (null == _arrPIDLs) { ReleaseAll(); return; } if (false == GetContextMenuInterfaces(_oParentFolder, _arrPIDLs)) { ReleaseAll(); return; } pMenu = CreatePopupMenu(); _oContextMenu.QueryContextMenu( pMenu, 0, CMD_FIRST, CMD_LAST, CMF.EXPLORE | CMF.NORMAL | ((Control.ModifierKeys & Keys.Shift) != 0 ? CMF.EXTENDEDVERBS : 0)); hook.Install(); uint nSelected = TrackPopupMenuEx( pMenu, TPM.RETURNCMD, pointScreen.X, pointScreen.Y, handleOwner, IntPtr.Zero); DestroyMenu(pMenu); pMenu = IntPtr.Zero; if (nSelected != 0) { InvokeCommand(_oContextMenu, nSelected, _strParentFolder, pointScreen); } } finally { hook.Uninstall(); if (pMenu != IntPtr.Zero) { DestroyMenu(pMenu); } ReleaseAll(); } }