- oriented. NET Programmer vs. extensions

Source: Internet
Author: User
Tags visual studio 2010

This article is divided into 2 parts first for custom multi-project templates second for vs add-in Development

Article All vs version is 2010

1. Custom templates

2. Tools menu

3. Windows

4. Engineering

5. Documents

...

I. Multi-project templates

Single item template It's simple. Select an item select an export template in the file column

Then select the project template

The last wizard will give you the output path, which is generally the user documentation path for the system +\visual Studio 2010\my exported Templates

Your corresponding project template compression package will be generated in the corresponding directory.

We generate template files for 2 projects and then make a multi-project template

We unzipped 2 template files and put them into a new folder named Maoyatemplates, cut to the visual Studio 2010\templates\projecttemplates path

New Template file MyTemplate.vstemplate

Modify a file based on the template content you want to define

Then compress the entire folder into a zip file to define some of the information you need, such as icon definition your template icon to place the icon file in the relative path TemplateData

Then reopen vs to see the template you just made

2 demo projects that are defined after opening are fairly simple

So you don't have to copy the code every time you come to a new project.

Two vs Plugins

Conceptual stuff can refer to http://msdn.microsoft.com/zh-cn/library/bb384200.aspx

Direct on Dry Goods

The VS plugin can help or optimize your most windows in the VS development process, which can be handled differently for the project against the window or even for different suffixes.

Below graphic + code introduction development steps (VS2010)

Select plug-in project into the wizard according to your own scene selection can

Intermediate Wizard process Skip last step

After completing the wizard, the default engineering framework is as follows

The key is the Connect.cs file.

Connect inherits 2 classes of IDTExtensibility2, IDTCommandTarget

IDTExtensibility2 contains the method that is used as an event when implementing an interface. Visual Studio calls these methods whenever an event that affects an add-in occurs, such as when the add-in is loaded or unloaded, and when any changes are made to the add-in.

The IDTCommandTarget interface enables developers to implement named commands in the environment. and to define the command state or execute the command.

Description of each method

The Chinese version can refer to MSDN

Http://msdn.microsoft.com/zh-cn/library/extensibility.idtextensibility2.aspx

Http://msdn.microsoft.com/zh-cn/library/envdte.idtcommandtarget.aspx

One of the things we need to focus on as an introductory development is

OnConnection is the Main method for IDTExtensibility2, because the method is called every time the add-in is loaded. This method is the entry point for the add-in at load time, so you can place any code you want to run at the time the addin starts (or call any other function).

void OnConnection (Object application,ext_connectmode connectmode,object addininst,ref Array Custom)

Parameters
Application
Type: System.Object
A reference to an instance of the Integrated development Environment (IDE) (DTE), which is the root object of the Visual Studio automation model.
ConnectMode
Type: Extensibility.ext_connectmode
A Ext_connectmode enumeration value that indicates how the add-in is loaded into Visual Studio.
addInInst
Type: System.Object
An AddIn reference to the external handler's own instance. This reference is stored for later use, such as the parent collection used to determine the add-in.
Custom
Type: System.Array
An empty array that you can use to pass host-specific data that is used in the add-in.

Let's start by building a menu under Toolbars (Tools)

First, determine the loading mode

if (ConnectMode = = Ext_connectmode.ext_cm_uisetup | | connectmode = EXT_CONNECTMODE.EXT_CM_STARTUP | |                ConnectMode = = Ext_connectmode.ext_cm_afterstartup)

The connectmode here comes from an enumeration reference MSDN Http://msdn.microsoft.com/zh-cn/library/extensibility.ext_connectmode.aspx

Then add the Register Plugin command and window key code:

                    var toolsbar = Commandbars[tools]; if (Toolsbar! = null) {Toolssubpopup = (CommandBarPopup) toolsBar.Controls.Add (M                        Socontroltype.msocontrolpopup, Type.Missing, Type.Missing, 1, true);                        Toolssubpopup.caption = Mainmenuname;                        CommandBar Toolssubbar = Toolssubpopup.commandbar; command command = commands.                            AddNamedCommand2 (_addInInstance, MenuName1,                            MenuName1, MenuName1, True, 190, Ref contextguids, (int) vscommandstatus.vscommandstatusunsupported + (int                        ) vscommandstatus.vscommandstatusenabled); Command.                        AddControl (Toolssubbar); Command = commands.                           AddNamedCommand2 ( _addInInstance, MenuName2, MenuName2, Me                            NuName2, True, contextGUIDS, ref.                        (int) vscommandstatus.vscommandstatusunsupported + (int) vscommandstatus.vscommandstatusenabled); Command.                    AddControl (Toolssubbar, 2); }

Commandbars[tools] represents the toolbar in the command bar, where tools are actually defined as the string "Tools".

CommandBarPopup indicates a popup command bar.

_addInInstance represents the current plug-in instance.

3 Menuname represents the abbreviated form of the command name, the name of the UI display, and the text that appears when the user hovers over any control that is bound to the new command.

  True indicates that the following is the ID of the Microsoft Office bitmap.

ContextUIGUIDs represents the GUID that determines which environment context (that is, debug mode, design mode, and so on) displays the command.

Vscommandstatusvalue determines whether the disable condition of the command is not visible or disabled when you provide a contextuiguids parameter and is not currently active.

