2011-09-23 66 views
1

我一直在努力(沒有太多的運氣)爲Visual Studio 2008編寫一個宏,它將打開的.cpp和.h文件分類到兩個標籤組中,將標題放在左邊的標籤組和右邊的.cpp文件中。我很想擁有這個功能,因爲我花了很多時間移動我的標籤,這樣我就可以看到我正在使用的兩個部分。我知道Visual Studio有一個非自由插件允許選項卡管理,但它與我需要用於工作的插件衝突,使得宏成爲迄今爲止最好的選擇。如何按文件類型對Visual Studio 2008中的選項卡進行排序?

我相信這可以應用於其他佈局和排序需求,以及如果我能得到它的工作。我的想法是每次打開文檔窗口時都會自動進行排序,因此我在Visual Studio Macro IDE的環境事件部分創建了一個可視化基本宏。下面是我到目前爲止的代碼:

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 

不幸的是,這個宏目前沒有做任何事情。我最好的猜測是我可以使用活動窗口的'Left'屬性來確定新窗口出現在哪個標籤組中,然後將窗口移到下一個標籤組,如果它不在我想要的位置。我已經爲「左」屬性嘗試了一系列不同的值,但到目前爲止我的選項卡都沒有移動。

有誰知道我做錯了什麼或有什麼建議嗎?預先感謝您的時間。

+0

我發現了一個方法,使分揀作業,但只能通過手動點擊一個按鈕。只要在運行宏時我在Visual Studio中設置了兩個垂直選項卡組,我寫的方法就會完成我想要的操作。它把所有的頭文件放在左邊,其他的放在右邊。代碼如下: – user960341

回答

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 

如果任何人都可以改善這種進一步PL

相關問題