Exemplo n.º 1
0
        public int QueryContextMenu(IntPtr hMenu, uint iMenu, uint idCmdFirst, uint idCmdLast, uint uFlags)
        {
            if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0)
            {
                return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0));
            }

            #region Initialize Command List
            //this counter keep track of the # of commands
            uint idCommand = 0;
            this.Command = new Dictionary <uint, string>();
            #endregion

            #region Create Command Separator Menu Item
            //Menu Item can be added multiple time, so we only need one separator menuitem
            MENUITEMINFO miiSeparator = new MENUITEMINFO();
            Helper.InitializeMenuitemInfoAsSeparator(ref miiSeparator);
            #endregion

            bool onlyFolder = this.CheckOnlyFolder();

            #region Create Main Menu (Explorer Extender)
            //this counter keep track of the position in menu MainMenu
            uint   iPosition_Menu_Main = 0;
            IntPtr h_Menu_Main         = NativeMethods.CreatePopupMenu();

            MENUITEMINFO mii_Menu_Main = new MENUITEMINFO();
            Helper.InitializeMenuitemInfoAsSubMenu(ref mii_Menu_Main, h_Menu_Main, "Explorer Extender", IntPtr.Zero);

            if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii_Menu_Main))
            {
                return(Marshal.GetHRForLastWin32Error());
            }
            #endregion

            #region Add MenuItem (Break Folder)

            if (!this.isClickedOnEmptyArea && onlyFolder)
            {
                MENUITEMINFO miiBreakFolder = new MENUITEMINFO();

                Helper.InitializeMenuitemInfoAsMenuItem(ref miiBreakFolder, idCmdFirst + idCommand, this.SelectedItems.Count == 1 ? "Break Folder" : "Break Folders", this.hIconUngroup);

                if (!NativeMethods.InsertMenuItem(h_Menu_Main, iPosition_Menu_Main++, true, ref miiBreakFolder))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }

                this.Command.Add(idCommand++, "Break");
            }

            #endregion

            #region Add MenuItem (Group into new folder)

            if (!this.isClickedOnEmptyArea)
            {
                MENUITEMINFO miiBuildFolder = new MENUITEMINFO();
                Helper.InitializeMenuitemInfoAsMenuItem(ref miiBuildFolder, idCmdFirst + idCommand, "Group", this.hIconGroup);

                if (!NativeMethods.InsertMenuItem(h_Menu_Main, iPosition_Menu_Main++, true, ref miiBuildFolder))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }

                this.Command.Add(idCommand++, "Build");
            }

            #endregion

            #region Add Separator
            if (!this.isClickedOnEmptyArea)
            {
                if (!NativeMethods.InsertMenuItem(h_Menu_Main, iPosition_Menu_Main++, true, ref miiSeparator))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }
            }
            #endregion

            if (!this.isClickedOnEmptyArea)
            {
                #region Create Submenu (Rename)
                //this counter keep track of the position in menu MainMenu
                uint   iPosition_Menu_Rename = 0;
                IntPtr h_Menu_Rename         = NativeMethods.CreatePopupMenu();

                MENUITEMINFO mii_Menu_Rename = new MENUITEMINFO();
                Helper.InitializeMenuitemInfoAsSubMenu(ref mii_Menu_Rename, h_Menu_Rename, "Rename", this.hIconRename);

                if (!NativeMethods.InsertMenuItem(h_Menu_Main, iPosition_Menu_Main++, true, ref mii_Menu_Rename))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }
                #endregion

                #region Add MenuItem (Search & Replace)
                MENUITEMINFO miiReplace = new MENUITEMINFO();
                Helper.InitializeMenuitemInfoAsMenuItem(ref miiReplace, idCmdFirst + idCommand, "Search and Replace", this.hIconReplace);

                if (!NativeMethods.InsertMenuItem(h_Menu_Rename, iPosition_Menu_Rename++, true, ref miiReplace))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }

                this.Command.Add(idCommand++, "Replace");
                #endregion

                #region Add MenuItem (URL Decode)
                MENUITEMINFO miiUrlEncode = new MENUITEMINFO();
                Helper.InitializeMenuitemInfoAsMenuItem(ref miiUrlEncode, idCmdFirst + idCommand, "Url Decode", this.hIconUrl);

                if (!NativeMethods.InsertMenuItem(h_Menu_Rename, iPosition_Menu_Rename++, true, ref miiUrlEncode))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }

                this.Command.Add(idCommand++, "UrlDecode");
                #endregion

                #region Add MenuItem (Html Decode)
                MENUITEMINFO miiHtmlEncode = new MENUITEMINFO();
                Helper.InitializeMenuitemInfoAsMenuItem(ref miiHtmlEncode, idCmdFirst + idCommand, "Html Decode", this.hIconHtml);

                if (!NativeMethods.InsertMenuItem(h_Menu_Rename, iPosition_Menu_Rename++, true, ref miiHtmlEncode))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }

                this.Command.Add(idCommand++, "HtmlDecode");
                #endregion
            }

            #region Create Submenu (File Mover)

            uint   iPosition_Menu_FileMover = 0;
            IntPtr h_Menu_FileMover         = NativeMethods.CreatePopupMenu();

            MENUITEMINFO mii_Menu_FileMover = new MENUITEMINFO();
            Helper.InitializeMenuitemInfoAsSubMenu(ref mii_Menu_FileMover, h_Menu_FileMover, "File Mover", IntPtr.Zero);

            if (!NativeMethods.InsertMenuItem(h_Menu_Main, iPosition_Menu_Main++, true, ref mii_Menu_FileMover))
            {
                return(Marshal.GetHRForLastWin32Error());
            }

            #endregion

            int fileCount = FileMover.GetListCount();

            #region Add MenuItem (Move File)
            if (CheckOnlyOneFolder() && fileCount > 0)
            {
                MENUITEMINFO miiFileMoverMove = new MENUITEMINFO();
                Helper.InitializeMenuitemInfoAsMenuItem(ref miiFileMoverMove, idCmdFirst + idCommand, fileCount == 1 ? "Move File Here" : "Move Files here", IntPtr.Zero);

                if (!NativeMethods.InsertMenuItem(h_Menu_FileMover, iPosition_Menu_FileMover++, true, ref miiFileMoverMove))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }

                this.Command.Add(idCommand++, "FileMoverMove");

                if (!NativeMethods.InsertMenuItem(h_Menu_FileMover, iPosition_Menu_FileMover++, true, ref miiSeparator))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }
            }
            #endregion

            #region Add MenuItem (Add to List)
            MENUITEMINFO miiFileMoverAdd = new MENUITEMINFO();
            Helper.InitializeMenuitemInfoAsMenuItem(ref miiFileMoverAdd, idCmdFirst + idCommand, "Add to list", this.hIconAdd);

            if (!NativeMethods.InsertMenuItem(h_Menu_FileMover, iPosition_Menu_FileMover++, true, ref miiFileMoverAdd))
            {
                return(Marshal.GetHRForLastWin32Error());
            }

            this.Command.Add(idCommand++, "FileMoverAdd");
            #endregion

            #region Add MenuItem (Clear List)
            if (fileCount > 0)
            {
                MENUITEMINFO miiFileMoverClear = new MENUITEMINFO();
                Helper.InitializeMenuitemInfoAsMenuItem(ref miiFileMoverClear, idCmdFirst + idCommand, "Clear List", this.hIconDelete);

                if (!NativeMethods.InsertMenuItem(h_Menu_FileMover, iPosition_Menu_FileMover++, true, ref miiFileMoverClear))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }

                this.Command.Add(idCommand++, "FileMoverClear");
            }
            #endregion

            if (!NativeMethods.InsertMenuItem(h_Menu_FileMover, iPosition_Menu_FileMover++, true, ref miiSeparator))
            {
                return(Marshal.GetHRForLastWin32Error());
            }

            var fileList = FileMover.GetList();

            #region Add MenuItem (Remove File From List)
            foreach (var i in fileList)
            {
                MENUITEMINFO mii_MenuItem_Remove = new MENUITEMINFO();
                Helper.InitializeMenuitemInfoAsMenuItem(ref mii_MenuItem_Remove, idCmdFirst + idCommand, i, this.hIconTrash);

                if (!NativeMethods.InsertMenuItem(h_Menu_FileMover, iPosition_Menu_FileMover++, true, ref mii_MenuItem_Remove))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }

                this.Command.Add(idCommand++, i);
            }
            #endregion

            return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, idCommand));
        }
