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

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

**适用于:**本主题中的信息适用于 Word 2007 和 Word 2010 的文档级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能

本演练阐释了以下任务:

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

  • 创建一个快捷菜单。

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

提示

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

系统必备

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

-

Visual Studio 2010 的一个版本,其中包含 Microsoft Office 开发工具。有关更多信息,请参见[将计算机配置为开发 Office 解决方案](bb398242\(v=vs.100\).md)。
  • Microsoft Office Word 2007 或 Word 2010。

创建项目

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

创建新项目

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

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

向文档中添加文本和书签

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

向文档中添加文本

  • 键入以下文本。

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

向文档中添加 Bookmark 控件

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

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

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

    bookmark1 便被添加到文档中。

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

    bookmark2 便被添加到文档中。

    提示

    “右击文本”文字会同时出现在 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 事件。

    提示

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

    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++)
        {
            if (e.Selection.Bookmarks[i].Start > startPosition)
            {
                startPosition = e.Selection.Bookmarks[i].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 事件。 有关创建事件处理程序的信息,请参见如何:在 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 的演练

使用扩展对象实现 Word 自动化

Bookmark 控件

Office 解决方案中的可选参数

其他资源

Office UI 自定义