Tabbars code interpretation-automated interfaces of Visual Studio

Source: Internet
Author: User

Microsoft's Visual Studio development tools provide powerful functions for software developers. by integrating the menus and toolbar provided by the Development Platform (IDE), developers can generate and compile projects.Code, Debugging until the application is finally generatedProgram. In addition, Visual Studio also provides a complete set of automated objects, through the methods provided by operating these automated objects, developers can also perform operations such as opening a file, activating a window, or changing the window size without using menus and toolbar. Visual Studio provides two methods for users to control the integrated development environment and its automation component objects. One is to use macro (macros ), the other is to use the built-in plug-in (add-ins ). This article mainly introduces the embedded plug-in (add-ins.
Each Automation Object represents a component or a group of related components in the integrated development environment. For example, a document represents an open document, and a document object represents all opened documents. Similarly, a window object represents an opened window, and a Windows Object represents all opened windows. You can open a project without using the built-in plug-in. Modify the files in the project or even build the project again. Using the built-in plug-in, you can also add practical commands to the IDE and create buttons on the toolbar to extend the IDE functions.
The idsaddin interface is a plug-in interface defined by Visual Studio. Visual Studio only defines this interface. A built-in plug-in is an implementation of an idsaddin interface. That is to say, a built-in plug-in is a COM object, it should implement at least one idsaddin interface. The core of the built-in plug-in code generated using the add-ins Wizard of VC is a cdsaddin class:

Class cdsaddin: Public idsaddin,
Public ccomobjectroot, public ccomcoclass <cdsaddin, & clsid_dsaddin <

The idsaddin interface actually has only two methods:

Idsaddin: onconnection plug-in loaded
Idsaddin: The ondisconnection plug-in is uninstalled.

The plug-in works like this. When the IDE starts a plug-in, it calls the onconnection implemented by the plug-in coclass according to the idsaddin interface and passes an application parameter to the plug-in, the onconnection of the plug-in completes the plug-in initialization. You can use this application object to query other automation objects supported by IDE and use these objects to complete specific tasks. A typical task of a built-in plug-in is to register a command with the commands object of the system and create a button on the toolbar. When the system needs to uninstall a built-in plug-in, it will call the corresponding ondisconnection. The plug-in should use this opportunity to release the applied system resources and deregister the registered commands from the commands object of the system, delete the buttons added to the toolbar. From this process, we can see that although the plug-in is loaded and released passively, the plug-in can complete custom initialization and destruction tasks by implementing the custom onconnection and ondisconnection interface functions, this is like the callback process of a COM event. From the interface function name, we can also see that some callback terminals, the interface function names all start with "on, this is usually the declaration of event interfaces in the com system. Figure (1) shows the relationship between the built-in plug-in, the idsaddin interface, and the Implementation class:


Figure 1. Relationship between plug-in components, idsaddin interfaces, and implementation classes

Visual Studio ide passes an important parameter to the plug-in through the idsaddin: onconnection interface function. This parameter is an application object. The Application Object corresponds to the entire Visual Studio ide instance. Simply put, an application is an open VC integrated development environment. The application object is the core component of the entire Visual Studio built-in component. Through the application object, you can query all the built-in objects of the entire Visual Studio IDE and control Visual Studio through these built-in objects. The following code demonstrates opening a VC workspace without a vc ide and executing the rebuild all command to compile all projects in the Workspace:

// Pseudo code, cannot be compiled directly
# Import ".../devshl. dll"

Iapplicationptr PAPP;
PAPP. Createobject (_ T ("msdev. application "));
If (PAPP! = NULL)
{
PAPP-<rebuildall ();
}

The built-in plug-in obtains an application object in the current integrated development environment through the idsaddin: onconnection interface function. All subsequent work of the plug-in is centered on this application object, cdsaddin is responsible for saving this application object. The built-in COM object interface of Visual Studio is a complex interface model. Figure (2) shows the relationship diagram of these built-in interfaces:

Figure 2. Visual Studio's built-in COM object interface Diagram

The following uses part of the code that describes the function of automatically adding a Formatting Function to tabbars as an example to demonstrate how to insert text content in the current document through the Application Object:

Ccomptr <idispatch <pdispdoc;
Ccomqiptr <itextdocument, & iid_itextdocument <pdoc;
// M_papplication is an application object saved by cdsaddin.
M_papplication-<get_activedocument (& pdispdoc );
Pdoc = pdispdoc; // implicit interface Query
Pdispdoc = NULL; // release it

If (pdoc)
{
Ccomptr <idispatch <pdispsel;
Ccomqiptr <itextselection, & iid_itextselection <psel;
Ccombstr BSTR;
Cstring strtext;

Pdoc-<get_selection (& pdispsel );
Psel = pdispsel; // query the itextselection object
Pdispsel = NULL;
HR = psel-<get_text (& BSTR); // obtain the selected text
If (succeeded (HR ))
{
Strtext = BSTR;
Strtext. Replace (_ T ("/t"), _ T (""); // replace/T with a space
BSTR = strtext;
Psel-<put_text (BSTR); // write the document
}
}

After learning about the automated interfaces of Visual Studio and how to use these interfaces, you can get a general idea about how tabbars works. The subsequent code explanation will focus on how each tabbars function is implemented, this section describes the usage of the built-in COM object interface in Visual Studio.

Related Article

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.