Now that the framework is ready, we hope to serve a certain target. We need to provide some basic services so that users can continue to expand their functions. The first thought of function is to manage menus and toolbar. Next we will implement some more popular functions, such as dock toolbar.
How to implement these services? We hope our plug-ins can get applications at runtime.ProgramYou can add a project to the menu bar, toolbar bar, or toolbar. For example, you can add a menu item and a toolbar button. To obtain a menu or toolbar at runtime, We need to assign a key to the toolbar of each menu and put it in a dictionary. When necessary, we use this key to obtain the instance. For this key, my example is simply his name. Let's take a look at the toolstripservice'sCode:CopySave
Using System; Using System. Collections. Generic; Using System. text; Using System. Windows. forms; Namespace Pluginframework { Public Class Toolstripservice: itoolstripservice { Private Iapplication application = Null ; Private Dictionary <string, toolstrip> toolstrips = New Dictionary < String , Toolstrip> (); Public Toolstripservice (iapplication application ){ This . Application = application ;} # Region itoolstripservice members Public System. Windows. Forms. toolstrip gettoolstrip ( String Toolstripname) {toolstrip = Null ; If (Toolstrips. containskey (toolstripname) {toolstrip = toolstrips [toolstripname];} Return Toolstrip ;} Public Void Addtoolstrip ( String Toolstripname, system. Windows. Forms. toolstrip ){ If (Toolstrips. containskey (toolstripname) {MessageBox. Show ( "The tool strip name has existed! " );} Else {Toolstrips [toolstripname] = toolstrip; // If no panel is specified for the toolstrip, it is added to the top by default. If (Application. toptoolpanel! = Null ) {Application. toptoolpanel. Controls. Add (toolstrip );}}} Public Void Addtoolstrip (String Toolstripname, system. Windows. Forms. toolstrip, toolstripdockstate option ){ If (Toolstrips. containskey (toolstripname) {MessageBox. Show ( "The tool strip name has existed! " );} Else {Toolstrips [toolstripname] = toolstrip; Switch (Option ){ Case Toolstripdockstate. left: If (Application. lefttoolpanel! = Null ) {Application. lefttoolpanel. Controls. Add (toolstrip );} Break ; Case Toolstripdockstate. Right: If (Application. righttoolpanel! = Null ) {Application. righttoolpanel. Controls. Add (toolstrip );} Break ; Case Toolstripdockstate. Top: If (Application. toptoolpanel! = Null ) {Application. toptoolpanel. Controls. Add (toolstrip );} Break ; Case Toolstripdockstate. Bottom: If (Application. bottomtoolpanel! = Null ) {Application. bottomtoolpanel. Controls. Add (toolstrip );} Break ;}}} Public Void Removetoolstrip ( String Toolstripname) {toolstrip = gettoolstrip (toolstripname ); If (Toolstrip! = Null ){ If (Application. toptoolpanel! = Null & Amp; application. toptoolpanel. Controls. Contains (toolstrip) {application. toptoolpanel. Controls. Remove (toolstrip );} Else If (Application. bottomtoolpanel! = Null & Amp; application. bottomtoolpanel. Controls. Contains (toolstrip) {application. bottomtoolpanel. Controls. Remove (toolstrip );} Else If (Application. lefttoolpanel! = Null & Amp; application. lefttoolpanel. Controls. Contains (toolstrip) {application. lefttoolpanel. Controls. Remove (toolstrip );} Else If (Application. righttoolpanel! = Null & Amp; application. righttoolpanel. Controls. Contains (toolstrip) {application. righttoolpanel. Controls. Remove (toolstrip) ;}} toolstrips. Remove (toolstripname );} # Endregion }}
For a view or a tool bar to be docked, it is best not to put an instance directly in the dictionary, but to put the object type into the dictionary, because both the view and the tool bar to be docked are derived from form, therefore, when the view or dock toolbar is closed, the object is destroyed, and the object is created in the load method of the plug-in, it is impossible for us to call the load method of the plug-in, which makes it inconvenient for us to use. Therefore, we should register the type and implement a show method in the service, for ease of demonstration, I instantiate the instance in load and put the instance in the dictionary.
The following figure shows the plug-in's Dock toolbar, toolbar, a new menu "View", and a sub-menu of the View menu:
There is really no time recently,ArticleThe sending speed is very slow, and the writing is also very wrong. If you are not clear about it, refer to it.Source codeThank you for your patience.
Source code
Http://files.cnblogs.com/guanjinke/pluginsample3.rar