67

对于小型社区讨论,您使用哪些基本的 Visual Studio 宏?

我刚开始了解它们,并想听听你们中的一些人离不开什么。

4

14 回答 14

31

我在工具栏上为以下 3 个宏添加了按钮。每个都将在任何文件中获取当前选定的文本并用谷歌搜索它(或 MSDN-it,或拼写检查)。为工具栏制作一个漂亮的图标以获得额外的样式点。

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
于 2009-03-12T20:00:36.387 回答
25

在“输出”窗口中显示构建持续时间

将此代码放入您的 EnvironmentEvents 模块中。这会将持续时间直接写入构建窗口,以便对解决方案执行任何操作(构建、重建、清理、部署)。

您可以更改 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
于 2010-01-21T19:26:13.793 回答
14

关闭解决方案后显示起始页(但保持 Visual Studio 打开)

将此代码放入您的 EnvironmentEvents 模块中:

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


打开解决方案后隐藏起始页

将此代码放入您的 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


当您打开解决方案时,这两者一起将导致您的起始页隐藏自己。当您关闭解决方案时,开始页面会返回。

于 2009-10-09T20:24:08.820 回答
13

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

  • Ctrl+Enter:在当前行上方插入一个空行(并将光标放在那里)
  • Ctrl+Shift+Enter:在当前行下方插入一个空行(并将光标放在那里)
  • Ctrl+Shift+V:循环剪贴板环
于 2009-07-28T09:26:49.023 回答
9

大纲:折叠到定义但扩展区域

您是否在那些坚持围绕所有内容的区域的商店之一工作,因此当您折叠到定义时,您看不到任何代码?

你真正需要的是一个collapse-to-definitions-but-expand-regions宏,就像这个:

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

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

(除了......如果您与一些将区域放入方法中的非常邪恶的人一起工作,不幸的是,这将扩展这些方法。如果有人知道如何编写这个来避免这种情况,请随时编辑。)

于 2009-10-09T20:29:40.413 回答
8

插入 GUID,非常适合 WiX 工作,作为按钮或快捷键添加到菜单。

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

组织解决方案中所有 .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 a c# 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
于 2009-03-12T19:45:22.727 回答
5

折叠解决方案面板的所有节点,特别适用于大型项目:

    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
于 2010-04-14T15:31:24.077 回答
4

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

于 2009-02-12T22:15:50.810 回答
4

我使用双显示器,我发现 Sharon 的布局切换宏(从 1 显示器到 2 显示器布局)非常宝贵。当您需要在键入一些代码时引用网页或其他程序时,Ctrl-Alt-1 可切换到 Visual Studio 窗口的单显示器布局。完成后,按 Ctrl-Alt-2 切换到两个显示器布局并恢复所有窗口。惊人的!

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

于 2009-03-12T20:10:25.430 回答
2

本身不是宏,但很有用:

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
于 2010-10-13T12:05:51.843 回答
1

我将 ctrl-shift-G 映射到以注册表格式生成 GUID 的宏 - 这对于编辑 IDL 很有用

于 2009-02-07T05:50:07.497 回答
1

我目前正在从事两个具有不同编码标准的不同项目,一个使用制表符作为行首,另一个使用空格。此宏将根据当前活动的环境在使用的标准之间切换:

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
于 2011-01-05T16:04:05.020 回答
0

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

VS 2005/2008 中的重构支持(以及添加通用代码片段的功能)以及使用 DXCore 和 SlickEdit 等插件让我无需再创建太多宏即可工作。

于 2009-02-07T06:22:11.850 回答
0

我不能不提这个问题就离开这个问题。它甚至有一个视频来展示如何安装和使用它。此宏仅允许您在解决方案资源管理器中创建嵌套文件(如 resources.resx)。

编辑:更新了链接

于 2009-03-12T20:17:22.103 回答