真棒视觉工作室macros

对于小型社区讨论,您使用的是一些基本的Visual Studiomacros?

我刚开始了解他们,想听听你们有些人不能没有。

我曾经在VS 2002/2003中使用过很多macros。 一个例子就是区域创build – 我总是喜欢将我的class级分成以下几个区域 – “私人成员”,“公共属性”,“公共方法”和“私人方法”。 所以,我有一个macros映射到一个快捷键,在任何新的类文件中创build这些区域。

VS 2005/2008中的重构支持(以及添加通用代码片段的function)以及像DXCore和SlickEdit这样的Addins的使用使我无需再创build太多的macros就可以工作。

我在工具栏上为以下3个macros添加button。 每个将采取任何文件中的当前选定的文本和谷歌(或MSDN的,或拼写检查)。 为工具栏创build一个漂亮的图标,以获得更多的样式点。

 Private Const BROWSER_PATH As String = "C:\Program Files\Mozilla Firefox\firefox.exe" Sub SearchGoogle() Dim cmd As String cmd = String.Format("{0} http://www.google.com/search?hl-en&q={1}", BROWSER_PATH, DTE.ActiveDocument.Selection.Text) Shell(cmd, AppWinStyle.NormalFocus) End Sub Sub SearchMSDN() Dim cmd As String cmd = String.Format("{0} http://www.google.com/search?hl-en&q={1}+site%3Amsdn.microsoft.com", BROWSER_PATH, DTE.ActiveDocument.Selection.Text) Shell(cmd, AppWinStyle.NormalFocus) End Sub Sub SpellCheck() Dim cmd As String cmd = String.Format("{0} http://www.spellcheck.net/cgi-bin/spell.exe?action=CHECKWORD&string={1}", BROWSER_PATH, DTE.ActiveDocument.Selection.Text) Shell(cmd, AppWinStyle.NormalFocus) End Sub 

在“输出”窗口中显示构build时间

把这个代码放到你的EnvironmentEvents模块中。 这会将持续时间直接写入构build窗口,以便解决任何操作(构build,重build,清理,部署)。

您可以更改IsBuild函数以指定要查看此信息的操作。

 Dim buildStart As Date Private Function IsBuild(ByVal scope As EnvDTE.vsBuildScope, ByVal action As EnvDTE.vsBuildAction) As Boolean Return scope = vsBuildScope.vsBuildScopeSolution End Function Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin If (IsBuild(Scope, Action)) Then buildStart = Date.Now End If End Sub Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone If (IsBuild(Scope, Action)) Then Dim buildTime = Date.Now - buildStart WriteToBuildWindow(String.Format("Build time: {0}", buildTime.ToString)) End If End Sub Private Sub WriteToBuildWindow(ByVal message As String) Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) Dim ow As OutputWindow = CType(win.Object, OutputWindow) For Each owPane As OutputWindowPane In ow.OutputWindowPanes If (owPane.Name.Equals("Build")) Then owPane.OutputString(message) Exit For End If Next End Sub 

在closures解决scheme后显示开始页面(但保持Visual Studio打开)

把这段代码放到你的EnvironmentEvents模块中:

 Private Sub SolutionEvents_AfterClosing() Handles SolutionEvents.AfterClosing DTE.ExecuteCommand("View.StartPage") End Sub 

打开解决scheme后隐藏起始页面

把这段代码放到你的EnvironmentEvents模块中:

 Private Sub SolutionEvents_Opened() Handles SolutionEvents.Opened Dim startPageGuid As String = "{387CB18D-6153-4156-9257-9AC3F9207BBE}" Dim startPage As EnvDTE.Window = DTE.Windows.Item(startPageGuid) If startPage IsNot Nothing Then startPage.Close() End Sub 

当你打开一个解决scheme时,这两者一起会导致你的起始页面隐藏起来。 当您closures解决scheme时,“开始页面”返回。

我经常使用以下鲜为人知的快捷方式:

  • Ctrl + Enter :在当前行上方插入一个空白行(并将光标放在那里)
  • Ctrl + Shift + Enter :在当前行的下面插入一个空白行(并将光标放在那里)
  • Ctrl + Shift + V :循环剪贴板环

大纲:折叠定义,但扩大地区

你是否在其中一个坚持每个地区的商店工作,所以当你崩溃的定义,你不能看到任何代码?

你真正需要的是一个崩溃到定义,但扩大区域的macros,像这样的一个:

 Sub CollapseToDefinitionsButExpandAllRegions() DTE.ExecuteCommand("Edit.CollapsetoDefinitions") DTE.SuppressUI = True Dim objSelection As TextSelection = DTE.ActiveDocument.Selection objSelection.StartOfDocument() Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText) Loop objSelection.StartOfDocument() DTE.SuppressUI = False End Sub 

把它放在一个常规的macros模块中,把它分配给一个热键,你的代码就回来了。

(除了……如果你和一些把区域放在方法中的非常邪恶的个人一起工作,这将不幸地扩展这些方法,如果有人知道这种方法来避免这种情况,可以自由编辑。

插入GUID,伟大的WiX工作,添加到菜单作为button或键快捷键。

 Sub InsertGuid() Dim objTextSelection As TextSelection objTextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection) objTextSelection.Text = System.Guid.NewGuid.ToString.ToUpper(New System.Globalization.CultureInfo("en", False)) End Sub 

