Beispiel #1
0
        private MenuComponent.MenuItem GetMenuItem(string name, List <MenuComponent.MenuItem> menuItems)
        {
            if (string.IsNullOrEmpty(name) == true)
            {
                return(null);
            }

            MenuComponent.MenuItem result = null;

            foreach (MenuComponent.MenuItem menuItem in menuItems)
            {
                if (menuItem.Name == name)
                {
                    result = menuItem;
                }
                else
                {
                    result = GetMenuItem(name, menuItem.ChildMenuItem);
                }
            }
            return(result);
        }
Beispiel #2
0
        private void Main_Load(object sender, EventArgs e)
        {
            PluginManager pluginManager = PluginManager.Create(this.GetType().Assembly, typeof(IFrameworkPlugin), this);

            List <MenuItemAttribute> menuItemAttributes = new List <MenuItemAttribute>();

            foreach (IPlugin plugin in pluginManager.Plugins)
            {
                IFrameworkPlugin imagePickerPlugin = plugin as IFrameworkPlugin;

                object[] attributes = imagePickerPlugin.GetType().GetCustomAttributes(typeof(MenuItemAttribute), false);
                if (attributes.Length == 0 || attributes.Length > 1)
                {
                    throw new AccumulateTreasureException("每个插件只允许注册一个菜单按钮.");
                }
                MenuItemAttribute menuItemAttribute = (MenuItemAttribute)attributes[0];

                if (menuItemMapPlugin.ContainsKey(menuItemAttribute.Name))
                {
                    throw new AccumulateTreasureException(string.Format("{0} 插件名重复.", imagePickerPlugin.Name));
                }

                menuItemAttributes.Add(menuItemAttribute);

                menuItemMapPlugin.Add(menuItemAttribute.Name, imagePickerPlugin);

                plugins.Add(imagePickerPlugin);
            }

            // add the parent menuitem.
            foreach (MenuComponent.MenuItemAttribute menuItemAttribute in menuItemAttributes)
            {
                if (string.IsNullOrEmpty(menuItemAttribute.ParentName) == true)
                {
                    menuItems.Add(new MenuComponent.MenuItem(menuItemAttribute.Name, menuItemAttribute.Image));
                }
            }

            foreach (MenuComponent.MenuItemAttribute menuItemAttribute in menuItemAttributes)
            {
                if (string.IsNullOrEmpty(menuItemAttribute.ParentName) == false)
                {
                    MenuComponent.MenuItem menuItem = GetMenuItem(menuItemAttribute.ParentName);
                    if (menuItem == null)
                    {
                        MenuComponent.MenuItem parentItem = new MenuComponent.MenuItem(menuItemAttribute.ParentName, menuItemAttribute.ParentImage);
                        parentItem.ChildMenuItem.Add(new MenuComponent.MenuItem(menuItemAttribute.Name, menuItemAttribute.Image));

                        menuItems.Add(parentItem);
                    }
                    else
                    {
                        menuItem.ChildMenuItem.Add(new MenuComponent.MenuItem(menuItemAttribute.Name, menuItemAttribute.Image));
                    }
                }
            }

            foreach (MenuComponent.MenuItem menuItem in menuItems)
            {
                if (menuItem.ChildMenuItem.Count == 0)
                {
                    ToolStripButton button = new ToolStripButton();
                    button.DisplayStyle          = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                    button.Image                 = Resources.GetImage(menuItem.Image);
                    button.DisplayStyle          = ToolStripItemDisplayStyle.ImageAndText;
                    button.ImageTransparentColor = System.Drawing.Color.Magenta;
                    button.Name = menuItem.Name;
                    button.Text = menuItem.Name;

                    RegistEvent(menuItemMapPlugin[menuItem.Name], button);

                    this.menu.Items.AddRange(new ToolStripItem[] { button });
                }
                else
                {
                    ToolStripDropDownButton dropDownButton = new ToolStripDropDownButton();
                    dropDownButton.DisplayStyle          = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
                    dropDownButton.Image                 = Resources.GetImage(menuItem.Image);
                    dropDownButton.ImageTransparentColor = System.Drawing.Color.Magenta;
                    dropDownButton.Name = menuItem.Name;
                    dropDownButton.Text = menuItem.Name;
                    List <ToolStripMenuItem> childItems = new List <ToolStripMenuItem>();
                    foreach (MenuComponent.MenuItem item in menuItem.ChildMenuItem)
                    {
                        ToolStripMenuItem button = new ToolStripMenuItem();
                        button.DisplayStyle          = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                        button.Image                 = Resources.GetImage(item.Image);
                        button.DisplayStyle          = ToolStripItemDisplayStyle.ImageAndText;
                        button.ImageTransparentColor = System.Drawing.Color.Magenta;
                        button.Name = item.Name;
                        button.Text = item.Name;

                        if (menuItemMapPlugin.ContainsKey(item.Name))
                        {
                            RegistEvent(menuItemMapPlugin[item.Name], button);
                        }
                        childItems.Add(button);
                    }
                    dropDownButton.DropDownItems.AddRange(childItems.ToArray());

                    this.menu.Items.AddRange(new ToolStripItem[] { dropDownButton });
                }
            }

            LoadSettingMenuItem();

            // Execute plugin 1;
            if (plugins.Count > 0)
            {
                bool handled = false;
                plugins[0].Execute(plugins[0].Name, ref handled);
            }
        }