オプション設定の制御

更新 : 2007 年 11 月

[ツール] メニューの [オプション] ダイアログ ボックス内のさまざまなページ (これ以降、"オプション ページ" と呼びます) の設定をアクティブまたは非アクティブにすることができます。Visual Studio オートメーション モデルの DTE オブジェクトの Properties プロパティ、Value プロパティ、および Item メソッドを使用するだけで、この操作を行うことができます。

awdwz11a.alert_note(ja-jp,VS.90).gifメモ :

オプション ページによっては、プログラムでアクセスできない項目もあります。[タスク一覧] オプション ページのコメントの [トークン リスト] など、ほとんどの項目はプログラムで表示または変更できます。ただし、[環境] ページの [ヘルプ] ノード内の一部のオプション ページ ([ダイナミック ヘルプ] ページなど) には、プログラムでアクセスできません。また、一部のオプション ページには、コードから操作できる設定がありますが、そのページの項目に必ずしもアクセスできるとは限りません。設定を変更できないことに気付いた場合は、この操作を行うために、Visual Studio Industry Partner (VSIP) プログラム の使用が必要になることがあります。詳細については、このトピックで後述する「既存のオプション ページへの設定の追加」を参照してください。プログラムでアクセスできるオプションおよびその正確な名前の一覧については、「オプション ページにあるプロパティ項目名の確認」の「プロパティ項目名」を参照してください。

オプションの設定の表示

Properties コレクションおよび Property オブジェクトを使用して、既存のオプション ページの設定にアクセスします。次の VSMacro の例では、[ドキュメント] オプション ページ内のすべての項目の名前および現在の値を表示します。

' Macro code.
Sub PropertiesExample()
    ' Create and initialize a variable to represent the Documents 
    ' Options page.
    Dim envGenTab As EnvDTE.Properties = _
    DTE.Properties("Environment", "Documents")
    Dim prop As EnvDTE.Property
    Dim msg As String

    ' Loop through each item in the Documents Options box.
    For Each prop In envGenTab
        msg += ("PROP NAME: " & prop.Name & "   VALUE: " & _
        prop.Value) & vbCr
    Next
    MsgBox(msg)
End Sub

次の VSMacro の例では、[環境] ノードの下の [タスク一覧] のオプション ページにある、使用できるすべてのプロパティを表示します。また、この例では、コメントの [トークン リスト] に使用できる値の一覧も表示します。

' Macro code.
Sub DisplayProperties()
    ' Variables to represent the properties collection
    ' and each property in the Options dialog box.
    Dim prop As EnvDTE.Property
    Dim props As EnvDTE.Properties
    Dim propVals As Object()
    Dim propVal, msg As String

    ' Represents the Task List Node under the 
    ' Enviroment node.
    props = DTE.Properties("Environment", "TaskList")
    ' Represents the items in the comment Token list
    ' and their priorities (1-3/low-high).
    prop = props.Item("CommentTokens")
    propVals = prop.Value

    Try
        ' List each property name for the Options page
        ' and all of its possible values.
        For Each prop In props
            msg += "PROP NAME: " & prop.Name & vbCr
            For Each propVal In propVals
                msg += "  Value: " & propVal & vbCr
            Next
        Next
        MsgBox(msg)
    Catch ex As System.Exception
        MsgBox("ERROR: " & ex.Message)
    End Try
End Sub

