Share via


控制選項設定

您可以撰寫程式碼來啟動或停用 [選項] 對話方塊中頁面上的許多設定 (以下稱為「選項頁」(Options pages))。 只要使用 Visual Studio Automation 模型中的 Properties 屬性、Value 屬性,以及 DTE 物件的 Item 方法即可。

注意事項注意事項

雖然許多 [選項] 頁上的許多項目都可以藉由程式設計的方式存取,但是某些頁面可能包含無法存取的項目。 另外,[選項] 頁本身可能也無法存取。 如果您無法使用 Automation 模型改變某項設定,可能可以使用 Visual Studio SDK 改變該設定。 如需詳細資訊,請參閱本文件稍後的<加入設定至現有選項頁>。 如需可藉由程式設計方式存取的選項清單及其確實名稱,請參閱在選項頁中決定屬性項目的名稱中的<屬性項目名稱>。

若要在整合式開發環境 (IDE) 中開啟 [選項] 對話方塊,請按一下 [工具] 功能表上的 [選項]。

顯示選項設定

使用 Properties 集合和 Property 物件存取 [選項] 頁上的設定。 下列 Visual Studio 巨集範例會顯示 [文件] 頁面上項目的名稱、目前值和類型。

' 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
            Try
                msg += ("PROP NAME: " & prop.Name & _ 
                " VALUE: " & prop.Value) & _
                "   TYPE: " & prop.Value.GetType.ToString()) & vbCr
            Catch
            End Try
    Next
    MsgBox(msg)
End Sub

下列巨集範例會顯示 [環境] 節點下 [工作清單] 之 [選項] 頁上的可用屬性。 此巨集也會列出 [語彙基元清單] 註解的可用值。

' 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

變更選項設定

您不僅能夠顯示 [選項] 頁上的設定值,還可以變更該值。 下列 Visual Studio 巨集範例會示範如何變更值。

注意事項注意事項

雖然您可以在現有 [選項] 頁中變更控制項的值,但是不能加入、移除或修改任何控制項或設定。 若要指定自己的設定,必須建立自訂 [選項] 頁。 如需詳細資訊,請參閱 HOW TO:建立自訂選項頁面

第一個範例 (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

下列巨集範例會在 [文字編輯器] 節點下的 [基本] 頁中,先變更再重設 [定位點] 區段中的 [定位點大小] 值。

' 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

這個巨集範例會變更 [環境] 節點下 [字型和色彩] 頁面上的設定。

' 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

這個巨集範例會在 [文字編輯器] 節點中開啟數種語言的行號設定功能。

' 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 Automation 模型將設定加入至現有 [選項] 頁,或是變更現有設定。若要進行這類修改,您必須使用 Visual Studio SDK。 如需詳細資訊,請參閱開發工具生態系統合作夥伴入口網站 (英文)。

請參閱

工作

HOW TO:建立自訂選項頁面

HOW TO:變更視窗特性

HOW TO:建立增益集

逐步解說:建立精靈

概念

Automation 物件模型圖表

其他資源

建立和控制環境視窗

建立增益集和精靈

Automation 與擴充性參考