Exemplo n.º 1
0
        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;
                }
            }
        }
Exemplo n.º 2
0
        private void ShowFolderMenu()
        {
            IntPtr contextMenu = IntPtr.Zero, viewSubMenu = IntPtr.Zero;
            IntPtr newContextMenuPtr = IntPtr.Zero, newContextMenuPtr2 = IntPtr.Zero, newContextMenuPtr3 = IntPtr.Zero;

            newSubmenuPtr = IntPtr.Zero;

            try
            {
                contextMenu = ShellFolders.CreatePopupMenu();

                ShellFolders.AppendMenu(contextMenu, ShellFolders.MFT.BYCOMMAND, (int)CairoContextMenuItem.OpenInNewWindow, Localization.DisplayString.sStacks_OpenInNewWindow);
                if (!StacksManager.StackLocations.Contains(directory))
                {
                    ShellFolders.AppendMenu(contextMenu, ShellFolders.MFT.BYCOMMAND, (int)CairoContextMenuItem.AddToStacks, Localization.DisplayString.sInterface_AddToStacks);
                }
                else
                {
                    ShellFolders.AppendMenu(contextMenu, ShellFolders.MFT.BYCOMMAND, (int)CairoContextMenuItem.RemoveFromStacks, Localization.DisplayString.sInterface_RemoveFromStacks);
                }

                ShellFolders.AppendMenu(contextMenu, ShellFolders.MFT.SEPARATOR, 1, string.Empty);

                ShellFolders.AppendMenu(contextMenu, ShellFolders.MFT.BYCOMMAND, (int)CairoContextMenuItem.Paste, Localization.DisplayString.sInterface_Paste);

                if (GetNewContextMenu(out newContextMenuPtr, out newContextMenu))
                {
                    ShellFolders.AppendMenu(contextMenu, ShellFolders.MFT.SEPARATOR, 1, string.Empty);
                    newContextMenu.QueryContextMenu(
                        contextMenu,
                        5,
                        ShellFolders.CMD_FIRST,
                        ShellFolders.CMD_LAST,
                        ShellFolders.CMF.NORMAL);

                    newSubmenuPtr = ShellFolders.GetSubMenu(contextMenu, 5);

                    Marshal.QueryInterface(newContextMenuPtr, ref ShellFolders.IID_IContextMenu2, out newContextMenuPtr2);
                    Marshal.QueryInterface(newContextMenuPtr, ref ShellFolders.IID_IContextMenu3, out newContextMenuPtr3);

                    try
                    {
                        newContextMenu2 =
                            (IContextMenu2)Marshal.GetTypedObjectForIUnknown(newContextMenuPtr2, typeof(IContextMenu2));

                        newContextMenu3 =
                            (IContextMenu3)Marshal.GetTypedObjectForIUnknown(newContextMenuPtr3, typeof(IContextMenu3));
                    }
                    catch (Exception) { }
                }

                ShellFolders.AppendMenu(contextMenu, ShellFolders.MFT.SEPARATOR, 0, string.Empty);
                ShellFolders.AppendMenu(contextMenu, 0, (int)CairoContextMenuItem.Properties, Localization.DisplayString.sInterface_Properties);

                CairoContextMenuItem selected = (CairoContextMenuItem)ShellFolders.TrackPopupMenuEx(
                    contextMenu,
                    ShellFolders.TPM.RETURNCMD,
                    this.x,
                    this.y,
                    this.Handle,
                    IntPtr.Zero);

                if ((int)selected >= ShellFolders.CMD_FIRST)
                {
                    string command = "";
                    switch (selected)
                    {
                    case CairoContextMenuItem.OpenInNewWindow:
                        command = "openWithShell";
                        break;

                    case CairoContextMenuItem.AddToStacks:
                        command = "addStack";
                        break;

                    case CairoContextMenuItem.RemoveFromStacks:
                        command = "removeStack";
                        break;

                    case CairoContextMenuItem.Properties:
                        command = "properties";
                        ShellFolders.InvokeCommand(
                            folder,
                            parentShellFolder,
                            new IntPtr[] { folderPidl },
                            command,
                            new Point(this.x, this.y));
                        break;

                    case CairoContextMenuItem.Paste:
                        command = "paste";
                        ShellFolders.InvokeCommand(
                            folder,
                            parentShellFolder,
                            new IntPtr[] { folderPidl },
                            command,
                            new Point(this.x, this.y));
                        break;

                    default:
                        if ((uint)selected <= ShellFolders.CMD_LAST)
                        {
                            ShellFolders.InvokeCommand(
                                newContextMenu,
                                (uint)selected - ShellFolders.CMD_FIRST,
                                this.folder,
                                new Point(this.x, this.y));
                        }
                        break;
                    }

                    if (this.folderItemSelected != null)
                    {
                        folderItemSelected(command, folder);
                    }
                }
            }
            catch (Exception) { }
            finally
            {
                if (newContextMenu != null)
                {
                    Marshal.FinalReleaseComObject(newContextMenu);
                    newContextMenu = null;
                }

                if (newContextMenu2 != null)
                {
                    Marshal.FinalReleaseComObject(newContextMenu2);
                    newContextMenu2 = null;
                }

                if (newContextMenu3 != null)
                {
                    Marshal.FinalReleaseComObject(newContextMenu3);
                    newContextMenu3 = null;
                }

                if (contextMenu != null)
                {
                    ShellFolders.DestroyMenu(contextMenu);
                }

                if (viewSubMenu != null)
                {
                    ShellFolders.DestroyMenu(viewSubMenu);
                }

                if (newContextMenuPtr != IntPtr.Zero)
                {
                    Marshal.Release(newContextMenuPtr);
                }

                if (newContextMenuPtr2 != IntPtr.Zero)
                {
                    Marshal.Release(newContextMenuPtr2);
                }

                if (newContextMenuPtr3 != IntPtr.Zero)
                {
                    Marshal.Release(newContextMenuPtr3);
                }

                newSubmenuPtr = IntPtr.Zero;

                Marshal.FreeCoTaskMem(folderPidl);
                Marshal.FreeCoTaskMem(folderRelPidl);
                folderPidl    = IntPtr.Zero;
                folderRelPidl = IntPtr.Zero;
            }
        }
Exemplo n.º 3
0
        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;
                }
            }
        }