/// <summary> /// This method receives WindowMessages. It will make the "Open With" and "Send To" work /// by calling HandleMenuMsg and HandleMenuMsg2. /// </summary> /// <param name="m">the Message of the Browser's WndProc</param> /// <returns>true if the message has been handled, false otherwise</returns> protected override void WndProc(ref Message m) { if (iContextMenu != null && m.Msg == (int)NativeMethods.WM.MENUSELECT && ((int)ShellFolders.HiWord(m.WParam) & (int)ShellFolders.MFT.SEPARATOR) == 0 && ((int)ShellFolders.HiWord(m.WParam) & (int)ShellFolders.MFT.POPUP) == 0) { string info = string.Empty; info = ShellFolders.GetCommandString( iContextMenu, (uint)ShellFolders.LoWord(m.WParam) - ShellFolders.CMD_FIRST, false); } if (iContextMenu2 != null && (m.Msg == (int)NativeMethods.WM.INITMENUPOPUP || m.Msg == (int)NativeMethods.WM.MEASUREITEM || m.Msg == (int)NativeMethods.WM.DRAWITEM)) { if (iContextMenu2.HandleMenuMsg( (uint)m.Msg, m.WParam, m.LParam) == ShellFolders.S_OK) { return; } } if (newContextMenu2 != null && ((m.Msg == (int)NativeMethods.WM.INITMENUPOPUP && m.WParam == newSubmenuPtr) || m.Msg == (int)NativeMethods.WM.MEASUREITEM || m.Msg == (int)NativeMethods.WM.DRAWITEM)) { if (newContextMenu2.HandleMenuMsg( (uint)m.Msg, m.WParam, m.LParam) == ShellFolders.S_OK) { return; } } if (iContextMenu3 != null && m.Msg == (int)NativeMethods.WM.MENUCHAR) { if (iContextMenu3.HandleMenuMsg2( (uint)m.Msg, m.WParam, m.LParam, IntPtr.Zero) == ShellFolders.S_OK) { return; } } if (newContextMenu3 != null && m.Msg == (int)NativeMethods.WM.MENUCHAR) { if (newContextMenu3.HandleMenuMsg2( (uint)m.Msg, m.WParam, m.LParam, IntPtr.Zero) == ShellFolders.S_OK) { return; } } base.WndProc(ref m); }
public void ShowContextMenu() { IntPtr contextMenu = IntPtr.Zero, iContextMenuPtr = IntPtr.Zero, iContextMenuPtr2 = IntPtr.Zero, iContextMenuPtr3 = IntPtr.Zero; try { if (ShellFolders.GetIContextMenu(parentShellFolder, pidls, out iContextMenuPtr, out iContextMenu)) { // get some properties about our file(s) bool allFolders = true; bool allInStacks = true; foreach (SystemFile file in paths) { if (!file.IsDirectory) { allFolders = false; } else { bool contains = false; foreach (SystemDirectory dir in StacksManager.StackLocations) { if (dir.Equals(file.FullName)) { contains = true; break; } } if (!contains) { allInStacks = false; } } } contextMenu = ShellFolders.CreatePopupMenu(); uint numPrepended = prependItems(contextMenu, allFolders, allInStacks); iContextMenu.QueryContextMenu( contextMenu, numPrepended, ShellFolders.CMD_FIRST, ShellFolders.CMD_LAST, ShellFolders.CMF.EXPLORE | ShellFolders.CMF.CANRENAME | ((Control.ModifierKeys & Keys.Shift) != 0 ? ShellFolders.CMF.EXTENDEDVERBS : 0)); appendItems(contextMenu, allFolders, allInStacks); Marshal.QueryInterface(iContextMenuPtr, ref ShellFolders.IID_IContextMenu2, out iContextMenuPtr2); Marshal.QueryInterface(iContextMenuPtr, ref ShellFolders.IID_IContextMenu3, out iContextMenuPtr3); try { iContextMenu2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(iContextMenuPtr2, typeof(IContextMenu2)); iContextMenu3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(iContextMenuPtr3, typeof(IContextMenu3)); } catch (Exception) { } uint selected = ShellFolders.TrackPopupMenuEx( contextMenu, ShellFolders.TPM.RETURNCMD, this.x, this.y, this.Handle, IntPtr.Zero); if (selected >= ShellFolders.CMD_FIRST) { string command = ShellFolders.GetCommandString(iContextMenu, selected - ShellFolders.CMD_FIRST, true); // set action strings for us to use in our custom execution function switch ((CairoContextMenuItem)selected) { case CairoContextMenuItem.AddToStacks: command = "addStack"; break; case CairoContextMenuItem.RemoveFromStacks: command = "removeStack"; break; case CairoContextMenuItem.OpenInNewWindow: command = "openWithShell"; break; default: if (command == "open" && allFolders) { // suppress running system code command = "openFolder"; } else { ShellFolders.InvokeCommand( iContextMenu, selected - ShellFolders.CMD_FIRST, parent, new Point(this.x, this.y)); } break; } if (this.itemSelected != null) { itemSelected(command, paths[0].FullName, sender); } } } else { CairoLogger.Instance.Debug("Error retrieving IContextMenu"); } } catch (Exception) { } finally { if (iContextMenu != null) { Marshal.FinalReleaseComObject(iContextMenu); iContextMenu = null; } if (iContextMenu2 != null) { Marshal.FinalReleaseComObject(iContextMenu2); iContextMenu2 = null; } if (iContextMenu3 != null) { Marshal.FinalReleaseComObject(iContextMenu3); iContextMenu3 = null; } if (parentShellFolder != null) { Marshal.FinalReleaseComObject(parentShellFolder); parentShellFolder = null; } if (contextMenu != null) { ShellFolders.DestroyMenu(contextMenu); } if (iContextMenuPtr != IntPtr.Zero) { Marshal.Release(iContextMenuPtr); } if (iContextMenuPtr2 != IntPtr.Zero) { Marshal.Release(iContextMenuPtr2); } if (iContextMenuPtr3 != IntPtr.Zero) { Marshal.Release(iContextMenuPtr3); } for (int i = 0; i < pidls.Length; i++) { Marshal.FreeCoTaskMem(pidls[i]); pidls[i] = IntPtr.Zero; } } }
public void ShowContextMenu() { IntPtr contextMenu = IntPtr.Zero, iContextMenuPtr = IntPtr.Zero, iContextMenuPtr2 = IntPtr.Zero, iContextMenuPtr3 = IntPtr.Zero; try { if (ShellFolders.GetIContextMenu(parentShellFolder, pidls, out iContextMenuPtr, out iContextMenu)) { contextMenu = ShellFolders.CreatePopupMenu(); iContextMenu.QueryContextMenu( contextMenu, 0, ShellFolders.CMD_FIRST, ShellFolders.CMD_LAST, ShellFolders.CMF.EXPLORE | ShellFolders.CMF.CANRENAME | ((Control.ModifierKeys & Keys.Shift) != 0 ? ShellFolders.CMF.EXTENDEDVERBS : 0)); // add to stacks option for folders if (Interop.Shell.Exists(paths[0]) && (File.GetAttributes(this.paths[0]) & FileAttributes.Directory) == FileAttributes.Directory) { ShellFolders.AppendMenu(contextMenu, ShellFolders.MFT.SEPARATOR, 1, string.Empty); ShellFolders.AppendMenu(contextMenu, ShellFolders.MFT.BYCOMMAND, (int)CairoContextMenuItem.AddToStacks, Localization.DisplayString.sInterface_AddToStacks); } Marshal.QueryInterface(iContextMenuPtr, ref ShellFolders.IID_IContextMenu2, out iContextMenuPtr2); Marshal.QueryInterface(iContextMenuPtr, ref ShellFolders.IID_IContextMenu3, out iContextMenuPtr3); try { iContextMenu2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(iContextMenuPtr2, typeof(IContextMenu2)); iContextMenu3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(iContextMenuPtr3, typeof(IContextMenu3)); } catch (Exception) { } uint selected = ShellFolders.TrackPopupMenuEx( contextMenu, ShellFolders.TPM.RETURNCMD, this.x, this.y, this.Handle, IntPtr.Zero); if (selected >= ShellFolders.CMD_FIRST) { string command = ShellFolders.GetCommandString(iContextMenu, selected - ShellFolders.CMD_FIRST, true); if ((CairoContextMenuItem)selected == CairoContextMenuItem.AddToStacks) { command = "addStack"; } else { ShellFolders.InvokeCommand( iContextMenu, selected - ShellFolders.CMD_FIRST, parent, new Point(this.x, this.y)); } if (this.itemSelected != null) { itemSelected(command, paths[0], sender); } } } else { CairoLogger.Instance.Debug("Error retrieving IContextMenu"); } } catch (Exception) { } finally { if (iContextMenu != null) { Marshal.FinalReleaseComObject(iContextMenu); iContextMenu = null; } if (iContextMenu2 != null) { Marshal.FinalReleaseComObject(iContextMenu2); iContextMenu2 = null; } if (iContextMenu3 != null) { Marshal.FinalReleaseComObject(iContextMenu3); iContextMenu3 = null; } if (parentShellFolder != null) { Marshal.FinalReleaseComObject(parentShellFolder); parentShellFolder = null; } if (contextMenu != null) { ShellFolders.DestroyMenu(contextMenu); } if (iContextMenuPtr != IntPtr.Zero) { Marshal.Release(iContextMenuPtr); } if (iContextMenuPtr2 != IntPtr.Zero) { Marshal.Release(iContextMenuPtr2); } if (iContextMenuPtr3 != IntPtr.Zero) { Marshal.Release(iContextMenuPtr3); } for (int i = 0; i < pidls.Length; i++) { Marshal.FreeCoTaskMem(pidls[i]); pidls[i] = IntPtr.Zero; } } }