控制选项设置

您可以编写代码来激活或停用**“选项”**对话框中的页面(以下简称为“选项页”)上的许多设置。 只需使用 Visual Studio 自动化模型中的 DTE 对象的 Properties 属性、Value 属性以及 Item 方法。

提示

虽然可以编程方式访问许多“选项”页上的很多项,但某些页面可能包含无法访问的项。 此外,“选项”页本身也可能无法访问。 如果您无法使用自动化模型影响设置,则可使用 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 宏示例演示如何执行此操作。

提示

虽然您可以更改现有“选项”页上控件的值,但不能添加、移除或修改任何控件或设置。 若要指定自己的设置,必须创建自定义“选项”页。 有关更多信息,请参见如何:创建自定义选项页

第一个示例 (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 自动化模型来将设置添加到现有**“选项”**页中或更改现有设置。若要进行这些类型的修改,必须使用 Visual Studio SDK。 有关更多信息,请参见Development Tools Ecosystem Partner Portal(开发工具生态系统合作伙伴门户)网站。

请参见

任务

如何:创建自定义选项页

如何:更改窗口特性

如何:创建外接程序

演练:创建向导

概念

自动化对象模型图表

其他资源

创建和控制环境窗口

创建外接程序和向导

自动化与扩展性参考