Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 很棒的Visual Studio宏_Visual Studio_Visual Studio Macros - Fatal编程技术网

Visual studio 很棒的Visual Studio宏

Visual studio 很棒的Visual Studio宏,visual-studio,visual-studio-macros,Visual Studio,Visual Studio Macros,对于小型社区讨论,您使用哪些基本的Visual Studio宏 我刚开始了解它们,想听听你们中的一些人离不开什么。我将ctrl-shift-G映射到一个以注册表格式生成GUID的宏-这对编辑IDL很有用。我在VS2002/2003中使用过很多宏。一个例子是区域创建——我总是喜欢将我的类划分为以下区域——“私有成员”、“公共属性”、“公共方法”和“私有方法”。因此,我有一个映射到快捷键的宏,该快捷键在任何新类文件中创建这些区域 VS 2005/2008中的重构支持(以及添加通用代码段的功能)以及D

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


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

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

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


VS 2005/2008中的重构支持(以及添加通用代码段的功能)以及DXCore和SlickEdit等插件的使用使我能够在不必再创建太多宏的情况下工作

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

插入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文件的使用-原始作者:


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

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

我使用双监视器,我发现Sharon的布局切换宏(从1监视器到2监视器布局)非常宝贵。当您需要在键入代码时引用网页或其他程序时,请按Ctrl-Alt-1切换到Visual Studio窗口的单监视器布局。完成后,按Ctrl-Alt-2切换到双监视器布局,并恢复所有窗口。太棒了


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


编辑:更新链接

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

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

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

将此代码放入EnvironmentEvents模块:

Private Sub SolutionEvents_AfterClosing() Handles SolutionEvents.AfterClosing
    DTE.ExecuteCommand("View.StartPage")
End Sub
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

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

将此代码放入EnvironmentEvents模块:

Private Sub SolutionEvents_AfterClosing() Handles SolutionEvents.AfterClosing
    DTE.ExecuteCommand("View.StartPage")
End Sub
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


当您打开一个解决方案时,这两个选项一起将导致起始页隐藏。关闭解决方案时,将返回起始页。

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

你是否在一家坚持所有东西都有区域的商店工作,这样当你崩溃到定义时,你就看不到任何代码了

您真正需要的是一个折叠到定义但展开区域的宏,如下所示:

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
将其放入常规宏模块中,将其分配给热键,代码就会返回


(除非……如果您与一些真正邪恶的个人合作,他们将区域放入方法中,这将不幸地扩展这些方法。如果有人知道如何编写此方法来避免这种情况,请随意编辑。)

在输出窗口中显示生成持续时间

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

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

    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

宏本身不是宏,但很有用:

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

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

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

对于被接受的答案,这个选择真奇怪。我见过陌生人。将答案按日期排序,您将看到基本原理。;-)他没花太多时间,是吗?对我个人来说不是很有用,但为什么是反对票?你能解释一下他们应该做什么吗。。谁真正需要起始页?VS 2010在起始页上添加了“项目加载后关闭页面”选项,因此不再需要关闭宏。他们有“启动时显示页面”和“启动时显示页面”,但由于我不知道的奇怪原因,没有“解决方案关闭后显示页面”选项,这与“项目加载后关闭页面”相对应。谁需要起始页?我用它来锁定最近的项目。好吧,这是唯一一个可以确定最近项目/解决方案的地方(2010年)。您无法在“文件->最近的项目和解决方案”菜单中锁定项目,因此长期使用不可靠。我有类似的方法,但我想知道是否有一种方法可以使运行总数保持不变,这样我就可以跟踪我一生中花在编译上的时间。当然,只需将时间跨度存储在数据库中即可。据我所知,在宏中几乎可以运行任何代码。所以,只需编写代码来存储最新的时间跨度,然后检索总时间并显示出来。派对有点晚了,但我想感谢你,因为这正是我想要的。旁注:您可以使用
ow.OutputWindowPanes.Item(“Build”).OutputString(message)
而不是每个
。由于某些原因,这对我不起作用,我是否必须以某种方式启用对EnvironmentEvents的调用?@IanRingrose,您使用的是哪一版本的Visual Studio?宏在VS 2012中被删除。如果宏无法运行,可以尝试创建一个