演练:创建书签的快捷菜单

更新:2007 年 11 月

适用于

本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。

项目类型

  • 文档级项目

Microsoft Office 版本

  • Word 2007

  • Word 2003

有关更多信息,请参见按应用程序和项目类型提供的功能

本演练演示如何在 Word 的文档级自定义项中创建 Bookmark 控件的快捷菜单。用户右击书签中的文本时,将出现一个快捷菜单,它为用户提供用于设置文本格式的选项。

本演练演示以下任务:

  • 设计时在文档级项目中向文档添加文本和书签。

  • 创建一个快捷菜单。

  • 检查是否有重叠的书签。

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

对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您使用的 Visual Studio 版本及设置。有关更多信息,请参见Visual Studio 设置

先决条件

您需要以下组件来完成本演练:

  • Visual Studio Tools for Office(Visual Studio 2008 专业版 和 Visual Studio Team System 的可选组件)。

  • Microsoft Office Word 2003 或 Microsoft Office Word 2007。

默认情况下,Visual Studio Tools for Office 随列出的 Visual Studio 版本一起安装。若要检查它是否已安装,请参见安装 Visual Studio Tools for Office

创建项目

第一步是在 Visual Studio 中创建 Word 文档项目。

创建新项目

  • 创建一个名为“我的书签快捷菜单”的 Word 文档项目。在向导中,选择“创建新文档”。有关更多信息,请参见如何:创建 Visual Studio Tools for Office 项目

    Visual Studio 将在设计器中打开一个新的 Word 文档,并将“我的书签快捷菜单”项目添加到“解决方案资源管理器”中。

向文档中添加文本和书签

向文档中添加一些文本,然后添加两个重叠书签。

向文档中添加文本

  • 键入以下文本。

    这是一个示例,将演示如何创建一个在右击书签中的文本时出现的快捷菜单。

向文档中添加 Bookmark 控件

  1. 从“工具箱”的“Word 控件”选项卡中,将一个 Bookmark 控件拖动到文档中。

    将出现“添加书签控件”对话框。

  2. 选择文字“创建一个在右击书签中的文本时出现的快捷菜单”,然后单击“确定”。

    bookmark1 便被添加到文档中。

  3. 将另一个 Bookmark 控件添加到“右击书签中的文本”文字。

    bookmark2 便被添加到文档中。

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

    “右击文本”文字会同时出现在 bookmark1 和 bookmark2 中。

在设计时向文档中添加书签时,将创建一个 Bookmark 控件。可以对一些书签事件进行编程。可以在书签的 BeforeRightClick 事件中编写代码,以便在用户右击书签中的文本时出现一个快捷菜单。

创建快捷菜单

创建快捷菜单

  1. 在“解决方案资源管理器”中,右击 ThisDocument,然后单击“查看代码”。

  2. 在类级别声明 CommandBar 变量和一个书签变量。

    Private commandBar As Office.CommandBar
    Private selectedBookmark As Microsoft.Office.Tools.Word.Bookmark
    WithEvents boldText As Office.CommandBarButton
    WithEvents ItalicText As Office.CommandBarButton
    
    private Office.CommandBar commandBar;
    private Office.CommandBarButton boldText;
    private Office.CommandBarButton italicText;
    private Microsoft.Office.Tools.Word.Bookmark selectedBookmark;
    
    const int WordTrue = -1;
    const int WordFalse = 0;
    
  3. 添加一个方法来创建菜单。

    Private Sub AddPopUp()
    
        commandBar = Application.CommandBars.Add( _
            "FormatText", Office.MsoBarPosition.msoBarPopup, , True)
    
        ' Add a button and set the style, caption, icon and tag.
        boldText = CType(commandBar.Controls.Add(1), Office.CommandBarButton)
    
        With boldText
            .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Bold"
            .FaceId = 113
            .Tag = "0"
        End With
    
        ' Add a button and set the style, caption, icon and tag.
        ItalicText = CType(commandBar.Controls.Add(1), Office.CommandBarButton)
    
        With ItalicText
            .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Italic"
            .FaceId = 114
            .Tag = "1"
        End With
    
        CType(Me.AttachedTemplate, Microsoft.Office.Interop.Word.Template).Saved = True
    
    End Sub
    
    private void AddPopUp()
    {
        commandBar = Application.CommandBars.Add("FormatText",
            Office.MsoBarPosition.msoBarPopup, missing, true);
    
        // Add a button and set the style, caption, icon and tag.
        boldText = (Office.CommandBarButton)commandBar.Controls.Add(
            1, missing, missing, missing, missing);
    
        boldText.Style = Office.MsoButtonStyle.msoButtonCaption;
        boldText.Caption = "Bold";
        boldText.FaceId = 113;
        boldText.Tag = "0";
    
        // Add a button and set the style, caption, icon and tag.
        italicText = (Office.CommandBarButton)commandBar.Controls.Add(
            1, missing, missing, missing, missing);
    
        italicText.Style = Office.MsoButtonStyle.msoButtonCaption;
        italicText.Caption = "Italic";
        italicText.FaceId = 114;
        italicText.Tag = "1";
    
        // Handle the click events with the ButtonClick procedure.
        boldText.Click += new Microsoft.Office.Core
            ._CommandBarButtonEvents_ClickEventHandler(ButtonClick);
    
        italicText.Click += new Microsoft.Office.Core
            ._CommandBarButtonEvents_ClickEventHandler(ButtonClick);
    
        ((Microsoft.Office.Interop.Word.Template)this.AttachedTemplate).Saved = true;
    }
    
  4. 从 ThisDocument 的 Startup 事件调用 AddPopup。

    Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As _
        System.EventArgs) Handles Me.Startup
    
        AddPopUp()
    End Sub
    
    private void ThisDocument_Startup(object sender, System.EventArgs e)
    {
        AddPopUp();
    }
    

