控制选项设置
更新:2007 年 11 月
您可以激活或取消激活“工具”菜单上的“选项”对话框中各种页面(以下称为“选项页”)的设置。只需使用 Visual Studio 自动化模型中 DTE 对象的 Properties 和 Value 属性以及 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
更改选项设置
虽然您可以更改现有“选项”页上控件的值,但不能添加、移除或修改其任何控件或设置。若要指定自己的设置,必须创建自定义“选项”页。有关更多信息,请参见如何:创建自定义工具选项页。
更改“选项”页中项的值与显示其值非常相似。下面的宏示例演示如何执行此操作。
第一个示例 (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 自动化模型无法执行此操作。必须使用 Visual Studio Industry Partner (VSIP) program。有关更多信息,请参见 Visual Studio Industry Partner Web site(Visual Studio 行业合作伙伴网站)。