更新:2007 年 11 月
| 适用对象 | 
|---|
| 本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。 项目类型 
 Microsoft Office 版本 
 有关更多信息,请参见按应用程序和项目类型提供的功能。 | 
此演练演示如何响应 XMLNode 事件。当用户在文档中移动光标时,操作窗格上的控件将会隐藏或变为可见状态。
本演练阐释以下任务:
- 向操作窗格中添加控件。 
- 打开应用程序时显示操作窗格。 
- 根据 XML 节点中的用户操作隐藏和显示操作窗格上的控件。 
| .gif) 说明: | 
|---|
| 对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您使用的 Visual Studio 版本及设置。有关更多信息,请参见Visual Studio 设置。 | 
先决条件
您需要以下组件来完成本演练:
- Visual Studio Tools for Office(Visual Studio 2008 专业版 和 Visual Studio Team System 的可选组件)。 
- Microsoft Office Word 2003。 
| .gif) 说明: | 
|---|
| 您也可以使用 Word 2007 执行此演练。但是,部分说明假定您在使用 Word 2003 中的菜单。 | 
默认情况下,Visual Studio Tools for Office 随列出的 Visual Studio 版本一起安装。若要检查它是否已安装,请参见安装 Visual Studio Tools for Office。
创建项目
第一步是创建 Word 文档项目。
创建新项目
- 创建一个名为“我的动态操作窗格”的 Word 文档项目。在向导中,选择“创建新文档”。有关更多信息,请参见如何:创建 Visual Studio Tools for Office 项目。 - Visual Studio 在设计器中打开新 Word 文档并将“我的动态操作窗格”项目添加到“解决方案资源管理器”中。 
创建要映射到文档的架构
若要创建 XML 节点,请将元素从 XML 架构拖到文档中。首先创建一个架构文件,然后创建一个与该架构对应的文档,接着将元素映射到该文档。
创建 XML 架构
- 在“解决方案资源管理器”中选择“我的动态操作窗格”项目。 
- 在“项目”菜单上单击“添加新项”。 - 即会出现“添加新项”对话框。 
- 在“模板”窗格中选择“XML 架构”。 
- 将该架构命名为“ActionsPaneSample.xsd”,然后单击“添加”。 
- 在“解决方案资源管理器”中右击“ActionsPaneSample.xsd”,然后单击“打开方式”。 
- 在“打开方式”对话框中选择“XML 编辑器”,然后单击“确定”。 
- 用下面的文本替换 XML 编辑器中的文本。 - <?xml version="1.0" encoding="utf-8" ?> <xs:schema targetNamespace="https://schemas.microsoft.com/vsto/samples" elementFormDefault="qualified" xmlns="https://schemas.microsoft.com/vsto/samples" xmlns:mstns="https://schemas.microsoft.com/vsto/samples" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Sample" type="SampleType"></xs:element> <xs:complexType name="SampleType"> <xs:all> <xs:element name="Insert" type="InsertType" minOccurs="0" maxOccurs="1" /> <xs:element name="Table" type="xs:string" minOccurs="0" maxOccurs="1" /> </xs:all> </xs:complexType> <xs:complexType name="InsertType"> <xs:sequence> <xs:element name="MemoTo" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="MemoFrom" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="MemoSubject" type="xs:string" minOccurs="1" maxOccurs="1"/> </xs:sequence> </xs:complexType> </xs:schema>
- 保存您的项目。 
向文档中添加文本和 XML
此演练的文档采用备忘录形式。将文本添加到文档,然后将 XML 元素映射到文档中的位置。
向文档中添加文本
- 在“解决方案资源管理器”中,右击“ThisDocument.cs”或“ThisDocument.vb”,然后单击“视图设计器”。 
- 向 Word 文档中添加以下文本。 - 备忘录 - 收件人: - 发件人: - 主题: - 下表列出所要求的联系人信息: - 姓名 - 地址 - 城市 - 州 - 邮政编码 - Nancy Davolio - 507 - 20th Ave. E., Apt.2A - Seattle - WA - 98122 
接着,将 XML 元素映射到备忘录中的文本。将为映射的每个 XML 元素创建一个 XMLNode 控件。通过在 Node 单词后面追加父元素和子元素的名称来对 XMLNode 控件进行命名。例如,如果从上面的架构中映射 Insert 元素,则将其命名为 SampleInsertNode。有关控件的更多信息,请参见 宿主项和宿主控件概述。
将架构附加到文档
- 打开“模板和加载项”对话框。在 Word 2007 中的操作步骤: - 在功能区上,单击“开发人员”选项卡。 .gif) 说明: 说明:- 如果看不到“开发人员”选项卡,您必须首先显示该选项卡。有关更多信息,请参见如何:在功能区上显示“开发人员”选项卡。 
- 在“XML”组中,单击“架构”。 - “模板和加载项”对话框将打开。 
 - 在 Word 2003 中打开“模板和加载项”对话框: - 在“工具”菜单上指向“Microsoft Office Word Tools”,然后单击“模板和加载项” - “模板和加载项”对话框将打开。 .gif) 说明: 说明:- 如果“Microsoft Office Word Tools”在“工具”菜单中不可用,请单击文档向其提供焦点。有关更多信息,请参见 Visual Studio 环境中的 Office 菜单。 
 
