Awesome Visual Studio Macros [closed]

2020-01-27 09:12发布

For a small community discussion, what are some essential Visual Studio macros you use?

I just started learning about them, and want to hear what some of you can't live without.

14条回答
我欲成王,谁敢阻挡
2楼-- · 2020-01-27 09:55

I work with dual monitors, and I find Sharon's layout-switching macro (from a 1 monitor to a 2 monitor layout) totally invaluable. When you need to be referencing a web page or other program while typing a bit of code, Ctrl-Alt-1 to switch to a one monitor layout for your Visual Studio windows. Once you're done, Ctrl-Alt-2 to switch to your two monitor layout and get all your windows back. Awesome!

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

查看更多
再贱就再见
3楼-- · 2020-01-27 09:55

I add buttons on the toolbar for the following 3 macros. Each will take the currently selected text in any file and google it (or MSDN-it, or spell-check-it). Make up a nifty icon for the toolbar for extra style-points.

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
查看更多
We Are One
4楼-- · 2020-01-27 09:56

Show the start page after you close a solution (but keep Visual Studio open)

Put this code in your EnvironmentEvents module:

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


Hide the start page after you open a solution

Put this code in your EnvironmentEvents module:

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


These two together will cause your Start Page to hide itself when you open a solution. When you close the solution, the Start Page comes back.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-01-27 10:01

Collapse all nodes of the Solution panel, very useful especially for big projects:

    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
查看更多
\"骚年 ilove
6楼-- · 2020-01-27 10:02

I use the following lesser-known shortcuts very often:

  • Ctrl+Enter: Insert a blank line above the current line (and place the cursor there)
  • Ctrl+Shift+Enter: Insert a blank line below the current line (and place the cursor there)
  • Ctrl+Shift+V: Cycles the clipboard ring
查看更多
We Are One
7楼-- · 2020-01-27 10:02

I couldn't let this question go without mentioning this one. It even has a video to show how to install and use it. This macro simply allows you to create the nested files in the solution explorer (like resources.resx).

Edit: Updated the link

查看更多
登录 后发表回答