Exemplo n.º 2
0
        public void InvokeCommand(IntPtr pici)
        {
            uint iCommand = (uint)Helper.GetCommandOffsetId(pici);

            if (!this.Command.ContainsKey(iCommand))
            {
                return;
            }

            string str = this.Command[iCommand];

            if (str == "Break")
            {
                FileOperations.BreakFolder(this.SelectedItems);
            }
            else if (str == "Build")
            {
                FileOperations.BuildFolder(this.SelectedItems);
            }
            else if (str == "Replace")
            {
                this.SelectedItems.ForEach(fi =>
                {
                    FileInfo fileInfo = new FileInfo(fi);
                    fileInfo.ReplaceFileName();
                });
            }
            else if (str == "UrlDecode")
            {
                this.SelectedItems.ForEach(fi =>
                {
                    FileInfo fileInfo = new FileInfo(fi);
                    fileInfo.DecodeFileNameUrl();
                });
            }
            else if (str == "HtmlDecode")
            {
                this.SelectedItems.ForEach(fi =>
                {
                    FileInfo fileInfo = new FileInfo(fi);
                    fileInfo.DecodeFileNameHtml();
                });
            }
            else if (str == "FileMoverMove")
            {
                FileMover.ExecuteListAction(this.SelectedItems[0]);
            }
            else if (str == "FileMoverAdd")
            {
                this.SelectedItems.ForEach(i => FileMover.AddPathToList(i));
            }
            else if (str == "FileMoverClear")
            {
                FileMover.ClearList();
            }
            else
            {
                var list = FileMover.GetList();
                if (list.Contains(str))
                {
                    FileMover.RemovePathFromList(str);
                }
                else
                {
                    Marshal.ThrowExceptionForHR(WinError.E_FAIL);
                }
            }
        }