次の例では、[テキスト エディタ] ノードの下の [C#] の [書式設定] にあるオプション ページのコードから操作できる設定の一覧を表示します。

' Macro code.
Sub PropertiesExample()
    ' Create and initialize a variable to represent the C# 
    ' Formatting text editor options page.
    Dim txtEdCSFormat As EnvDTE.Properties = _
    DTE.Properties("TextEditor", "CSharp - Formatting")
    Dim prop As EnvDTE.Property
    Dim msg As String

    ' Loop through each item in the C# Formatting Options page.
    For Each prop In txtEdCSFormat
        msg += ("PROP NAME: " & prop.Name & "   VALUE: " & _
        prop.Value) & vbCr
    Next
    MsgBox(msg)
End Sub

オプションの設定の変更

既存のオプション ページ上のコントロールの値は変更できますが、そのページのコントロールまたは設定は追加、削除、または変更できません。独自の設定を指定するには、カスタムのオプション ページを作成する必要があります。詳細については、「方法 : カスタム ツール オプション ページを作成する」を参照してください。

オプション ページ内の項目の値を変更する方法は、その値を表示する方法と似ています。この方法を次のマクロの例で示します。

最初の例 (ToolOpt1) では、ReuseSavedActiveDocWindow のブール値を切り替えます。これは、[環境] ノードの [ドキュメント] ページにある "保存済みの現在のドキュメント ウィンドウを再利用" オプションに相当します。

' Macro code.
Sub ToolOpt1()
    Dim props As EnvDTE.Properties = DTE.Properties("Environment", _
    "Documents")
    Dim prop As EnvDTE.Property

    prop = props.Item("ReuseSavedActiveDocWindow")
    ' If value is TRUE, change it to FALSE, or vice-versa.
    MsgBox("PROP NAME: " & prop.Name & "   VALUE: " & prop.Value)
    prop.Value = Not (prop.Value)
    MsgBox("PROP NAME: " & prop.Name & "   VALUE: " & prop.Value)
    ' Change it to the original value.
    prop.Value = Not (prop.Value)
End Sub

次の VSMacro の例では、[テキスト エディタ] ノードの [Basic] ページの [タブ] セクションにある [タブ サイズ] の値を変更してリセットします。

' Macro code.
Sub ToolOpt2()
    Dim props As EnvDTE.Properties = DTE.Properties("TextEditor", _
    "Basic")
    Dim prop As EnvDTE.Property
    Dim tmp As String

    prop = props.Item("TabSize")
    ' Set a new value for Tab Size.
    MsgBox("PROP NAME: " & prop.Name & "   VALUE: " & prop.Value)
    tmp = prop.Value
    prop.Value = 10
    MsgBox("PROP NAME: " & prop.Name & "   VALUE: " & prop.Value)
    ' Change it back to the original value.
    prop.Value = tmp
    MsgBox("PROP NAME: " & prop.Name & "   VALUE: " & prop.Value)
End Sub

次の VSMacro の例では、[環境] ノードの [フォントおよび色] ページ内の設定を変更します。

' Macro code.
Sub ToolOpt3()
    ' Changes the background color of text in the Fonts and Colors
    ' page of the Options dialog box on the Tools menu.
    Dim props As EnvDTE.Properties
    Dim prop As EnvDTE.Property
    Dim fontColorItems As EnvDTE.FontsAndColorsItems

    props = DTE.Properties("FontsAndColors", "TextEditor")
    prop = props.Item("FontsAndColorsItems")
    fontColorItems = prop.Object

    Try
        MsgBox("NAME: " & prop.Name & vbCr & "BACKGROUND VALUE: " & _
        CStr(fontColorItems.Item("Plain Text").Background.ToString))
        ' Turn the text background from its current color to red.
        fontColorItems.Item("Plain Text").Background = 255
        MsgBox("NAME: " & prop.Name & vbCr & "BACKGROUND VALUE: " & _
        Hex(fontColorItems.Item("Plain Text").Background.ToString))
    Catch ex As System.Exception
        MsgBox("ERROR: " & ex.Message)
    End Try
End Sub

次の VSMacro の例では、[オプション] ダイアログ ボックスの [テキスト エディタ] ノード内の複数の言語で行番号表示を有効にします。

' Macro code.
Sub TurnOnLineNumbers()
   DTE.Properties("TextEditor", "Basic").Item("ShowLineNumbers") _
   .Value = True
   DTE.Properties("TextEditor", "PlainText").Item("ShowLineNumbers") _
   .Value = True
   DTE.Properties("TextEditor", "CSharp").Item("ShowLineNumbers") _
   .Value = True
   DTE.Properties("TextEditor", "HTML/XML").Item("ShowLineNumbers") _
   .Value = True
   DTE.Properties("TextEditor", "C/C++").Item("ShowLineNumbers") _
   .Value = True
   DTE.Properties("TextEditor", "Visual JSharp") _
   .Item("ShowLineNumbers").Value = True
End Sub

既存のオプション ページへの設定の追加

ある時点で、既存のオプション ページの独自の設定を変更または追加することが必要になる場合があります。たとえば、[フォントおよび色] ページに独自のフォントの設定を追加する場合などがあります。この操作は、Visual Studio オートメーション モデルを使用して行うことができません。Visual Studio Industry Partner (VSIP) プログラム を使用する必要があります。詳細については、「Visual Studio Industry Partner Web サイト」を参照してください。

参照

処理手順

方法 : カスタム ツール オプション ページを作成する

方法 : ウィンドウの特性を変更する

方法 : アドインを作成する

チュートリアル : ウィザードの作成

概念

オートメーション オブジェクト モデルの階層図

その他の技術情報

環境ウィンドウの作成と制御

アドインおよびウィザードの作成

オートメーションと機能拡張のリファレンス