private void RegistEvent(IFrameworkPlugin imagePickerPlugin, ToolStripItem item) { item.Click += button_Click; CommandStatus status = CommandStatus.Unsupported; imagePickerPlugin.QueryStatus(item.Name, ref status); }
private void lbPlugins_SelectedIndexChanged(object sender, EventArgs e) { IFrameworkPlugin plugin = GetPluginByName(lbPlugins.SelectedItem.ToString()); this.settingPanel.Controls.Clear(); Control setting = plugin.GetSetting(); setting.Dock = DockStyle.Fill; this.settingPanel.Controls.Add(setting); }
private void Main_FormClosing(object sender, FormClosingEventArgs e) { IFrameworkPlugin plugin = null; try { foreach (IFrameworkPlugin p in menuItemMapPlugin.Values) { plugin = p; plugin.OnDisconnection(); } } catch { MessageBox.Show(string.Format("插件 {0} 退出失败", plugin.Name), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
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); } }