Introduction-what can the Visual Studio. NET plug-in do?
The Visual Studio. NET plug-in can do many things, such:
1. Compile a coderush-like Development Environment Code auxiliary tool
2. Compile a code template tool such as codesmith
3. Write a code generator to automatically generate code based on custom conditions. For example, some popular code generation tools can be used more conveniently if they are integrated with the development environment.
4. Compile a debugging tool such as datasetpryer to view the dataset content during debugging.
5. You can even integrate Google search engine in vs. Net or integrate MSN into vs. net.
I will not list them here. All in all, anything that can be related to the Visual Studio. NET development environment can be done in the form of a plug-in.
Development. net plug-in. Currently, there are two forms: one is the vs external program generated using the vs wizard; the other is the Microsoft vsip Development Kit (Visual Studio industry partner: Microsoft Partner Program ). This article discusses the first method.
Ii. Program Framework Overview
In Visual Studio.. net, select "New Project", "other projects", and "Extension Project. ", generate code according to the wizard, and finally generate two project files, one is the external program project, the other is the external program installation project. You can see in the external project that the generated project file contains a connect. CS file, which has the following parts:
1. class inheritance interface and constant definition
[Guidattribute ("952a6cff-8516-4da0-b0ba-519cb9614525"), progid ("stdtools. Connect")]
Public class CONNECT: object, extensibility. idtextensibility2, idtcommandtarget
{...}
The CONNECT class inherits from two interfaces. One is the extensibility. idtextensibility2 interface, which defines the following methods:
Onaddinsupdate method: It occurs when an external program is loaded or uninstalled in the environment.
Onbeginshutdown method: the environment is being disabled.
Onconnection method: This occurs when an external program is loaded into the environment.
Ondisconnection method: This method occurs when an external program is detached from the environment.
Onstartupcomplete method: occurs when the environment is started.
The idtcommandtarget interface defines the following two methods:
Exec method: This method is called by the vs environment when an external menu command is selected in the vs development environment.
Querystatus method: This method is called to query the menu status when the vs environment displays an external menu.
This method returns the current status of the specified named command, whether enabled, disabled, or hidden.
2. onconnection () function:
This event processing function occurs when the plug-in is loaded and is generally used for initialization, such as creating menus. The input parameters of this function are as follows:
Object application: defines the IDE Automation Object
Extensibility. ext_connectmode connectmode: connection mode, indicating the current connection mode of the plug-in.
Ext_cm_afterstartup the external program is loaded after the application starts, or by setting the connect attribute of the corresponding addin object to true.
The ext_cm_startup external program is loaded at startup.
Ext_cm_uisetup is started for the first time after the external program is installed.
Object addininst: The addin object of the external program instance.
Ref system. array custom: A variant array that can be used to provide additional data.
3. ondisconnection () function: called when the system uninstalls the plug-in
This event processing function occurs when the plug-in is uninstalled. Its input parameters are as follows:
Extensibility. ext_disconnectmode disconnectmode:
Ext_dm_hostshutdown: the external program is uninstalled when the development environment is closed.
Ext_dm_userclosed: the external program is uninstalled when you clear the check box for this external program in the "external program manager" dialog box.
Ext_dm_uisetupcomplete: the external program is uninstalled after the environment is installed and after the onconnection method is returned.
Ref system. array custom:
4. querystatus () function: the system queries the menu status.
This method has four input parameters.
CommonNameThe name of the command to be checked.
Neededtext
A vscommandstatustextwanted constant that specifies whether to return the check information. If yes, it also specifies the type of the returned information.
Vscommandstatustextwantednone does not return information.
Vscommandstatustextwantedname.
Vscommandstatustextwantedstatus returns the command status.
Statusoption
The vscommandstatus constant of a specified command.
The vscommandstatusunsupported command is not supported in this context.
The vscommandstatussupported command is supported in this context.
The vscommandstatusenabled command is currently enabled.
The vscommandstatuslatched command is currently locked.
Vscommandstatusninched is reserved for future use.
The vscommandstatusinvisible command is currently hidden.
Commandtext
SpecifyVscommandstatustextwantedstatus.
5. Exec () function:
When an external menu command is selected in the vs development environment, it is called by the vs environment. Here, you can write your own response code, such as running your own program or popping up a window.
3. processing menu
You can perform a series of initialization work in the onconnect method, one of which is to generate a menu
1. Add menu bar and toolbar menu
Applicationobject = (_ DTE) application;
Addininstance = (addin) addininst;
If (connectmode = extensibility. ext_connectmode.ext_cm_uisetup
| Connectmode = extensibility. ext_connectmode.ext_cm_startup)
{// If the agent is in the installation status or has just been started, create a menu
Object [] contextguids = new object [] {};
// Obtain the command set and commandbar set of the IDE environment
Commands commands = applicationobject. commands;
_ Commandbars = applicationobject. commandbars;
Try
{
// The menu bar object and toolbar object are of the commandbar type.
Commandbar menuobj, toolbarobj;
// Generate a new sub-menu object, which will be inserted into the menu bar and toolbar object.
Command commandobj = commands. addnamedcommand (addininstance,
"Publishusermanage ",
"Add User Management Code ",
"Add User Management Code ",
True,
127,
Ref contextguids, (INT) vscommandstatus. vscommandstatussupported + (INT) vscommandstatus. vscommandstatusenabled
);
// Determine the ID of the bitmap displayed on the button. You can use the tool in the appendix.
# Region-related help information
/*
Returns a command object.
[C #]
Public command addnamedcommand (
Addin paddin, // addin object used to add new commands
String name, // abbreviated name of the new command. Addnamedcommand automatically adds the prefix to this abbreviation
// "Stdtools. Connect" in progid ("stdtools. Connect") to create a unique name
String buttontext, // The name used when the command is bound to a button with a name instead of an icon
String tooltip, // The text displayed when you hover the mouse pointer over any control bound to the new command
Bool msobutton, // indicates whether the button image of the specified command is an office image. True = button image from
// Obtain it from the office resource file. False indicates that the image resource of the button is from another file.
Int bitmap, // the ID of the bitmap displayed on the button
Object [] ppsacontextuiguids,
// Safearray of guid, which determines the environment context for enabling this command (that is, debug mode, design mode, and so on)
Int disableflags
// Make sure that the command is not visible or gray when contextuiguids is provided and none of them are active.
);
The current status of the disabeflags command.
The vscommandstatusunsupported 0 command is not supported in this context.
The vscommandstatussupported 1 command is supported in this context.
The vscommandstatusenabled 2 command is currently enabled.
The vscommandstatuslatched 4 command is currently locked.
Vscommandstatusninched 8 is reserved for future use.
The vscommandstatusinvisible 16 command is currently hidden.
Here are some notes:
1. Menu image:
The bool msobutton parameter is true, which means that the image of the menu command is from the office resource library, and the int Bitmap (Bitmap ID) next to it specifies the image. How do I know which ID corresponds to which image? ).
If you do not want to use the image resources that come with Microsoft Office, you must first add an mfc dll project in your solution, then add image resources to it, and then set msobutton to false, set the bitmap Bitmap bitmap ID to the ID of the image resource in the resource file, add a folder in the installer, and add the registry key. The specific process is no longer mentioned. You can refer to the reference materials.
2. Name of the menu item
String name (in this example, publishusermanage "), when a menu object is generated, the system automatically adds the string represented by progid of this plug-in before name as the full name of the menu object. The full name is required for retrieving the menu object by name. In this example, it should be "stdtools. Connect. publishusermanage ";
*/
# Endregion
// Add the main menu
Commandbarbutton buttonobj;
// Create a main menu item and toolbar
Menuobj = (commandbar) applicationobject. commands. addcommandbar ("code generation (& C )",
Vscommandbartype. vscommandbartypemenu,
Applicationobject. commandbars ["menubar"], 10 );
# Region-related help information
/*
Public object addcommandbar (
String name, // name of the new command bar
Vscommandbartype type, // used to determine the vscommandbartype constant of the command bar type
Commandbar commandbarparent, // The Office commandbar object to add to the new command bar
Int position // The index position of the new command column in the Command column, starting from 1
);
The value of vscommandbartype is:
Vscommandbartypepopup 10
Vscommandbartypetoolbar 23 toolbar command bar
Vscommandbartypemenu 24 menu command bar
*/
# Endregion-related help information
Toolbarobj = (commandbar) applicationobject. commands. addcommandbar (
"Codetools (& C )",
Vscommandbartype. vscommandbartypetoolbar,
Null,
-1 );
Toolbarobj. Position = Microsoft. Office. Core. msobarposition. msobartop;
// Add a sub-menu
// Add the sub-menu to the main menu and toolbar
Buttonobj = (commandbarbutton) commandobj. addcontrol (menuobj, menuobj. Controls. Count + 1 );
Buttonobj = (commandbarbutton) commandobj. addcontrol (toolbarobj, toolbarobj. Controls. Count + 1 );
Buttonobj. Style = msobuttonstyle. msobuttonicon;
// Right-click the sub-menu to add to the project menu
Commandbar projbar = This. applicationobject. commandbars ["project"];
Commandobj. addcontrol (projbar, 1 );
}
Catch (system. Exception ex)
{
String error = ex. message;
// MessageBox. Show (error );
}
2. Right-click the shortcut menu
In addition to the general menu added in the vs development environment above, you can also add some shortcut menu items for the development environment. The following code adds a shortcut menu for the code editing window.
// Retrieve the toolbar from the shortcut menu in the code editing window
Commandbar projbar = This. applicationobject. commandbars ["code window"];
// Add your menu items to the toolbar
Commandobj. addcontrol (projbar, 1 );
If you want to right-click a project in Solution Explorer and add your own menu items to the menu bar that appears, you only need to change the above "code window" to "project. There is no material to explain how it is "code window" or "project", but it is also very simple. Just write a plug-in program and list all menu bar objects. Fortunately, the pen has done this by listing the names of all the food orders in the commandbar_names.txt file in the Appendix. All you have to do is to guess which menu bar you need based on the name.
3. unmount the menu
Why do I need to unmount the menu?
If the uninstallation menu is not processed in your plug-in, the user temporarily closes your plug-in through "à external program manager, or when you uninstall your plug-in by adding/deleting programs, the menu item still exists, but the command cannot be executed, which is unreasonable.
Public void ondisconnection (extensibility. ext_disconnectmode disconnectmode,
Ref system. array custom)
{
/*
The name of the command to be checked.
Neededtext
A vscommandstatustextwanted constant that specifies whether the check information is returned,
If yes, the type of returned information is also specified.
Vscommandstatustextwantednone does not return information.
Vscommandstatustextwantedname.
Vscommandstatustextwantedstatus returns the command status.
Statusoption
The vscommandstatus constant of a specified command.
The vscommandstatusunsupported command is not supported in this context.
The vscommandstatussupported command is supported in this context.
The vscommandstatusenabled command is currently enabled.
The vscommandstatuslatched command is currently locked.
Vscommandstatusninched is reserved for future use.
The vscommandstatusinvisible command is currently hidden.
Commandtext
Specifies the text returned when vscommandstatustextwantedstatus is specified.
*/
Try
{
If (disconnectmode = extensibility. ext_disconnectmode.ext_dm_hostshutdown
| Disconnectmode = extensibility. ext_disconnectmode.ext_dm_userclosed)
{
Command commandobj = This. applicationobject. commands. Item (
"Stdtools. Connect. publishusermanage",-1 );
If (commandobj! = NULL)
Commandobj. Delete ();
Commandbar menuobj = (commandbar)
This. applicationobject. commandbars ["code generation (& C)"];
Commandbar toolbarobj = (commandbar)
This. applicationobject. commandbars ["codetools (& C)"];
If (menuobj! = NULL)
{
This. applicationobject. commands. removecommandbar (menuobj );
}
If (toolbarobj! = NULL)
{
This. applicationobject. commands. removecommandbar (toolbarobj );
}
}
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
}
The above code is relatively simple. First, check whether the vs environment is closed or the user suspends the plug-in through the external program, find and delete the sub-menu items, and then find the main menu items and toolbar items, remove these command entry objects from the system menu set.
4. Confirm the menu status.
A very important task in menu display is that the display status of menu objects is constantly changing depending on the application environment. Can also be used, disabled, not displayed. Whenever the vs Integration Environment displays a menu of the plug-in, it calls the querystatus () method to query the status that the menu should display. The following code is used:
Public void querystatus (string commandname,
Envdte. vscommandstatustextwanted neededtext,
Ref envdte. vscommandstatus status, ref object commandtext)
{
/*
Command name: name of the command to be checked.
Neededtext: A vscommandstatustextwanted constant that specifies whether the check information is returned,
If yes, the type of returned information is also specified.
Status: the constant of vscommandstatus in the current state of a specified command.
The vscommandstatusunsupported 0 command is not supported in this context.
The vscommandstatussupported 1 command is supported in this context.
The vscommandstatusenabled 2 command is currently enabled.
The vscommandstatuslatched 4 command is currently locked.
Vscommandstatusninched 8 is reserved for future use.
The vscommandstatusinvisible 16 command is currently hidden.
Commandtext: Specifies the text returned when vscommandstatustextwantedstatus is used.
*/
If (neededtext = envdte. vscommandstatustextwanted. vscommandstatustextwantednone)
{
If (commandname = "stdtools. Connect. publishusermanage ")
{
Status = (vscommandstatus) vscommandstatus. vscommandstatussupported;
If (// code for determining the status is omitted here ....)
{
Status =
(Vscommandstatus) vscommandstatus. vscommandstatussupported |
Vscommandstatus. vscommandstatusenabled;
}
}
}
}
The code above first sets the menu item status to supported (if not supported, the menu will not be displayed), and then adds the menu to the available status when conditions are met, if the conditions are not met, the menu is disabled.
5. Run the menu command
When you select a menu item of the plug-in, the integration environment will call the exec () method, as shown below:
Public void exec (string commandname,
Envdte. vscommandexecoption executeoption,
Ref object Varin, ref object varout, ref bool handled)
{
/*
Vscommandexecoption specifies the constant of the execution option.
Constant Value description
Vscommandexecoptiondodefault 0 executes the default action, regardless of whether the user input is prompted.
Vscommandexecoptionpromptuser 1 obtains user input and runs the command.
Vscommandexecoptiondopromptuser 2 does not prompt the user to execute the command.
For example, selecting the "print" button on the toolbar causes the document to be printed immediately without user input.
Vscommandexecoptionshowhelp 3 displays help for possible commands, but does not execute commands.
*/
Handled = false;
If (executeoption = envdte. vscommandexecoption. vscommandexecoptiondodefault)
{
If (commandname = "stdtools. Connect. publishusermanage ")
{
// Skip 200 words here... Pai_^
Handled = true;
Return;
}
}
}
The name of the menu to be executed in the Code query above is the name expected by the plug-in. Execute a piece of code, such as a display window. If the execution is successful, set handled to true, tells the integration environment that the command has been successfully executed.