AddNamedCommand2 creates a named command, which is saved by the environment and is available the next time the environment starts, whether or not the add-in is loaded. The add-in can later change the ButtonText name by responding to the QueryStatus method. If the text begins with #, the remainder of the string is an integer that represents the resource ID in the satellite DLL that the add-in has registered. There are two default order states: The default value enables the status and default visibility states. These default states are important if the command handler cannot be found (because the element is not loaded or does not implement IDTCommandTarget). If your component loads and implements IDTCommandTarget, the default value does not apply.

Command.addcontrol creates a persistence command bar control for this command. The number that follows indicates the menu position.

Detailed reference http://msdn.microsoft.com/zh-cn/library/envdte80.commands2.addnamedcommand2.aspx

This basically completes the effect shown.

But we need a functional development of 2 menus.

  The QueryStatus method returns the current state of the specified named command (enabled, disabled, hidden, and so on).

Add a fragment in this method

  if (Neededtext = = Vscommandstatustextwanted.vscommandstatustextwantednone)            {                if (commandName = = Getcommandname (mainmenuname))                {                    status = vscommandstatus.vscommandstatussupported | vscommandstatus.vscommandstatusenabled;                    return;                } }

Vscommandstatussupported indicates that the command is supported in this context, and vscommandstatusenabled indicates that the command is currently enabled.

  The Exec method executes the specified named command.

            if (executeoption = = Vscommandexecoption.vscommandexecoptiondodefault)            {                if (CommandName = = Getcommandname ( Mainmenuname))                {                    MessageBox.Show ("This is Maoya test");                    handled = TRUE;                    return;                }                if (CommandName = = Getcommandname (MenuName1))                {                    MessageBox.Show ("This is Maoya Test1 test");                    handled = TRUE;                    return;                }                if (CommandName = = Getcommandname (MenuName2))                {                    MessageBox.Show ("This is Maoya Test2 test");                    handled = TRUE;                    return;                }            }

The simple implementation here is to pop up a WinForm. You can do a lot of functions here.

The above is the implementation of the toolbar, the following is a description of the operation of the window

Onconnection:

                    CommandBar Mdicommandbar = commandbars["Easy MDI Document window"];                    if (Mdicommandbar! = null)                    {                        Command mdicmd = commands. AddNamedCommand2 (_addInInstance, MenuName3,                                                                            MenuName3,                                                                            MenuName3, False, 0,                                                                            ref contextguids);                        Mdicmd.addcontrol (Mdicommandbar);                    }

QueryStatus:

            if (CommandName = = Getcommandname (MenuName3) | | commandName = = getcommandname (MenuName4) | | commandName = getcommandname (MENUNAME5))            {                status = vscommandstatus.vscommandstatussupported | vscommandstatus.vscommandstatusenabled;                return;            }

The Exec function is to create a new file

            if (CommandName = = Getcommandname (MenuName3))            {                _applicationobject.executecommand ("File.NewFile", String. Empty);                handled = TRUE;                return;            }

  

Now we're doing a custom button for project engineering.

OnConnection:

 CommandBar Projectbar = commandbars["Project"]; if (Projectbar! = null) {Projectsubpopup = (CommandBarPopup) projectbar.controls .                        ADD (Msocontroltype.msocontrolpopup, Type.Missing, Type.Missing, 1, true);                        Projectsubpopup.caption = Mainmenuname;                        CommandBar Projectsubbar = Projectsubpopup.commandbar; Command Projectcommand = commands.                                                   AddNamedCommand2 (_addInInstance,                                                   MenuName4, MenuName4,                                                   MenuName4, True,                                                   contextGUIDS, ref. (int) Vscommandstatus.vscommandstatusunsupported + (int) vscommandstatus.vscommandstatusenabled); if (Projectsubbar! = null) {Projectcommand.addcontrol (Projectsubbar, 1)                        ; }                    }

QueryStatus:

            if (CommandName = = Getcommandname (MenuName3) | | commandName = = getcommandname (MenuName4) | | commandName = getcommandname (MENUNAME5))            {                status = vscommandstatus.vscommandstatussupported | vscommandstatus.vscommandstatusenabled;                return;            }

Exec here can catch you choose which project includes the name path and so on according to the specific requirements to deal with the corresponding logic

  if (CommandName = = Getcommandname (MenuName4))            {                MessageBox.Show (string. Format ("This is Maoya {0} test,/r/n the project name is {1}", MenuName4, _applicationobject.selecteditems.item (1). Name));                handled = TRUE;                return;            }

  

  

Similar to the operation of the file, the code is not posted directly

Worth mentioning you can use the file suffix and so on to do some special operations

In the QueryStatus method, add

            if (CommandName = = Getcommandname (MenuName6))            {                var uiHierarchy = (UiHierarchy) _ ApplicationObject.Windows.Item (                        constants.vswindowkindsolutionexplorer). Object;                if (from UIHierarchyItem item in (Array) uihierarchy.selecteditems Select item. Name). Any (ItemName = Itemname.indexof (". config") >-1))                {                    status = vscommandstatus.vscommandstatusenabled | vscommandstatus.vscommandstatussupported;                }            }

  

Summing up the template of VS, plug-in extension and so on can bring great convenience to the developers, for example, you can do code generator or directly to DLL files to call some anti-compilation software without cutting out and re-open the window to find files and so on can be very small

Hope to be helpful to everyone

- oriented. NET Programmer vs. extensions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.