⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 browsercontextmenuwrappers.cs

📁 C#界面编程
💻 CS
📖 第 1 页 / 共 5 页
字号:
                    }
                    catch (Exception) { }
                }

                #endregion

                #region Properties Menu

                if (!br.SelectedItem.Equals(br.ShellBrowser.DesktopItem))
                {
                    ShellAPI.AppendMenu(contextMenu, ShellAPI.MFT.SEPARATOR, 0, string.Empty);
                    ShellAPI.AppendMenu(contextMenu, 0, (int)CMD_CUSTOM.Properties, "Properties");
                }

                #endregion

                #endregion

                CMD_CUSTOM selected = (CMD_CUSTOM)ShellAPI.TrackPopupMenuEx(
                                    contextMenu,
                                    ShellAPI.TPM.RETURNCMD,
                                    ptInvoke.X,
                                    ptInvoke.Y,
                                    this.Handle,
                                    IntPtr.Zero);

                #region Invoke

                if ((int)selected >= ShellAPI.CMD_FIRST)
                {
                    switch (selected)
                    {
                        #region Normal Views

                        case CMD_CUSTOM.Tiles:
                            br.FileView.View = View.Tile;
                            br.ResetSpecialView();
                            provider.ReleaseStorage();
                            provider.ReleaseStream();
                            break;
                        case CMD_CUSTOM.Icons:
                            br.FileView.View = View.LargeIcon;
                            br.ResetSpecialView();
                            provider.ReleaseStorage();
                            provider.ReleaseStream();
                            break;
                        case CMD_CUSTOM.List:
                            br.FileView.View = View.List;
                            br.ResetSpecialView();
                            provider.ReleaseStorage();
                            provider.ReleaseStream();
                            break;
                        case CMD_CUSTOM.Details:
                            br.FileView.SuspendHeaderContextMenu = true;
                            br.FileView.View = View.Details;
                            br.ResetSpecialView();
                            provider.ReleaseStorage();
                            provider.ReleaseStream();
                            break;

                        #endregion

                        #region Folder Items

                        case CMD_CUSTOM.Properties:
                            ContextMenuHelper.InvokeCommand(
                                br.SelectedItem.ParentItem,
                                new IntPtr[] { br.SelectedItem.PIDLRel.Ptr },
                                "properties",
                                ptInvoke);
                            break;

                        case CMD_CUSTOM.Paste:
                            ContextMenuHelper.InvokeCommand(
                                br.SelectedItem.ParentItem,
                                new IntPtr[] { br.SelectedItem.PIDLRel.Ptr },
                                "paste",
                                ptInvoke);
                            break;

                        case CMD_CUSTOM.Paste_ShortCut:
                            ContextMenuHelper.InvokeCommand(
                                br.SelectedItem.ParentItem,
                                new IntPtr[] { br.SelectedItem.PIDLRel.Ptr },
                                "pastelink",
                                ptInvoke);
                            break;

                        #endregion

                        default:
                            #region New
                            if ((uint)selected <= ShellAPI.CMD_LAST)
                            {
                                lock (br.ShellBrowser)
                                {
                                    br.NewItemCreated = true;
                                }

                                ContextMenuHelper.InvokeCommand(
                                    newContextMenu,
                                    (uint)selected - ShellAPI.CMD_FIRST,
                                    ShellItem.GetRealPath(br.SelectedItem),
                                    ptInvoke);
                            }
                            #endregion
                            #region Special Views
                            else
                            {
                                int index = (int)selected - (int)CMD_CUSTOM.SpecialView;

                                if (br.FileView.Alignment != ListViewAlignment.Left)
                                    br.FileView.Alignment = ListViewAlignment.Left;
                                
                                br.FileView.View = View.LargeIcon;
                                br.CurrentViewPlugin = pluginWrapper.ViewPlugins[index] as IViewPlugin;
                                br.SpecialViewPanelVisible = true;
                                br.SpecialViewPanel.Controls.Add(br.CurrentViewPlugin.ViewControl);
                                br.CurrentViewPlugin.ViewControl.Dock = DockStyle.Fill;
                            }
                            #endregion
                            break;
                    }
                }

                #endregion
            }
            catch (Exception) { }
            #region Finally
            finally
            {
                if (newContextMenu != null)
                {
                    Marshal.ReleaseComObject(newContextMenu);
                    newContextMenu = null;
                }

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

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

                if (contextMenu != null)
                    ShellAPI.DestroyMenu(contextMenu);

                if (viewSubMenu != null)
                    ShellAPI.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;
            }
            #endregion
        }

        #endregion

        #region IViewPlugin Methods

        private delegate void FileSelected(IFileInfoProvider provider, ShellItem item);
        private delegate void FolderSelected(IDirInfoProvider provider, ShellItem item);

        void viewPluginTimer_Tick(object sender, EventArgs e)
        {
            if (!viewPluginWorker.IsBusy)
            {
                viewPluginWorker.RunWorkerAsync();
                viewPluginTimer.Stop();
            }
        }

        void br_SelectedFolderChanged(object sender, SelectedFolderChangedEventArgs e)
        {
            if (br.CurrentViewPlugin != null)
                br.CurrentViewPlugin.Reset();
        }

        void FileView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected && br.SelectionChange && br.CurrentViewPlugin != null)
            {
                viewPluginTimer.Stop();
                provider.ProviderItem = e.Item.Tag as ShellItem;
                viewPluginTimer.Start();
            }
        }

        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            provider.ReleaseStorage();
            provider.ReleaseStream();

            try
            {
                if (provider.ProviderItem.CanRead)
                {
                    IAsyncResult asyncResult = null;

                    if (provider.ProviderItem.IsFolder)
                    {
                        FolderSelected selectedDelegate = new FolderSelected(br.CurrentViewPlugin.FolderSelected);
                        asyncResult = br.CurrentViewPlugin.ViewControl.BeginInvoke(selectedDelegate, provider, provider.ProviderItem);
                    }
                    else
                    {
                        FileSelected selectedDelegate = new FileSelected(br.CurrentViewPlugin.FileSelected);
                        asyncResult = br.CurrentViewPlugin.ViewControl.BeginInvoke(selectedDelegate, provider, provider.ProviderItem);
                    }

                    asyncResult.AsyncWaitHandle.WaitOne();
                }
                else
                    br.CurrentViewPlugin.Reset();
            }
            catch (Exception) { }
        }

        #endregion

        #region ToolTip

        void SetToolTip(ListViewItem listItem)
        {
            IntPtr queryInfoPtr;
            IQueryInfo queryInfo;

            if (ShellHelper.GetIQueryInfo((ShellItem)listItem.Tag, out queryInfoPtr, out queryInfo))
            {
                string info;
                queryInfo.GetInfoTip(ShellAPI.QITIPF.DEFAULT, out info);

                //if (string.IsNullOrEmpty(info))
                    //queryInfo.GetInfoTip(ShellAPI.QITIPF.USESLOWTIP, out info);

                Marshal.ReleaseComObject(queryInfo);
                Marshal.Release(queryInfoPtr);

                listItem.ToolTipText = info;
            }
        }

        void FileView_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
        {
            br.BeginInvoke(toolTipDelegate, e.Item);
        }

        #endregion
    }

    #region ContextMenuHelper

    /// <summary>
    /// This class provides static methods which are being used to retrieve IContextMenu's for specific items
    /// and to invoke certain commands.
    /// </summary>
    internal static class ContextMenuHelper
    {
        #region GetCommandString

        public static string GetCommandString(IContextMenu iContextMenu, uint idcmd, bool executeString)
        {
            string command = GetCommandStringW(iContextMenu, idcmd, executeString);

            if (string.IsNullOrEmpty(command))
                command = GetCommandStringA(iContextMenu, idcmd, executeString);

            return command;
        }

        /// <summary>
        /// Retrieves the command string for a specific item from an iContextMenu (Ansi)
        /// </summary>
        /// <param name="iContextMenu">the IContextMenu to receive the string from</param>
        /// <param name="idcmd">the id of the specific item</param>
        /// <param name="executeString">indicating whether it should return an execute string or not</param>
        /// <returns>if executeString is true it will return the executeString for the item, 
        /// otherwise it will return the help info string</returns>
        public static string GetCommandStringA(IContextMenu iContextMenu, uint idcmd, bool executeString)
        {
            string info = string.Empty;
            byte[] bytes = new byte[256];
            int index;

            iContextMenu.GetCommandString(
                idcmd,
                (executeString ? ShellAPI.GCS.VERBA : ShellAPI.GCS.HELPTEXTA),
                0,
                bytes,
                ShellAPI.MAX_PATH);

            index = 0;
            while (index < bytes.Length && bytes[index] != 0)
            { index++; }

            if (index < bytes.Length)
                info = Encoding.Default.GetString(bytes, 0, index);

            return info;
        }

        /// <summary>
        /// Retrieves the command string for a specific item from an iContextMenu (Unicode)
        /// </summary>
        /// <param name="iContextMenu">the IContextMenu to receive the string from</param>
        /// <param name="idcmd">the id of the specific item</param>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -