Author: Hao. Yu
Download the sample code in this article
Recently I spent some time learning how to write add-in for Visual C ++ 6.0. This is an interesting question, but in general, documents and sample programs in this regard are still relatively lacking (Chinese is more rare ). Therefore, I decided to write out some of my learning experiences and share them with you.
I. FAQs about visual C ++ add-in
(1) What is Visual C ++ add-in? What is its purpose?
In general, Visual C ++ add-in is a component object that implements certain specific COM interfaces. It can be embedded in the developer studio integrated development environment and provides some commands for automated tasks, or simplify the programming process.
From the programmer's point of view, a visual C ++ add-in is basically a COM object that implements the idsaddin interface. With this interface, add-in can take charge of the developer studio environment and execute specific tasks.
(2) how to compile add-in?
The simplest way is to start visual c ++ Appwizard and select devstudio add-in Wizard from the project type, as shown in.
(3) Where can I find the add-in programming materials and documents?
The most complete documentation is in msdn, the location is msdn Library/Visual Studio documentation/using Visual C ++/Visual C ++ User's Guide/automating tasks in Visual C ++/add-ins for Visual C +. + developer studio. Msdn of different versions may have slight differences in specific paths.
(4) How to Use add-in?
If you get a useful add-in or write one yourself, install it as follows:
Select Tools | customize from the main menu of Visual C ++ and go to the add-ins and macro files page:
If your add-in does not appear in the list, press the Browse button to find the add-in file (note that the default file type (*. change DSM (*. DLL )):
Later, add-in will appear in the add-ins and macros list. Confirm that the check box on the left of add-in is selected, and then press close.
Return to the integration environment. In general, add-in adds a new toolbar in the environment, which lists the available commands for add-in. Now you can use these command buttons to work.
(5) What are the advantages and disadvantages of add-in?
Add-in is integrated with the development environment, which means that we can use add-in without leaving the IDE to complete the work without using external tools, this is a major advantage of add-in. Therefore, add-in is usually used to simplify repetitive work and improve the efficiency of programmers. For example, an add-in named autobuildnumber can automatically add the version number to 1 during each project compilation; another famous add-in named wndtabs (it is estimated that the central readers are already using it) provides a page similar to ultra-edit, this allows programmers to quickly switch between opened files.
Add-in also has its disadvantages, which are manifested in several aspects. First, if you want to simplify repetitive work, you don't have to write add-in: You can first consider writing macro scripts ). Like add-in, a script has the ability to access the complete developer studio object model, and it is easier to write and maintain, so it is also a good choice. However, scripts cannot implement advanced functions, such as calling Win32 APIs. You should consider adding-in.
Another disadvantage of add-in is that it is restricted by interfaces provided by the development environment. Microsoft provides a set of interfaces for add-in. You need to use the attributes and methods of these interfaces to complete actual tasks. If some features are not provided by interfaces, then it is difficult for you to implement them in add-in. To understand what add-in can do and what it cannot do, the best way is to familiarize yourself with the developer studio object model. For this reason, refer to msdn. In addition, add-in cannot (or is difficult) implement advanced UI functions. For example, the user interface implemented by add-in is basically only a modal dialog box; it is almost impossible to create a window and associate it with IDE. Add-in such as wndtabs implements some special techniques to break through this restriction, but it is more or less used by some hacker means, which is not officially supported, that is to say, as long as Microsoft makes a small change to Visual C ++, it may be totally ineffective. In addition, it is very difficult to find these skills by yourself. Therefore, this approach is generally not recommended.
Ii. Example programs in this article
Now, let's take a look at a specific example based on the add-in document. This example implements four useful methods. I will introduce them one by one.
(1) show Object Model
This method is the product of learning documents, because when I read msdn, I always want to know what the object model provided by the add-in specification should look like. With this method, When you select this command, it will call up a dialog box where all objects in the model and their respective attributes are listed according to the hierarchy.
As you can see, the top layer of the model is an application object, which is named Microsoft developer studio and version 6.0. Other attributes can also be viewed intuitively. For the relationship between these objects and them, refer to msdn.
When writing add-in, I found that this method is also very useful, because it can intuitively tell me what objects are currently available and the attributes of objects are available. In this way, I have saved a lot of time to switch between Visual C ++ and msdn.
(2) switch between. h and. cpp
A friend who has used Borland C ++ builder should be familiar with this method. For example, if test. cpp is opened, the corresponding test. h can be viewed immediately with this command, and vice versa. Visual c ++ does not implement similar functions, so I wrote a method to simulate C ++ builder.
(3) Open RC as text
Sometimes we need to open the project resource file for some text editing. In some cases, this is more convenient and convenient than using resource editor, and we can also avoid the restrictions of the integration environment. However, Visual C ++ does not have a simple way to do this, so every time I have to close all the resource windows, use the open command to browse the RC file, select the open mode as text, it takes several steps to complete. Why is it not automated? The open RC as text command implements this function.
(4) include Browser
This command can quickly open the header file in the Visual C ++ include environment variable, avoiding the trouble of manual search. It has a very considerate function. when too many files are listed, enter the first few characters of the file you want to view, and the list will automatically filter out the files that meet the criteria, you do not need to flip back and forth in a long list. For example, if you want to view the header file definition related to Windows Socket, you only need to select this command and enter "Winsock", so the list will list the files that meet the conditions, select a file and press "open" to open it, as shown in.
There is nothing special about the program code, so I don't want to explain them either. If you don't understand anything, refer to the source code. Even so, the Code still provides some interesting things, such as the template functions that simplify the query interface attributes and methods, the use of the ccomdispatchdriver object, the embedded toolbar in the dialog box, and the custom painting (custom draw) list control, registry query functions, and so on. Many of these methods can be reused or used for reference. For details, refer to the relevant code.
Iii. Description and conclusion of procedures
Because this program is still a draft, if you are interested in add-in programming, it provides a good starting point; even if you do not want to write it yourself, the features listed above may also be helpful for your daily programming tasks. Add-in programming is an interesting field. Here you can share the same fate with your integrated development environment on a daily basis, and in Visual Studio.. NET 2002 and Visual Studio. in NET 2003, this model is constantly changing and increasing. The example in this article is based on Visual C ++ 6.0, but it still has reference value in subsequent versions. For more information about add-in, see several famous visual c ++ programming sites, such as msdn online, codeguru, and codeproject, I believe that you will be able to improve your ability to write add-in.