對VS2003插件進行深度研究
雖然有許多人對VS 2003插件的安全性表示懷疑,但在年復一年的不斷發展中,他的安全性也在不斷提高。保障VS 2003插件的安全性是完全有可能的,但前提是要深入理解到底什么是VS 2003插件,及他是怎么運作的。 #t#
選擇"新建"->"項目"->"其他項目"->"Visual Studio.Net外接程序"創建一個C#的外接項目。默認地,會生成一個Connet.cs文件,該文件就是當你的Visual Studio 啟動時加載的東西:好了,那么具體應該怎么寫代碼呢,看幾個代碼片斷吧:
- object []contextGUIDS = new object[] { };
- // 獲得Visual Studio的Commands
- Commands commands = applicationObject.Commands;
- // 獲得Visual Studio的菜單
- _CommandBars commandBars = applicationObject.CommandBars;
- try
- {
- // 獲得Visual Studio的"工具"菜單
- CommandBar toolsBar = (CommandBar)commandBars["Tools"];
- foreach(CommandBarControl control in toolsBar.Controls)
- {
- // 如果VSX菜單已經存在就直接返回
- if(control.Caption == "VSX")
- {
- return;
- }
- }
- // 添加VSX菜單到工具欄下面
- CommandBar vsxBar = (CommandBar)commands.AddCommandBar("VSX",vsCommandBarType.vsCommandBarTypeMenu,toolsBar,1);
- // 添加OutLook到VSX菜單下面
- Command command = commands.AddNamedCommand(addInInstance,"OutLook","打開OutLook","在VS中嵌入OutLook",true,50,ref contextGUIDS,(int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);
- command.AddControl(vsxBar,1);
- }
- catch(Exception ex)
- {
- System.Windows.Forms.MessageBox.Show(ex.ToString());
- }
看看VS 2003插件注釋就知道干了些什么。首先獲取"工具"菜單,然后在"工具"上面加了個"VSX", ***在VSX上面加了個"OutLook". F5Debug一下,會彈出Visual Studio 2003,VS 2003插件看到工具下面的VSX沒有?不過還沒有任何功能。這里要注意的是:command.AddControl(vsxBar,1);就將command命令和vsxBar綁定在一起了。