逐步解說:建立書籤的快速鍵功能表

更新:2007 年 11 月

適用於

本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。

專案類型

  • 文件層級專案

Microsoft Office 版本

  • Word 2007

  • Word 2003

如需詳細資訊,請參閱依應用程式和專案類型提供的功能

本逐步解說示範如何透過 Word 文件層級自訂建立 Bookmark 控制項的快速鍵功能表。當使用者以滑鼠右鍵按一下書籤中的文字時,快速鍵功能表就會顯示,提供使用者格式化文字的選項。

這個逐步解說將說明下列工作:

  • 在設計階段,透過文件層級專案將文字和書籤加入至文件。

  • 建立快速鍵功能表。

  • 檢查重疊的書籤。

注意事項:

您的電腦可能會在下列說明中,以不同名稱或位置顯示某些 Visual Studio 使用者介面項目。您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。如需詳細資訊,請參閱 Visual Studio 設定

必要條件

您需要下列元件才能完成此逐步解說:

  • Visual Studio Tools for Office (Visual Studio 2008 Professional 和 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 文件專案。

若要建立新的專案

  • 建立名為 My Bookmark Shortcut Menu 的 Word 文件專案。選取精靈中的 [建立新文件]。如需詳細資訊,請參閱 HOW TO:建立 Visual Studio Tools for Office 專案

    Visual Studio 會在設計工具中開啟新的 Word 文件,並將 [My Bookmark Shortcut Menu] 專案加入至 [方案總管]。

將文字和書籤加入至文件

將部分文字加入至文件,然後加入兩個重疊書籤。

若要將文字加入至文件

  • 輸入下列文字。

    這是您在以滑鼠右鍵按一下書籤中的文字時,建立快速鍵功能表的範例。

若要將書籤控制項加入至文件

  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++)
        {
            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 事件。如需建立事件處理常式的詳細資訊,請參閱 HOW TO:在 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 中的所有文字是斜體,以及在 bookmark1 中只有與 bookmark2 重疊的文字部分是斜體。

後續步驟

以下則是接下來的一些工作:

請參閱

概念

使用 Word 的逐步解說

Office UI 自訂

書籤控制項

了解 Office 方案中的選擇性參數

其他資源

Word 主控制項