memo‎ > ‎

VS Macro

Visual Studio マクロ
操作系
 書き方の例 解   説
 DTE.Windows.Item("result_codes.h").Activate() result_codes.h をアクティブ化する
 DTE.ActiveWindow.Object.GetItem("chrome\(chrome)\common\common\
resource_response.h").Select(vsUISelectionType.vsUISelectionTypeSelect)
 ソリューションエクスプローラ上の resource_response.h を選択状態にする
 DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate() ソリューションエクスプローラをアクティブ化する
 DTE.ActiveWindow.Object.GetItem("chrome\(base)\base").UIHierarchyItems.Expanded = Trueソリューションエクスプローラ上の base を展開する
 DTE.ActiveWindow.Object.GetItem("chrome\(base)\base").UIHierarchyItems.Expanded = False ソリューションエクスプローラ上の base の展開を閉じる
 DTE.Solution.FindProjectItem("result_codes.h").ExpandView() ソリューションエクスプローラ上の result_codes.h を選択状態にする
 DTE.Solution.FindProjectItem("result_codes.h").Open() ソリューションエクスプローラ上の result_codes.h をアクティブ化する
 DTE.Solution.FindProjectItem(DTE.ActiveDocument.Name).ExpandView() 現在のアクティブウィンドウのファイルをソリューションエクスプローラ上で選択状態にする 。
 DTE.ExecuteCommand("View.StartPage") スタートページを開く

文字列系
 書き方の例     解   説
 str = DTE.ActiveDocument.Name アクティブドキュメントのファイル名を取得する。result_codes.hなど
 MsgBox(str) 文字列をメッセージボックスで表示する。定番コード
 Dim ts As TextSelection = DTE.ActiveDocument.Selection
 If Not ts.IsEmpty Then
   System.Diagnostics.Process.Start("http://www.google.co.jp/search?sourceid=chrome&ie=UTF-8&q=" + ts.Text)
 End If
 選択中のテキストをデフォルトブラウザでGoogle検索する。
 ちょっと外道っぽい?Ctrl+0, F1 でショートカットキーに登録するといいかも

・作成したマクロを実行するにはマクロエクスプローラからマクロをダブルクリックすれば良い。
・ツールバーに設定したい場合は[ツール]>[ユーザー設定]のコマンドで分類でマクロをクリックし、コマンド内からマクロをドラッグする。
・ショートカットを設定したい場合は[ツール]>[オプション]>[環境]>[キーボード]をクリックし、「以下の文字列を含むコマンドを表示」にマクロ名を入力し選択し、ショートカットキーテキストボックスをクリックしキー入力(Ctrl+0, Ctrl+Eなど)し、割り当てボタンをクリックする。Ctrl+0 で組み合わせることを推奨
・EnvDTE.Project プロパティはソリューションフォルダも代入できる。
  Name FullName UniqueName
 プロジェクト プロジェクト名 絶対パス 相対パス
 ソリューションフォルダ フォルダ名 空白("") フォルダ名{GUID}


