コード例 #1
0
        /// <summary>
        /// 从可用插件列表移除一个插件,若已加载,卸载
        /// 实现Menus.Remove接口
        /// </summary>
        /// <param name="menuName"></param>
        /// <returns></returns>
        public bool Remove(string menuName)
        {
            System.Windows.Forms.ToolStripItem m;
            string key = MenuTableKey(menuName);

            System.Windows.Forms.MenuStrip parentMenu  = null;
            System.Windows.Forms.ToolStrip parentStrip = null;
            try
            {
                if (m_MenuTable.ContainsKey(key))
                {
                    m = (System.Windows.Forms.ToolStripItem)(m_MenuTable[key]);
                    if (m.GetType().Name == "ToolStripMenuItem")//是Form.ToolStripMenuItem类型,则从m_MenuTable移除该菜单项 的子项
                    {
                        RemoveSubMenusFromMenuTable((System.Windows.Forms.ToolStripMenuItem)m);
                    }
                    m_MenuTable.Remove(key);//移除该项

                    try
                    {
                        object o = m.GetCurrentParent();//ToolStripItem的父对象ToolStrip
                        if (o is System.Windows.Forms.MenuStrip)
                        {
                            parentMenu = (System.Windows.Forms.MenuStrip)o;
                        }
                        if (o is System.Windows.Forms.ToolStrip)
                        {
                            parentStrip = (System.Windows.Forms.ToolStrip)o;
                        }
                        if (parentMenu != null)//是MenuStrip类型,从窗体中移除该菜单项
                        {
                            parentMenu.Items.Remove(m);
                        }
                        else if (parentStrip != null)//是ToolStrip类型,从窗体中移除该菜单项
                        {
                            parentStrip.Items.Remove(m);
                        }
                        else //既不是MenuStrip也不是Toolstrip,是上级菜单项,则直接移除
                        {
                            Program.frmMain.menuStrip1.Items.Remove(m);
                        }
                    }
                    catch (Exception e)
                    {
                        Program.CustomExceptionHandler.OnThreadException(e);
                        return(false);
                    }

                    System.Diagnostics.StackFrame stackFrame = new System.Diagnostics.StackFrame(1);
                    string pluginfile = stackFrame.GetMethod().Module.FullyQualifiedName;
                    if (!pluginfile.ToLower().EndsWith("mapwingis.exe"))//不是以mapwingis.exe结尾,则表示是插件菜单
                    {
                        //获取插件Key
                        string     pluginkey = "";
                        PluginInfo info      = new PluginInfo();
                        if (info.Init(pluginfile, typeof(Interfaces.IPlugin).GUID) && info.Key != "")
                        {
                            pluginkey = info.Key;
                        }
                        //如果获取插件Key成功,则移除该插件
                        if (pluginkey != "")
                        {
                            MenuTrackerRemove(menuName, pluginkey);
                        }
                    }

                    // 当菜单栏所有项都移除了,则移除菜单栏(menuStrip1)
                    if (Program.frmMain.menuStrip1.Items.Count == 0)
                    {
                        //修改,menuStrip1不是在TopToolStripPanel中
                        Program.frmMain.Controls.Remove(Program.frmMain.menuStrip1);
                        //Program.frmMain.StripDocker.TopToolStripPanel.Controls.Remove(Program.frmMain.menuStrip1);
                        //如果所有的控件都移除了,就隐藏工具条(TopToolStripPanel)
                        if (Program.frmMain.StripDocker.TopToolStripPanel.Controls.Count == 0)
                        {
                            Program.frmMain.StripDocker.TopToolStripPanel.Hide();
                        }
                    }
                    return(true);
                }
                //菜单中不包含该名字的菜单,则移除失败
                return(false);
            }
            catch (Exception ex)
            {
                Program.g_error = ex.ToString();
                Program.ShowError(ex);
                return(false);
            }
        }