- 在“模板和加载项”对话框中单击“XML 架构”选项卡,然后单击“添加架构”。 
- 浏览到以前创建的“ActionsPaneSample.xsd”架构(位于项目目录中),然后单击“打开”。 
- 在“架构设置”对话框中单击“确定”。 
- 单击“确定”以关闭“模板和加载项”对话框。 - 随即打开“XML 结构”任务窗格。 
将 XML 元素映射到文档
- 在“XML 结构”任务窗格中单击“Sample”元素。 
- 出现提示后单击“应用于整个文档”。 
- 选择“收件人”、“发件人”和“主题”这三行文本,然后在“XML 结构”任务窗格中单击“Insert”元素。 
- 将光标放到“收件人:”之后,按两次 Tab 键,然后单击“MemoTo”元素将其插入。 
- 将光标放到“发件人:”之后,按两次 Tab 键,然后单击“MemoFrom”元素。 
- 将光标放到“主题:”之后,按两次 Tab 键,然后单击“MemoSubject”元素。 
- 选择表使整个表突出显示,然后单击“Table”元素以对其进行应用。 - 在表单元格内出现 XML 标记。 
- 清除“在文档中显示 XML 标记”复选框。 
- 在“XML 结构”任务窗格底部单击“XML 选项”。 
- 选择“忽略混合内容”复选框,然后单击“确定”。 
- 关闭“XML 结构”任务窗格。 
向操作窗格中添加控件
若要设计操作窗格上控件的布局,可以使用名为操作窗格控件的控件的设计器,此控件本质上与用户控件相同。可以将其他控件(比如按钮和文本框)拖到操作窗格控件上并对其进行排列。若要依据用户上下文更改操作窗格,可以为每个上下文添加和设计一个操作窗格控件,然后根据需要显示和隐藏它们。
此演练使用三个操作窗格控件。第一个操作窗格控件包含三个文本框和一个按钮,以便用户能够输入数据并将数据添加到文档。第二个操作窗格控件包含一个按钮,用于打开“表属性”对话框。第三个操作窗格控件包含一个复选框,用于显示操作窗格上的所有控件(不管光标位于文档中的何处)。
添加操作窗格控件
- 在“解决方案资源管理器”中选择“我的动态操作窗格”项目。 
- 在“项目”菜单上单击“添加新项”。 
- 在“添加新项”对话框中选择“操作窗格控件”,将其命名为“AddTextControl”,然后单击“添加”。 
- 将操作窗格控件的“Size”属性更改为“170, 135”。 
向第一个操作窗格控件添加 Windows 窗体控件
- 从“工具箱”的“Windows 窗体”选项卡中,将“Label”控件拖动到“AddTextControl”,然后将“Text”属性更改为“收件人:”。 
- 向“AddTextControl”中添加“Textbox”控件,并更改以下属性。 - 属性 - 值 - Name - toBox - Size - 110, 20 
- 向“AddTextControl”中添加第二个“Label”控件,并将“Text”属性更改为“发件人:”。 
- 向“AddTextControl”中添加第二个“Textbox”控件,并更改以下属性。 - 属性 - 值 - Name - fromBox - Size - 110, 20 
- 将第三个“Label”控件添加到前一个文本框下面的“AddTextControl”中,并将“Text”属性更改为“主题:”。 
- 将第三个“Textbox”控件添加到前一个标签旁边的“AddTextControl”中,并更改以下属性。 - 属性 - 值 - Name - subjectBox - Multiline - True - Size - 110, 40 
- 调整“subjectBox”文本框的大小使其允许多行。 
- 向“AddTextControl”中添加一个“Button”控件,并更改以下属性。 - 属性 - 值 - Name - insertText - Text - Insert 
创建第二个操作窗格控件
- 向项目中添加第二个操作窗格控件,并将其命名为“ShowPropertiesControl”。 
- 将操作窗格控件的“Size”属性更改为“170, 50”。 
- 从“工具箱”的“Windows 窗体”选项卡中将“Button”控件拖动到“ShowPropertiesControl”,并更改以下属性。 - 属性 - 值 - Name - tableProperties - Text - Table Properties - Enabled - False - Size - 100, 23 
- 对于 C#,将按钮的“Modifiers”属性更改为“Internal”。 
创建第三个操作窗格控件
- 向项目中添加第三个操作窗格控件,并将其命名为“ShowAllControl”。 
- 将该操作窗格控件的“Size”属性更改为“170, 75”。 
- 从“工具箱”的“Windows 窗体”选项卡中,将“GroupBox”控件添加到“ShowAllControl”,并将其“Text”属性更改为“我的操作窗格”。 
- 在此组合框顶部向“ShowAllControl”中添加一个“CheckBox”控件,并更改以下属性。 - 属性 - 值 - Name - showCheck - Text - 显示所有控件 
- 对于 C#,将该复选框的“Modifiers”属性更改为“Internal”。 
添加用户上下文响应代码
现在您可以编写代码以显示操作窗格,并根据光标在文档中的位置添加和移除操作窗格上的操作窗格控件。代码将放在 XMLNode 控件的事件处理程序中。
向 XMLNode 控件事件添加代码
- 在“解决方案资源管理器”中,右击“ThisDocument.vb”或“ThisDocument.cs”,然后单击“查看代码”。 
- 在 ThisDocument 的声明部分为每个操作窗格控件创建一个变量引用。 - Dim addText As New AddTextControl Dim showProperties As New ShowPropertiesControl Dim showAll As New ShowAllControl Friend WithEvents showCheck As System.Windows.Forms.CheckBox- private AddTextControl addText = new AddTextControl(); private ShowPropertiesControl showProperties = new ShowPropertiesControl(); private ShowAllControl showAll = new ShowAllControl();
- 在 ThisDocument 的 Startup 事件处理程序中,编写以下代码以添加 ShowAllControl 控件和显示操作窗格。 - Me.ActionsPane.Controls.Add(showAll) showCheck = showAll.showCheck- this.ActionsPane.Controls.Add(showAll);.gif) 说明: 说明:- 向操作窗格添加控件的顺序将确定其位置。如果要使某个控件始终在操作窗格顶部可见,则应首先添加该控件。该顺序还依赖于操作窗格的 StackOrder 属性。有关更多信息,请参见 如何:管理操作窗格上的控件布局。 
- 将以下代码添加到 SampleInsertNode 控件的 ContextEnter 事件处理程序。 - Private Sub SampleInsertNode_ContextEnter(ByVal sender As Object, _ ByVal e As Microsoft.Office.Tools.Word.ContextChangeEventArgs) _ Handles SampleInsertNode.ContextEnter If showAll.showCheck.Checked = False Then Me.ActionsPane.Controls.Add(addText) Me.ActionsPane.Controls.Remove(showProperties) End If End Sub- private void SampleInsertNode_ContextEnter(object sender, Microsoft.Office.Tools.Word.ContextChangeEventArgs e) { if (showAll.showCheck.Checked == false) { this.ActionsPane.Controls.Add(addText); this.ActionsPane.Controls.Remove(showProperties); } }
- 将以下代码添加到 SampleInsertNode 控件的 ContextLeave 事件处理程序,以便在用户将光标移出此 XML 节点后隐藏控件。 - Private Sub SampleInsertNode_ContextLeave(ByVal sender As Object, _ ByVal e As Microsoft.Office.Tools.Word.ContextChangeEventArgs) _ Handles SampleInsertNode.ContextLeave If showAll.showCheck.Checked = False Then Me.ActionsPane.Controls.Remove(addText) End If End Sub- private void SampleInsertNode_ContextLeave(object sender, Microsoft.Office.Tools.Word.ContextChangeEventArgs e) { if (showAll.showCheck.Checked == false) { this.ActionsPane.Controls.Remove(addText); } }
- 将以下代码添加到 SampleTableNode 控件的 ContextEnter 事件处理程序。 - Private Sub SampleTableNode_ContextEnter(ByVal sender As Object, _ ByVal e As Microsoft.Office.Tools.Word.ContextChangeEventArgs) _ Handles SampleTableNode.ContextEnter If showAll.showCheck.Checked = False Then Me.ActionsPane.Controls.Remove(addText) Me.ActionsPane.Controls.Add(showProperties) End If showProperties.tableProperties.Enabled = True End Sub- private void SampleTableNode_ContextEnter(object sender, Microsoft.Office.Tools.Word.ContextChangeEventArgs e) { if (showAll.showCheck.Checked == false) { this.ActionsPane.Controls.Remove(addText); this.ActionsPane.Controls.Add(showProperties); } showProperties.tableProperties.Enabled = true; }
- 将以下代码添加到 SampleTableNode 控件的 ContextLeave 事件处理程序,以便在用户将光标移出此 XML 节点后隐藏控件。 - Private Sub SampleTableNode_ContextLeave(ByVal sender As Object, _ ByVal e As Microsoft.Office.Tools.Word.ContextChangeEventArgs) _ Handles SampleTableNode.ContextLeave If showAll.showCheck.Checked = False Then Me.ActionsPane.Controls.Remove(showProperties) End If showProperties.tableProperties.Enabled = False End Sub- private void SampleTableNode_ContextLeave(object sender, Microsoft.Office.Tools.Word.ContextChangeEventArgs e) { if (showAll.showCheck.Checked == false) { this.ActionsPane.Controls.Remove(showProperties); } showProperties.tableProperties.Enabled = false; }
- 在 C# 中,必须为 XMLNode 控件添加事件处理程序。可以将此代码放置在 ThisDocument 的 Startup 事件处理程序中。有关创建事件处理程序的信息,请参见如何:使用 Visual Studio Tools for Office 创建事件处理程序。 - this.SampleInsertNode.ContextEnter += new Microsoft.Office.Tools.Word. ContextChangeEventHandler(SampleInsertNode_ContextEnter); this.SampleInsertNode.ContextLeave += new Microsoft.Office.Tools.Word. ContextChangeEventHandler(SampleInsertNode_ContextLeave); this.SampleTableNode.ContextEnter += new Microsoft.Office.Tools.Word. ContextChangeEventHandler(SampleTableNode_ContextEnter); this.SampleTableNode.ContextLeave += new Microsoft.Office.Tools.Word. ContextChangeEventHandler(SampleTableNode_ContextLeave);
检查点
保存项目并验证操作窗格控件是否能随着光标在文档中的移动而发生改变。
测试项目
- 按 F5 运行项目。 
- 将插入点移动到单词“收件人”处,并验证 addText 操作窗格控件在操作窗格上是否可见。 
- 将插入点移动到表内,并验证 showProperties 操作窗格控件在操作窗格上是否可见。 
- 将插入点移动到“备忘录”一词处,并验证 addText 或 showProperties 操作窗格控件在操作窗格上是否不可见。 
添加代码以响应操作窗格的控件事件
至此,操作窗格上的控件未执行任何操作。最后一步是编写代码以显示所有控件,插入文本和显示“表属性”对话框。
显示操作窗格上的所有控件
- 将下列代码添加到 ThisDocument。 - Private Sub showCheck_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles showCheck.CheckStateChanged If showAll.showCheck.Checked = True Then Me.ActionsPane.Controls.Add(addText) Me.ActionsPane.Controls.Add(showProperties) Else Me.ActionsPane.Controls.Remove(addText) Me.ActionsPane.Controls.Remove(showProperties) End If End Sub- private void showCheck_CheckStateChanged(object sender, EventArgs e) { if (showAll.showCheck.Checked) { this.ActionsPane.Controls.Add(addText); this.ActionsPane.Controls.Add(showProperties); } else { this.ActionsPane.Controls.Remove(addText); this.ActionsPane.Controls.Remove(showProperties); } }
- 在 C# 中,必须为 checkbox 控件添加事件处理程序。可以将此代码放置在 ThisDocument 的 Startup 事件处理程序中。 - showAll.showCheck.CheckStateChanged += new EventHandler(showCheck_CheckStateChanged);
单击“插入”按钮时插入文本
- 在“解决方案资源管理器”中右击“AddTextControl.vb”或“AddTextControl.cs”,然后单击“查看代码”。 
- 在 insertText 按钮的 Click 事件处理程序中,编写以下代码。 - Private Sub insertText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles insertText.Click With Globals.ThisDocument .InsertMemoFromNode.Text = Me.fromBox.Text .InsertMemoToNode.Text = Me.toBox.Text .InsertMemoSubjectNode.Text = Me.subjectBox.Text End With ' Clear the text boxes. Me.fromBox.Text = "" Me.toBox.Text = "" Me.subjectBox.Text = "" End Sub- private void insertText_Click(object sender, System.EventArgs e) { Globals.ThisDocument.InsertMemoFromNode.Text = this.fromBox.Text; Globals.ThisDocument.InsertMemoToNode.Text = this.toBox.Text; Globals.ThisDocument.InsertMemoSubjectNode.Text = this.subjectBox.Text; // Clear the text boxes. this.fromBox.Text = ""; this.toBox.Text = ""; this.subjectBox.Text = ""; }
- 在 C# 中,必须为按钮添加事件处理程序。可以将此代码放在 AddTextControl 构造函数中 InitializeComponent 调用的后面。 - public AddTextControl() { InitializeComponent(); this.insertText.Click += new System.EventHandler(this.insertText_Click); }
显示“表属性”对话框
- 在“解决方案资源管理器”中右击“ShowPropertiesControl.vb”或“ShowPropertiesControl.cs”,然后单击“查看代码”。 
- 将以下代码添加到 tableProperties 按钮的 Click 事件处理程序中。 - Private Sub tableProperties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles tableProperties.Click Globals.ThisDocument.Application.Dialogs( _ Word.WdWordDialog.wdDialogTableProperties).Show() End Sub- private void tableProperties_Click(object sender, System.EventArgs e) { object timeout = 0; Globals.ThisDocument.Application.Dialogs[ Microsoft.Office.Interop.Word.WdWordDialog.wdDialogTableProperties] .Show(ref timeout); }
- 在 C# 中,必须为按钮添加事件处理程序。可以将这些代码放在 ShowPropertiesControl 构造函数中 InitializeComponent 调用的后面。 - public ShowPropertiesControl() { InitializeComponent(); this.tableProperties.Click += new System.EventHandler(this.tableProperties_Click); }
测试应用程序
现在可以进一步测试文档以验证操作窗格控件的功能。
测试文档
- 按 F5 运行项目。 
- 确认操作窗格可见。 
- 将光标移动到包含“收件人”、“发件人”和“主题”行的文档部分,确认 addText 控件显示在操作窗格上。 
- 在操作窗格上的“收件人”、“发件人”和“主题”文本框中输入文本,然后单击“插入”。 
- 将光标移动到表上,确认 showProperties 控件显示在操作窗格上。 
- 单击“表属性”按钮,确认“表属性”对话框打开。 
- 单击“全部显示”复选框,确认操作窗格上的所有控件均可见。 
- 将光标移到表外,确认“表属性”按钮变为禁用状态。 
后续步骤
此演练演示根据用户上下文更改操作窗格的基本操作。下一步可能要执行以下几项任务:
- 部署项目。有关更多信息,请参见 如何:部署 Office 解决方案 (2003 System)。 
- 将数据绑定到 Word 中的控件。有关更多信息,请参见演练:将数据绑定到“Word 操作”窗格上的控件。 
- 将数据绑定到 Excel 中的控件。有关更多信息,请参见 演练:将数据绑定到 Excel 操作窗格上的控件。