Recently, I used C # To write a vs plug-in. The main function is to insert standard comment segments and some common code segments. During the development process, I encountered some problems, read some materials, and did some research. Here we will make a simple record for some of the minor issues, hoping to help.
(1) When determining connectMode in OnConnection, you must add ext_cm_AfterStartup.
If (connectMode = Extensibility. ext_ConnectMode.ext_cm_UISetup
| ConnectMode = Extensibility. ext_ConnectMode.ext_cm_Startup
| ConnectMode = Extensibility. ext_ConnectMode.ext_cm_AfterStartup) // this line will work when u choose addin in addin manager
In this way, the plug-in will be re-displayed only when the plug-in is selected in The Addin Manager of vs. In the general example, only the first two judgments are available.
(2) In QueryState, when setting state, use the following statement
If (the command you added)
{
If (the display conditions are met)
Status = (vsCommandStatus) vsCommandStatus. vsCommandStatusSupported | vsCommandStatus. vsCommandStatusEnabled;
Else
Status = (vsCommandStatus) vsCommandStatus. vsCommandStatusSupported;
}
Else
Status = (vsCommandStatus) vsCommandStatus. vsCommandStatusUnsupported;
In this way, the plug-in menu becomes grayed out when the conditions are not met.
(3) the method for judging the existence of the code window is
(ApplicationObject. ActiveWindow! = Null) & (applicationObject. ActiveWindow. Type = vsWindowType. vsWindowTypeDocument)
That is to say, there is an active window and its type is document type.
(4) how to insert characters in the document window is
TextSelection ts = (TextSelection) applicationObject. ActiveDocument. Selection;
EditPoint ep = ts. ActivePoint. CreateEditPoint ();
Ep. Insert (strCode );
Of course, you can also call other EditPoint methods to delete, replace, and so on.
It's almost as simple as using C # As a plug-in program. At the same time, I feel that the object model designed by Microsoft is really comfortable to use. If you can design such a good system during development, how nice it is, haha