如何:控制动态帮助窗口

更新:2007 年 11 月

Visual Studio 具有“动态帮助”窗口,此窗口根据用户的上下文,提供指向相关帮助主题的链接。例如,如果开始在代码编辑器中进行编辑,将在“动态帮助”窗口中显示如何使用代码编辑器的主题。此窗口将与当前上下文相关的关键字和与帮助主题相关联的关键字列表进行比较,从而执行此操作。

ContextAttributes 集合与 ContextAttribute 对象使您能够列出适用于 Visual Studio 中指定窗口的关键字集合。它们还使您能够添加属性,并将属性按优先顺序排列。因此,自动化客户端可以在“动态帮助”窗口或窗口的元素(如选项卡或链接)被选定时,在该窗口中显示它们选择的主题。

ContextAttribute 对象表示特定窗口或窗口元素的上下文,而 ContextAttributes 是给定窗口的所有 ContextAttribute 对象的集合。上下文包括三种上下文类型:Filter、Lookup 和 LookupF1:

Filter

属性添加到上下文集合中。属性是名称/值对,用于筛选“动态帮助”窗口中显示的 F1 和查找关键字主题,或筛选当用户按 F1 时显示的主题。

Lookup

关键字添加到上下文集合中。对应于此关键字的主题显示在“动态帮助”窗口中。Lookup 关键字提供在“动态帮助”窗口中显示的背景或相关主题的列表。“动态帮助”窗口中的关键字主题列表在用户更改当前选定内容时更新。Lookup 关键字与在已编译的帮助文件的索引中显示的关键字从同一源派生。

LookupF1

与此关键字对应的主题在“动态帮助”窗口中显示并用于查找 F1 帮助主题。当上下文元素(如工具窗口、编辑器或模式对话框)被选定并且按 F1 键时,F1 关键字将提供帮助主题。F1 关键字主题也在“动态帮助”窗口中列出。F1 关键字被添加到上下文集合中。

有三种类型的上下文属性指示 ContextAttributes 对象产生的位置:

全局

全局集成开发环境 (IDE) 上下文。

高优先级

高优先级上下文集合。高优先级上下文属性意味着主题链接显示在“动态帮助”窗口中其链接组的顶部。

窗口

窗口上下文。

从下列对象中可以获得 ContextAttributes 集合:

ContextAttributes

影响全局上下文集合,它具有用于排序主题的最低优先级。

ContextAttributes

影响窗口的上下文集合。对于工具窗口,此属性仅当窗口被选中时才有效。对于编辑器和设计器,只要编辑器为最后的活动 MDI 子窗口,属性就有效。如果 HighPriorityAttributes 属性的值是 True,则此属性总是有效并具有最高优先级。

将关键字与窗口或窗口元素关联后,可以使用此关键字来显示帮助主题。通过使用 XML 帮助提供程序,可以显示自定义帮助主题或 URL。有关更多信息,请在 Visual Studio Industry Partner (VSIP) SDK 帮助中搜索“XML 帮助提供程序和动态帮助”。

除了控制“动态帮助”窗口的内容外,还可以控制其特性(如宽度和高度)。有关更多信息,请参见 如何:更改窗口特性

使用 ContextAttributesContextAttribute 可以执行以下操作:

  • 添加或移除关键字名称、值和类型。

  • 获取关键字的名称和值。

  • 从 DTE.ContextAttributes 中获取高优先级属性列表作为 ContextAttributes 集合。

f88ctah3.alert_note(zh-cn,VS.90).gif说明:

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。这些过程是使用现用的常规开发设置开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置

示例

此外接程序示例演示如何引用和使用“动态帮助”自动化模型的各种成员。此示例列出了与“解决方案资源管理器”相关联的关键字的名称和数量。此示例还将一个新的 F1 关键字添加到列表,并将其移除。有关如何将此示例作为外接程序或 Visual Studio 宏运行的更多信息,请参见如何:编译和运行自动化对象模型代码示例

Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    ' Pass the applicationObject member variable to the code example.
    CATest(_applicationObject)
End Sub

Sub CATest(ByVal dte As DTE2)
    ' Get a reference to Solution Explorer.
    Dim SolnEx As Window = _applicationObject.Windows.Item(Constants. _
    vsWindowKindSolutionExplorer)
    Dim CA As ContextAttribute

    ' List the current attributes associated with Solution Explorer.
    ListAttr(SolnEx, CA)

    ' Associate a new F1 keyword to Solution Explorer.
    SolnEx.ContextAttributes.Add("ANewKeyword", "900", _
    vsContextAttributeType.vsContextAttributeLookupF1)
    ListAttr(SolnEx, CA)

    ' Delete the new F1 keyword from Solution Explorer.
    SolnEx.ContextAttributes.Item(3).Remove()
    ListAttr(SolnEx, CA)
End Sub

Sub ListAttr(ByVal SolnEx As Object, ByVal CA As ContextAttribute)
    ' Support function for CATest(). Lists the current attributes 
    ' associated with Solution Explorer.
    Dim msg As String
    msg = ""

    MsgBox("Number of context attributes in Solution Explorer: "  _
    & SolnEx.ContextAttributes.Count)
    For Each CA In SolnEx.ContextAttributes
        msg = msg & CA.Name & Chr(13)
    Next
    MsgBox(msg)
    msg = ""
End Sub
public void OnConnection(object application,
 Extensibility.ext_ConnectMode connectMode, object addInInst, ref
 System.Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    //  Pass the applicationObject member variable to the code example.
    CATest(_applicationObject); 
}

public void CATest( DTE2 dte ) 
{ 
    // Add-in code.
    // Get a reference to Solution Explorer.
    Window SolnEx = dte.Windows.Item
( Constants.vsWindowKindSolutionExplorer ); 
    ContextAttribute CA = null; 

    // List the current attributes associated with Solution Explorer.
    ListAttr( SolnEx, CA ); 

    // Associate a new F1 keyword to Solution Explorer.
    SolnEx.ContextAttributes.Add( "ANewKeyword",
 System.Convert.ToString(900),
 vsContextAttributeType.vsContextAttributeLookupF1 ); 
    ListAttr( SolnEx, CA ); 

    // Delete the new F1 keyword from Solution Explorer.
    SolnEx.ContextAttributes.Item( 2 ).Remove(); 
    ListAttr( SolnEx, CA ); 
} 

public void ListAttr( EnvDTE.Window SolnEx, ContextAttribute CA ) 
{ 
    // Support function for CATest(). Lists the current attributes 
    // associated with Solution Explorer.
    string msg = null; 

    MessageBox.Show
( "Number of context attributes in Solution Explorer: " 
+ SolnEx.ContextAttributes.Count); 
    foreach ( EnvDTE.ContextAttribute temp in 
SolnEx.ContextAttributes ) 
    { 
        CA = temp; 
        msg = msg + CA.Name + "\n"; 
    }
    MessageBox.Show( msg); 
    msg = ""; 
}

请参见

任务

如何:更改窗口特性

如何:创建外接程序

演练:创建向导

概念

自动化对象模型图表

其他资源

创建和控制环境窗口

创建外接程序和向导

自动化与扩展性参考