52.5. Develop an Add-In

A common step in building an add-in is developing it. Many common steps of the add-in creation process are automated by the Add-in Wizard, Visual Studio templates, and other tools, but you still need to write your own programming code to get what you desire from an add-in.

This development process depends on your requirements and your programming language and programming knowledge, but overall the development process is similar for all programming languages and add-ins.

As in other development projects, you can use classes, modules, methods, and other programming concepts to write an add-in, but the important point is the right usage of add-in methods to make them work. There are five methods (related to five events) in the add-in lifecycle that you need to apply in order to start and activate your add-in. There are also two methods for command handling and showing user interface elements.

To get started with the development process for an add-in, you can look at the following quick example covering the main aspects of add-in development. In this example you learn how to develop an add-in that lets you copy a piece of selected text in an editor to a clipboard.

After creating the add-in project and checking the checkboxes to include Tools menu items, you have an initial generated code to get started with. To write such an add-in you need to handle the command associated with the add-in in the Exec method in order to copy the selected text to the clipboard.

The following code is an updated version of the generated code you saw earlier to add this functionality. Notice the highlighted code:

using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Windows.Forms;

namespace Chapter52
{
    public class Connect : IDTExtensibility2, IDTCommandTarget
    {
        public Connect()
        {
        }

        public void OnConnection(object application, ext_ConnectMode connectMode,
            object addInInst, ref Array custom)
        {    // This method hasn't been modified.    }

        public void OnDisconnection(ext_DisconnectMode disconnectMode,
ref Array custom) {        }

        public void OnAddInsUpdate(ref Array custom){        }

        public void OnStartupComplete(ref Array custom){        }

        public void OnBeginShutdown(ref Array custom){        }

        public void QueryStatus(string commandName, vsCommandStatusTextWanted
neededText,
            ref vsCommandStatus status, ref object commandText)
        {    // This method hasn't been modified.    }

        public void Exec(string commandName, vsCommandExecOption executeOption,
            ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                if (commandName == "Chapter52.Connect.Chapter52")
                {
                    CopyToClipboard();

                    handled = true;
                    return;
                }
            }
        }

private void CopyToClipboard()
        {
            TextSelection selection = this._applicationObject.ActiveDocument.
Selection as TextSelection;

            Clipboard.SetText(selection.Text);
        }


        private DTE2 _applicationObject;
        private AddIn _addInInstance;
    }
}

Note that there is a new method call added to the Exec method in order to copy the selected text to the clipboard. In the CopyToClipboard method you see a very simple code that uses the TextSelection class in the code model (which is a smaller model in the automation model) to get access to the selected text in the editor. There isn't enough space here to discuss this object and other objects in the automation model, but the code should be self-explanatory.

Now you can open a document in the Visual Studio editor (whether it be a text file, code file, or any other material) and select a text like what you see in Figure 52-11.

Figure 52.11. Figure 52-11

If you run the add-in, your selected text should be copied to the clipboard and you can paste it somewhere to see the result. Figure 52-12 shows the selected text that is pasted from the clipboard to Notepad.

Figure 52.12. Figure 52-12

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.249.220