在解决scheme中组织所有.cs文件的使用 – 原作者: djpark 。

 Sub OrganizeSolution() Dim sol As Solution = DTE.Solution For i As Integer = 1 To sol.Projects.Count OrganizeProject(sol.Projects.Item(i)) Next End Sub Private Sub OrganizeProject(ByVal proj As Project) For i As Integer = 1 To proj.ProjectItems.Count OrganizeProjectItem(proj.ProjectItems.Item(i)) Next End Sub Private Sub OrganizeProjectItem(ByVal projectItem As ProjectItem) Dim fileIsOpen As Boolean = False If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then 'If this is ac# file If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then 'Set flag to true if file is already open fileIsOpen = projectItem.IsOpen Dim window As Window = projectItem.Open(Constants.vsViewKindCode) window.Activate() projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort") 'Only close the file if it was not already open If Not fileIsOpen Then window.Close(vsSaveChanges.vsSaveChangesYes) End If End If End If 'Be sure to apply RemoveAndSort on all of the ProjectItems. If Not projectItem.ProjectItems Is Nothing Then For i As Integer = 1 To projectItem.ProjectItems.Count OrganizeProjectItem(projectItem.ProjectItems.Item(i)) Next End If 'Apply RemoveAndSort on a SubProject if it exists. If Not projectItem.SubProject Is Nothing Then OrganizeProject(projectItem.SubProject) End If End Sub 

折叠 “解决scheme”面板的所有节点 ,对于大型项目非常有用:

  Public Module CollapseAllNodes Sub RunCollapseAllNodes() Dim UIHSolutionExplorer As UIHierarchy UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object() ' Check if there is any open solution If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then Return End If ' Get the top node (the name of the solution) Dim UIHSolutionRootNode As UIHierarchyItem UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1) CloseRecursif(UIHSolutionRootNode) ' Select the solution node, or else when you click ' on the solution windows scrollbar, it will synchronize the open document ' with the tree and pop out the corresponding node which is probably not ' what you want. UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect) End Sub Function CloseRecursif(ByRef element) For Each UIHChild In element.UIHierarchyItems() CloseRecursif(UIHChild) If (UIHChild.UIHierarchyItems.Expanded = True) Then UIHChild.UIHierarchyItems.Expanded = False End If Next End Function End Module 

如果我要将代码示例粘贴到博客文章或电子邮件中,我使用Jeff的FormatToHtmlmacros。

我使用双显示器,我发现Sharon的布局切换macros(从1显示器到2显示器布局)是非常宝贵的。 如果您需要在input一些代码的同时引用网页或其他程序,请按Ctrl-Alt-1切换到Visual Studio窗口的一个显示器布局。 一旦你完成,Ctrl-Alt-2切换到你的两个显示器布局,并获得所有的窗户。 真棒!

http://www.invisible-city.com/sharon/2008/06/workstation-hack-visual-studio-on-2.html

不是一个macros,而是有用的:

 Public Sub WriteToOutputWindow(ByVal pane as String, ByVal Msg As String) Dim owPane As OutputWindowPane Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) Dim ow As OutputWindow = win.Object Try owPane = ow.OutputWindowPanes.Item(pane) Catch owPane = ow.OutputWindowPanes.Add(pane) End Try If Not owPane Is Nothing Then owPane.Activate() owPane.OutputString(Msg & vbCrLf) End If End Sub 

我将ctrl-shift-G映射到一个以registry格式生成GUID的macros – 这对编辑IDL很有用

我目前正在使用不同编码标准的两个不同项目,一个使用制表符开头,另一个使用空格。 这个macros将根据哪个环境当前处于活动状态来切换使用哪个标准:

 Public Sub ToggleTabs() If DTE.ActiveDocument.Language = "CSharp" Then Dim currentSetting As Boolean = DTE.Properties("TextEditor", "CSharp").Item("InsertTabs").Value DTE.Properties("TextEditor", "CSharp").Item("InsertTabs").Value = Not currentSetting End If If DTE.ActiveDocument.Language = "SQL" Then Dim currentSQLSetting As Boolean = DTE.Properties("TextEditor", "SQL").Item("InsertTabs").Value DTE.Properties("TextEditor", "SQL").Item("InsertTabs").Value = Not currentSQLSetting End If If DTE.ActiveDocument.Language = "HTML" Then Dim currentHTMLSetting As Boolean = DTE.Properties("TextEditor", "HTML").Item("InsertTabs").Value DTE.Properties("TextEditor", "HTML").Item("InsertTabs").Value = Not currentHTMLSetting End If If DTE.ActiveDocument.Language = "JScript" Then Dim currentJScriptSetting As Boolean = DTE.Properties("TextEditor", "JScript").Item("InsertTabs").Value DTE.Properties("TextEditor", "JScript").Item("InsertTabs").Value = Not currentJScriptSetting End If End Sub 

我不能不提这个问题就走这个问题。 它甚至有一个video来展示如何安装和使用它。 此macros只允许您在解决scheme资源pipe理器中创build嵌套文件(如resources.resx)。

编辑:更新链接