' VS2010版 Utility ファイル
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module Utility
    'アウトプットペインに文字列を出力する
    Sub WriteOutputpane(ByVal paneName As String, ByVal text As String)
        Dim output As OutputWindowPane
        Try
            output = DTE.ToolWindows.OutputWindow.OutputWindowPanes.Item(paneName)
        Catch ex As Exception
            output = DTE.ToolWindows.OutputWindow.OutputWindowPanes.Add(paneName)
        End Try

        output.OutputString(text)
    End Sub
    Sub WriteOutputpane(ByVal text As String)
        WriteOutputpane("My Macro Default", text)
    End Sub
    Sub WriteOutputpane(ByVal paneName As OutputWindowPane, ByVal text As String)
        paneName.OutputString(text)
    End Sub
    'アウトプットペインに改行付きで文字列を出力する
    Sub WriteLineOutputpane(ByVal paneName As String, ByVal text As String)
        WriteOutputpane(paneName, text + vbCrLf)
    End Sub
    Sub WriteLineOutputpane(ByVal text As String)
        WriteOutputpane("My Macro Default", text + vbCrLf)
    End Sub
    Sub WriteLineOutputpane(ByVal paneName As OutputWindowPane, ByVal text As String)
        paneName.OutputString(text + vbCrLf)
    End Sub

    Sub WriteLineOutputpane()
        WriteOutputpane(vbCrLf)
    End Sub

    Sub ClearOutputpane(ByVal paneName As String)
        Dim output As OutputWindowPane
        output = DTE.ToolWindows.OutputWindow.OutputWindowPanes.Item(paneName)
        output.Clear()
    End Sub

    'インクルードガードを書き込む
    Sub WriteIncludeGuard()
        If IsTextWindow() Then Return

        Dim name As String
        name = DTE.ActiveDocument.Name
        name = name.ToUpper()
        name = name.Replace(".", "_")
        name = name + "_"
        DTE.ActiveDocument.Selection.Text = _
                        "#ifndef " + name + NewLine() + _
                        "#define " + name + NewLine() + _
                        NewLine(3) + _
                        "#endif // " + name + NewLine()
    End Sub

    'namespace を書き込む
    Sub WriteNamespace()
        If IsTextWindow() Then Return

        DTE.ActiveDocument.Selection.Text = "namespace  {" + NewLine(3) + "}  // namespace "
    End Sub

    'インクルードガードと名前空間を書き込む
    Sub WriteIncludeGuardAndNamespace()
        If IsTextWindow() Then Return

        Dim name As String
        name = DTE.ActiveDocument.Name
        name = name.ToUpper()
        name = name.Replace(".", "_")
        name = name + "_"
        DTE.ActiveDocument.Selection.Text = _
                        "#ifndef " + name + NewLine() + _
                        "#define " + name + NewLine() + _
                        NewLine() + _
                        "namespace  {" + NewLine() + _
                        NewLine(3) + _
                        "}  // namespace " + NewLine() + _
                        NewLine() + _
                        "#endif // " + name + NewLine()
    End Sub

    Sub WriteIncludeAndNamespace()
        If IsTextWindow() Then Return

        DTE.ActiveDocument.Selection.Text = "#include """ + HeaderName(DTE.ActiveDocument.Name) + """" + NewLine(2)
        WriteNamespace()
    End Sub

    'ソリューションをアクティブ化する
    Sub ActiveSolution()
        DTE.Windows.Item("ソリューション エクスプローラー").Activate()
    End Sub

    'ウィンドウがテキストウィンドウか調べる
    Function IsTextWindow() As Boolean
        IsTextWindow(DTE.ActiveWindow)
    End Function
    Function IsTextWindow(ByVal win As EnvDTE.Window) As Boolean
        If TypeOf win.Object Is TextWindow Then
            IsTextWindow = True
        Else
            IsTextWindow = False
        End If
    End Function

    '改行文字列を返す
    Function NewLine() As String
        Return Environment.NewLine
    End Function
    Function NewLine(ByVal number As Integer) As String
        Dim s As String = ""
        For i As Integer = 1 To number
            s += Environment.NewLine
        Next
        Return s
    End Function

    'ソースファイル名(.cpp)をもとにヘッダファイル名(.h)を返す
    Function HeaderName(ByVal SourceName As String) As String
        Return SourceName.Replace(".cpp", ".h")
    End Function

    '
    Sub 一時()
        MsgBox("")
    End Sub
End Module

' VS2010版 公開 ファイル
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module 公開

    'インクルードガードを書き込む
    Sub インクルードガード()
        Utility.WriteIncludeGuard()
    End Sub

    'インクルードと名前空間を書き込む
    Sub インクルードと名前空間()
        Utility.WriteIncludeAndNamespace()
    End Sub

    'インクルードガードと名前空間を書き込む
    Sub インクルードガードと名前空間()
        Utility.WriteIncludeGuardAndNamespace()
    End Sub

    'ソリューションエクスプローラを開き、アクティブ化する
    Sub ソリューションへ()
        Utility.ActiveSolution()
    End Sub

    '選択中のテキストをGoogle検索する
    Sub ブラウザ検索()
        Dim ts As TextSelection = DTE.ActiveDocument.Selection
        If Not ts.IsEmpty Then
            System.Diagnostics.Process.Start("http://www.google.co.jp/search?sourceid=chrome&ie=UTF-8&q=" + ts.Text)
        End If
    End Sub

    'サンプル InsertTimeDate() から
    Sub 日時挿入()
        Dim textSelection As EnvDTE.TextSelection
        textSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
        textSelection.Text = System.DateTime.Now.ToLongDateString() + " " + System.DateTime.Now.ToLongTimeString()
    End Sub

    'デバッグなしで実行
    Sub 実行()
        DTE.ExecuteCommand("Debug.StartWithoutDebugging")
    End Sub
End Module


リンク