共用方式為


控制選項設定

更新:2007 年 11 月

您可以啟動或停用 [工具] 功能表上 [選項] 對話方塊中各頁面 (以下稱為「選項頁面」) 的設定。您只需要使用 Visual Studio Automation 模型中的 PropertiesValue 屬性,以及 DTE 物件的 Item 方法。

注意事項:

有些 [選項] 頁中的部分項目不能以程式設計方式存取。大多數項目 (例如 [工作清單選項] 頁中的 [語彙基元清單] 註解) 都可以用程式設計方式來檢視或變更,但是有些 [選項] 頁則不能,例如 [環境] 頁的 [說明] 節點中的 [動態說明] 頁。此外,雖然有些 [選項] 頁具有可程式化的設定,但是選項頁中的項目不一定都能存取。如果您發現某項設定無法變更,可能就需要使用 Visual Studio Industry Partner (VSIP) program 才能予以變更。如需詳細資訊,請參閱本主題稍後的<加入設定至現有選項頁>一節。如需可用程式設計方式存取的選項及其確實名稱的完整清單,請參閱在選項頁中決定屬性項目的名稱中的<屬性項目名稱>。

顯示選項設定

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

變更選項設定

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

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

' 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 Automation 模型無法達成這個目的,您必須使用 Visual Studio Industry Partner (VSIP) program。如需詳細資訊,請參閱Visual Studio 產業夥伴網站

請參閱

工作

HOW TO:建立自訂工具選項頁

HOW TO:變更視窗特性

HOW TO:建立增益集

逐步解說:建立精靈

概念

Automation 物件模型圖表

其他資源

建立和控制環境視窗

建立增益集和精靈

Automation 與擴充性參考