当前位置:  开发笔记 > 编程语言 > 正文

令人敬畏的Visual Studio宏

如何解决《令人敬畏的VisualStudio宏》经验,为你挑选了7个好方法。

对于小型社区讨论,您使用的Visual Studio宏是什么?

我刚刚开始了解它们,并希望听到你们中的一些人不能没有.



1> Aardvark..:

我在工具栏上为以下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



2> Ryan Lundy..:

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

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


我有类似的东西,但我想知道是否有办法让一个持久的运行总数,所以我可以跟踪我的生活中我花了多少时间编译.

3> Ryan Lundy..:

关闭解决方案后显示起始页(但保持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


当您打开解决方案时,这两者将导致您的起始页隐藏起来.关闭解决方案后,"起始页面"将返回.


谁真的需要开始页面?
VS 2010在起始页面上添加了"项目加载后关闭页面"选项,因此不再需要关闭宏.他们有这个和"启动时显示页面",但由于我不知道的奇怪的原因,没有"解决方案关闭后显示页面"选项,这是"项目加载后关闭页面"的匹配对应.谁需要首页?我用它来固定最近的项目.AFAIK,它是您可以确定最近项目/解决方案的唯一地方(2010年).您无法将项目固定在文件 - >最近的项目和解决方案菜单中,因此长期使用它不可靠.

4> Sam Harwell..:

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

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

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

Ctrl + Shift + V:循环剪贴板环


好像是穷人的VIM

5> Ryan Lundy..:

概述:折叠到定义但扩展区域

你是否在那些坚持所有地区的商店工作,这样当你崩溃到定义时,你看不到任何代码?

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

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

将它放在常规宏模块中,将其分配给热键,然后返回代码.

(除非...如果你与一些非常邪恶的人合作,他们将区域放在方法中,不幸的是会扩展这些方法.如果有人知道写这个的方法来避免这种情况,请随意编辑.)



6> si618..:

插入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



7> Olivier Paye..:

折叠 "解决方案"面板的所有节点,对于大型项目非常有用:

    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

推荐阅读
虎仔球妈_459
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有