The recent use of idle time to learn the VSPackage, so the idea of extending a template code generation tool in the IDE has sprouted. Here are some notes from the study.
I. Extension Server Explorer table right-click menu button
To extend a right-click menu in the IDE's Server Explorer It was a very painful thing to do, but I succeeded in extending the menu I wanted, with the effect of:
Although it is difficult, but this blog "VS2013 in the right-click menu Add Command plug-in development" gave me great help, blog post title Three: "How to get the target menu GUID and ID value" In the introduction is the key to solve the problem. According to the method in this article, I made the Server Resource Management table right-click menu Guid:{d4f02a6a-c5ae-4bf2-938d-f1625bdca0e2}. Such as:
With this GUID everything will be solved, of course, during the time to put the custom menu in this GUID corresponding to the menu or encountered a lot of trouble. But needless to mention, the following is the VSCT configuration for the extended right-click menu:
<?xml version= "1.0" encoding= "Utf-8"? ><commandtable xmlns= "http://schemas.microsoft.com/VisualStudio/ 2005-10-18/commandtable "xmlns:xs=" Http://www.w3.org/2001/XMLSchema "> <extern href=" stdidcmd.h "/> < Extern href= "Vsshlids.h"/> <commands package= "guidmyvspackagepkg" > <Groups> <!--<group guid= "Guidmyvspackagecmdset" id= "Mytablerightclickmenumastergroup" priority= "0x0600" > <parent guid= "guidSHLMainMe Nu "id=" Idm_vs_menu_tools "/> </Group>--> <!--Server Explorer table right-click Custom group, which is independent of the other groups in the table's right-click menu and is displayed at the top of the menu- Lt Group guid= "Guidmyvspackagecmdset" id= "Mytablerightclickmenumastergroup" priority= "> <!--<parent guid= "Guideditorrightclickmenucmdset" id= "Editorrightclickmenu"/>--><!--Solution Explorer file right-click menu, <!--< Parent guid= "Guidshlmainmenu" id= "Idm_vs_ctxt_itemnode"/>--> <!--the group is located in the Server Explorer table right-click menu--&L T Parent guid= "Guidserverexplorermenucmdset" Id= "Serverexplorertablerightclickmenu"/> </Group> <!--groups in the object Model menu--<group guid= "G Uidmytablerightclickmenu "id=" Mytablerightclickmenugroup "priority=" 0x0600 "> <!--the group is located in the object Model menu-- <parent guid= "Guidmytablerightclickmenu" id= "Mytablerightclickmenu"/> </Group> </Groups> <Buttons> <button guid= "Guidmyvspackagecmdset" id= "Cmdidmycommand" priority= "0x0100" type= "button" > <!--the button is in the Object Model menu group, it's worth noting that no submenu or button is displayed in the custom menu--<parent guid= "Guidmytablerightclickmenu" id= "mytabl Erightclickmenugroup "/> <icon guid=" guidimages "id=" BmpPic1 "/> <Strings> <butto Ntext> template code Generation </ButtonText> </Strings> </Button> </Buttons> <Menus> <!--Server Explorer table custom Right-click Menu--<menu guid= "Guidmytablerightclickmenu" id= "Mytablerightclickmenu" priority= "0x70 0 "type=" menu "> <!--the menu is located in the Server Explorer table right-click Custom group, and the custom menu or command does not appear to be detached from the group-<parent guid= "Guidmyvspackagecmdset" id= "Mytableri Ghtclickmenumastergroup "/> <Strings> <ButtonText> object Model </ButtonText> <comm Andname> Object Model </CommandName> </Strings> </Menu> </Menus> <Bitmaps> & Lt Bitmap guid= "Guidimages" href= "Resources\images.png" usedlist= "BmpPic1, BmpPic2, Bmppicsearch, Bmppicx, Bmppicarrows" /> </Bitmaps> </Commands> <Symbols> <!--This is the package GUID. --<guidsymbol name= "guidmyvspackagepkg" value= "{27b31d67-797a-4b04-98da-b66c930919b8}"/> <!--{ 74d21310-2aee-11d1-8bfb-00a0c90f26f7}1283 Server Explorer Right-click Menu--<!--{d309f791-903f-11d0-9efc-00a0c911004f} 1072 Solution Explorer file right-click menu-<!--Server Explorer table right-click menu, which is found in the registry--<guidsymbol Name= " Guidserverexplorermenucmdset "value=" {d4f02a6a-c5ae-4bf2-938d-f1625bdca0e2} "> <idsymbol naMe= "Serverexplorertablerightclickmenu" value= "33280"/> </GuidSymbol> <!--This was the GUID used to Group the menu commands together---<guidsymbol name= "Guidmyvspackagecmdset" value= "{fc41e6d9-9414-4cda-92a8-0d 2f39215247} "> <!--Server Explorer table right-click menu Custom Group-<idsymbol name=" Mytablerightclickmenumastergroup "value=" 0x10 "/> <!--definition" template code Generation "button--<idsymbol name=" Cmdidmycommand "value=" 0x0100 "/> </GUIDSYMBOL&G T <!--extend its menu in the Server Explorer table right-click menu, where the GUID is freely generated--<guidsymbol name= "Guidmytablerightclickmenu" value= "{ 87325143-3929-42b9-a6c2-9b61dc72879b} "> <!--" Object Model "menu--<idsymbol name=" Mytablerightclickmenu "value = "0x1000"/> <!--"Object Model" menu group--<idsymbol name= "Mytablerightclickmenugroup" value= "0x1001"/> &L T;/guidsymbol> <!--code Editor right-click Edit menu-<guidsymbol name= "Guideditorrightclickmenucmdset" value= "{ d309f791-903f-11d0-9efc-00a0c911004f} "> <idsymbol name= "Editorrightclickmenu" value= "1037"/> </GuidSymbol> <guidsymbol name= "GuidI Mages "value=" {67d162b3-9cbc-4e1f-b6b1-48ac6206b50b} "> <idsymbol name=" BmpPic1 "value=" 1 "/> <idsym Bol name= "BmpPic2" value= "2"/> <idsymbol name= "Bmppicsearch" value= "3"/> <idsymbol name= "Bmppicx" Value= "4"/> <idsymbol name= "bmppicarrows" value= "5"/> <idsymbol name= "Bmppicstrikethrough" value= " 6 "/> </GuidSymbol> </Symbols></CommandTable>
In this configuration file, I have removed the original automatically generated comments so that it should look like a bit of conditioning. It is estimated that the novice may be a bit blindfolded when seeing this profile, and the following explanation of the configuration file:
First look at the symbols node:
Look at the commands node again:
Can be seen, the commands node is the concrete implementation of symbols, this place is so detailed because I am in touch with this time confused, although there are many explanations on the Internet, but are not simple and direct, so it took so much space to introduce, I hope that the latter has helped. At this point, the Server Explorer table right-click menu (The table right-click menu) has been successfully expanded, followed by a more painful egg.
To get the table name that is selected when the table right-click menu pops up
To connect to the database in the table context menu, the first thing to do is to get the selected table name. In view of the extension table right-click menu This idea may be more wonderful, so this aspect of the introduction of the Web less, let alone to get the right-click menu popup When the table name is selected. Baidu found only the introduction of the MSDN inside, helpless under, can only go directly to the MSDN inside the search. Consider a snippet of code in an auto-generated template instance:
DTE DTE = (DTE) GetService (typeof (DTE));
So I assume that all extensions should be expanded from DTE? This opens the API EnvDTE namespace under the DTE namespace and begins to look blindly because of the instance code:
Ivsuishell Uishell = (Ivsuishell) GetService (typeof (Svsuishell));
(This code has a lot of appearances in the blog post), the target I'm looking for should belong to the interface, thus narrowing the search. Finally EnvDTE interface is about to be my browsing, I found the following:
The crowd found him 1100 degrees, the original hierarchy structure UIHierarchy, decisive point to see, the content is as follows:
Then the next is good to do, get UIHierarchy code is written by VB:
Dim Uih as UIHierarchy = _dte. Windows.item (Constants.vswindowkindmacroexplorer). Object
It is easy to convert this code into C #, as follows:
DTE DTE = (DTE) GetService (typeof (DTE)); var Serverexplorar = DTE. Windows.item (EnvDTE.Constants.vsWindowKindServerExplorer). Object as UIHierarchy;
From another place, it is easier to get the hierarchy of Server Explorer:
var dte2 = (envdte80.dte2) System.Runtime.InteropServices.Marshal.GetActiveObject ("visualstudio.dte.11.0"); var Serverexplorar = Dte2. Toolwindows.gettoolwindow ("Server Explorer") as UIHierarchy;
So in the "template Code Generation" button event (how to bind an extended button event, read another article or refer to the automatically generated code sample), get the code snippet for the right-click-selected table name in the following:
DTE DTE = (DTE) GetService (typeof (DTE)); var Serverexplorar = DTE. Windows.item (EnvDTE.Constants.vsWindowKindServerExplorer). Object as Uihierarchy;var SelectedItems = Serverexplorar.selecteditems as Object[];var Selecteduih = selectedItems[0] as U Ihierarchyitem;
Iii. getting the database connection information configured in Server Explorer
Well, the table name also knows, how to connect the database? Read from the IDE global configuration file? The configuration file was not found for a long time in the IDE installation directory. Is that not the IDE itself allows me to read the data connection string already configured in Server Explorer or even the database connection is connection? Based on this idea again in the MSDN again and again, how to find the key code is not verbose, as described in front of the article is almost. In the case of arduous hardships and Wan Chi, find the Key interface "Ivsdataexplorerconnectionmanager", The interface is in the namespace Microsoft.VisualStudio.Data.Services, and can be referenced in the Add referenced assembly-extension, as shown in the following code snippet:
var conectionservice = (Ivsdataexplorerconnectionmanager) GetService (typeof (Ivsdataexplorerconnectionmanager));
The other GetService method can be directly called directly under the namespace of the button binding event, not to be too concerned.
Conectionservice, you have all the database configuration information, database connections, and so on. In this place want to leave a suspense, if the Server Explorer configured multiple database information, how to get the table right-click menu selected table corresponding database information?
In my blog post, I do not like to directly put the complete code out, just hope that through the way to guide the later, so that the latter can more easily solve the problem, expect to read a friend understanding.
Extending the Server Explorer table right-click menu and connecting to the database through the VSPackage in VS2012