设置书签中文本的格式

  1. 添加一个 ButtonClick 事件处理程序,以对书签应用格式设置。

    Private Sub ButtonClick(ByVal ctrl As Office.CommandBarButton, _
        ByRef Cancel As Boolean) Handles boldText.Click, ItalicText.Click
    
        Select Case ctrl.Caption
            Case "Bold"
                selectedBookmark.Bold = Not selectedBookmark.Bold
            Case "Italic"
                selectedBookmark.Italic = Not selectedBookmark.Italic
        End Select
    
        Me.ActiveWindow.SetFocus()
    End Sub
    
    private void ButtonClick(Microsoft.Office.Core.CommandBarButton Ctrl, 
        ref bool CancelDefault)
    {
        if (Ctrl.Caption == "Bold")
        {
            if (selectedBookmark.Bold == WordTrue)
            {
                selectedBookmark.Bold = WordFalse;
            }
            else 
            {
                selectedBookmark.Bold = WordTrue;
            }
        }
        else if (Ctrl.Caption == "Italic")
        {
            if (selectedBookmark.Italic == WordTrue)
            {
                selectedBookmark.Italic = WordFalse;
            }
            else
            {
                selectedBookmark.Italic = WordTrue;
            } 
        }
    }
    
  2. 添加一个 showPopupMenu 事件处理程序,以处理这两个书签的 BeforeRightClick 事件。

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

    必须编写代码来处理书签重叠的情况。否则,默认情况下将为所选内容中的所有书签调用代码。

    Private Sub showPopupMenu(ByVal sender As Object, ByVal e _
        As Microsoft.Office.Tools.Word.ClickEventArgs) _
        Handles Bookmark1.BeforeRightClick, Bookmark2.BeforeRightClick
    
        Dim startPosition As Integer = 0
        Dim i As Integer
    
        ' If bookmarks overlap, get bookmark closest to cursor.
        For i = 1 To e.Selection.Bookmarks.Count
            If e.Selection.Bookmarks(i).Start > startPosition Then
                startPosition = e.Selection.Bookmarks(i).Start
            End If
        Next
    
        ' If closest bookmark is the sender, show the popup.
        If DirectCast(sender, Microsoft.Office.Tools.Word.Bookmark).Start = _
            startPosition Then
            selectedBookmark = DirectCast(sender, Microsoft.Office.Tools.Word.Bookmark)
            commandBar.ShowPopup()
            e.Cancel = True
        End If
    End Sub
    
    private void showPopupMenu(object sender, 
        Microsoft.Office.Tools.Word.ClickEventArgs e)
    {
        int startPosition = 0;
    
        // If bookmarks overlap, get bookmark closest to cursor.
        for (int i = 1; i <= e.Selection.Bookmarks.Count; i++)
        {
            object o = i;
            if (e.Selection.Bookmarks.get_Item(ref o).Start > startPosition)
            {
                startPosition = e.Selection.Bookmarks.get_Item(ref o).Start;
            }
        }
    
        // If closest bookmark is the sender, show the popup.
        if (((Microsoft.Office.Tools.Word.Bookmark)sender).Start == startPosition)
        {
            selectedBookmark = (Microsoft.Office.Tools.Word.Bookmark)sender;
            commandBar.ShowPopup(missing, missing);
            e.Cancel = true;
        }
    
    
    }
    
  3. 在 C# 中,必须将书签控件的事件处理程序添加到 Startup 事件。有关创建事件处理程序的信息,请参见如何:使用 Visual Studio Tools for Office 创建事件处理程序

    this.bookmark1.BeforeRightClick += new Microsoft.Office.Tools.Word.
        ClickEventHandler(showPopupMenu);
    
    this.bookmark2.BeforeRightClick += new Microsoft.Office.Tools.Word.
        ClickEventHandler(showPopupMenu);
    

测试应用程序

对文档进行测试,以验证在右击书签中的文本时快捷菜单中是否出现“粗体”和“斜体”菜单项,同时还要验证文本格式是否正确。

测试文档

  1. 按 F5 运行项目。

  2. 右击第一个书签,然后单击“粗体”。

  3. 验证 bookmark1 中的所有文本是否都已设置为粗体格式。

  4. 右击书签重叠处的文本,然后单击“斜体”。

  5. 验证 bookmark2 中的所有文本是否为斜体,并验证与 bookmark2 重叠的 bookmark1 中是否只有部分文本为斜体。

后续步骤

下一步可能要执行以下几项任务:

请参见

概念

使用 Word 的演练

Office UI 自定义

Bookmark 控件

了解 Office 解决方案中的可选参数

其他资源

Word 宿主控件