【问题标题】:How can you sort tabs in Visual Studio 2008 by file type?如何按文件类型对 Visual Studio 2008 中的选项卡进行排序?
【发布时间】:2011-09-23 03:42:13
【问题描述】:

我一直在尝试(运气不佳)为 Visual Studio 2008 编写一个宏,它将打开的 .cpp 和 .h 文件分类到两个选项卡组中,将标题放在左侧选项卡组中,将 .cpp 文件放在右侧。我很想拥有这个功能,因为我花了很多时间来移动我的标签,这样我就可以看到我正在学习的课程的两个部分。我知道有一个用于 Visual Studio 的非免费插件,它允许标签管理,但它与我需要用于工作的插件冲突,因此到目前为止,宏是我的最佳选择。

我确信如果我可以让它工作,这也可以应用于其他布局和排序需求。我的想法是在每次打开文档窗口时自动进行排序,因此我在 Visual Studio 宏 IDE 的环境事件部分创建了一个 Visual Basic 宏。以下是我到目前为止的代码:

Public Sub keepHeaderAndCxxInDifferentTabs() Handles WindowEvents.WindowCreated
     Dim openedFile As String
     openedFile = ActiveWindow.Document.FullName

     If openedFile.Contains(".h") Then
         ' if the file being opened is a header file make sure it appears on the left
         If DTE.ActiveDocument.ActiveWindow.Left > 600 Then
             DTE.ExecuteCommand("Window.MovetoNextTabGroup")
         End If
     ElseIf openedFile.Contains(".cpp") Then
         ' if the file being opened is a cpp file make sure it appears on the right
         If DTE.ActiveDocument.ActiveWindow.Left < 250 Then
             DTE.ExecuteCommand("Window.MovetoNextTabGroup")
         End If
     Else
         ' if the file being opened is anything else make sure it appears on the right
         If DTE.ActiveDocument.ActiveWindow.Left < 250 Then
             DTE.ExecuteCommand("Window.MovetoNextTabGroup")
         End If
     End If
 End Sub

遗憾的是,这个宏目前没有做任何事情。我最好的猜测是,我可以使用活动窗口的“左”属性来确定新窗口出现在哪个选项卡组中,然后将窗口移动到下一个选项卡组(如果它不是我想要的位置)。我已经为“左”属性尝试了一系列不同的值,但到目前为止我的标签都没有移动。

有人知道我做错了什么或有什么建议吗?提前感谢您的宝贵时间。

【问题讨论】:

  • 我找到了一种使排序工作的方法,但只能通过手动单击按钮。只要我在运行宏时在 Visual Studio 中设置了两个垂直选项卡组,我编写的方法就会完全符合我的要求。它将所有头文件放在左侧,将其他任何内容放在右侧。这是代码:

标签: visual-studio sorting macros tabs


【解决方案1】:

我找到了一种方法来使用下面的函数来做我想做的事。只要我在运行宏时设置了两个垂直选项卡,它就会将所有标题放在左侧选项卡组中,并将所有其他文件放在右侧选项卡组中。这可以进一步扩展,以便当我使用任何其他宏打开任何文件时,我编写它也会通过在宏运行后调用它来对它们进行排序。遗憾的是,这不会自动工作,每当触发特定事件(使用环境事件部分)时,我无法让它实际执行排序。

'=====================================================================
' Sorts all opened documents putting headers into the left tab group
' and everything else into the right tab group
'===================================================================== 
Public Sub SortFilesInTabs()
    For i = 1 To DTE.Windows.Count Step 1
        If DTE.Windows.Item(i).Document IsNot Nothing Then
            If DTE.Windows.Item(i).Document.FullName.Contains(".h") Then
                ' if the file is a header file make sure it appears on the left
                If DTE.Windows.Item(i).Document.ActiveWindow.Left > 600 Then
                    WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to left")
                    DTE.Windows.Item(i).Document.Activate()
                    DTE.ExecuteCommand("Window.MovetoPreviousTabGroup")
                End If
            ElseIf DTE.Windows.Item(i).Document.FullName.Contains(".cpp") Then
                ' if the file is a cpp file make sure it appears on the right
                If DTE.Windows.Item(i).Document.ActiveWindow.Left < 250 Then
                    WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to right")
                    DTE.Windows.Item(i).Document.Activate()
                    DTE.ExecuteCommand("Window.MovetoNextTabGroup")
                End If
            ElseIf DTE.Windows.Item(i).Document.FullName.Length > 0 Then
                ' if the file is any other valid document then make sure it appears on the right
                If DTE.Windows.Item(i).Document.ActiveWindow.Left < 250 Then
                    WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to right")
                    DTE.Windows.Item(i).Document.Activate()
                    DTE.ExecuteCommand("Window.MovetoNextTabGroup")
                End If
            End If
        End If
    Next i
End Sub

如果有人可以进一步改进这一点

【讨论】:

    猜你喜欢
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多