功能区概述Ribbon overview

功能区是一种方法,以便可以更轻松地找到整理相关的命令。The ribbon is a way to organize related commands so that they are easier to find. 命令显示为功能区上的控件。Commands appear as controls on the ribbon. 控件整理为沿应用程序窗口上边缘处呈水平条。Controls are organized into groups along a horizontal strip at the top edge of an application window. 在选项卡上,相关组进行了整理。Related groups are organized on tabs.

现在可以通过使用功能区进行访问的大多数已使用 Microsoft Office system 的早期版本中的菜单和工具栏访问的功能。Most of the features that were accessed by using menus and toolbars in earlier versions of the Microsoft Office system can now be accessed by using the ribbon. 有关详细信息,请参阅技术文章2007 Microsoft Office 系统的用户界面的开发人员概述For more information, see the technical article Developer overview of the user interface for the 2007 Microsoft Office system.

适用于: 本主题中的信息适用于文档-级别项目和 VSTO 添加-的以下应用程序项目中:Excel 中;InfoPath 2013 和 InfoPath 2010;Outlook;PowerPoint;项目;Visio;Word。Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for the following applications: Excel; InfoPath 2013 and InfoPath 2010; Outlook; PowerPoint; Project; Visio; Word. 有关详细信息,请参阅按 Office 应用程序和项目类型提供的功能For more information, see Features available by Office application and project type.

自定义 Microsoft Office 功能区Customize the Microsoft Office Ribbon

若要自定义功能区,请向 Office 项目中添加以下功能区项之一:To customize the ribbon, add one of the following Ribbon items to your Office project:

  • 功能区 (可视化设计器)Ribbon (Visual Designer)

  • Ribbon (XML)Ribbon (XML)

    例如,若要自定义 Excel 功能区,则将功能区项添加到 Excel VSTO 外接程序项目中。For example, to customize the Excel Ribbon, add a Ribbon item to an Excel VSTO Add-in project.

功能区 (可视化设计器) 项Ribbon (Visual Designer) item

功能区 (可视化设计器) 项提供了高级的工具,可使你更轻松地设计和开发自定义功能区。The Ribbon (Visual Designer) item provides advanced tools that make it easier for you to design and develop a custom ribbon. 使用功能区 (可视化设计器) 要按以下方式自定义功能区项:Use the Ribbon (Visual Designer) item to customize the ribbon in the following ways:

  • 向功能区添加自定义或内置选项卡。Add custom or built-in tabs to a ribbon.

  • 将自定义组添加到自定义或内置选项卡。Add custom groups to a custom or built-in tab.

    Note

    内置选项卡或组是一个 Microsoft Office 应用程序的功能区上已存在。A built-in tab or group is one that already exists on the ribbon of a Microsoft Office application. 例如,数据选项卡是在 Excel 中的内置选项卡。For example, the Data tab is a built-in tab in Excel. 连接上的组是内置组数据选项卡。The Connections group is a built-in group on the Data tab.

  • 将自定义控件添加到自定义组。Add custom controls to a custom group.

  • 将自定义控件添加到 Backstage 视图。Add custom controls to the Backstage View.

    有关如何使用自定义功能区的详细信息功能区 (可视化设计器) 项,请参阅功能区设计器For more information about how to customize a ribbon by using the Ribbon (Visual Designer) item, see Ribbon designer.

功能区 (XML) 项Ribbon (XML) item

使用功能区 (XML) 项,如果你想要自定义功能区中不支持一种方法功能区 (可视化设计器) 项。Use the Ribbon (XML) item if you want to customize the ribbon in a way that is not supported by the Ribbon (Visual Designer) item. 使用功能区 (XML) 要按以下方式自定义功能区项:Use the Ribbon (XML) item to customize the ribbon in the following ways:

  • 添加内置到自定义选项卡或内置选项卡组。Add built-in groups to a custom tab or built-in tab.

  • 将内置控件添加到自定义组。Add built-in controls to a custom group.

  • 添加自定义代码,以替代内置控件的事件处理程序。Add custom code to override the event handlers of built-in controls.

  • 自定义“快速访问工具栏”。Customize the Quick Access Toolbar.

  • 通过使用限定 ID,在 VSTO 外接程序之间共享功能区自定义项。Share a Ribbon customization between VSTO Add-in by using a qualified ID.

    有关如何使用自定义功能区的详细信息功能区 (XML) 项,请参阅功能区 XMLFor more information about how to customize the ribbon by using the Ribbon (XML) item, see Ribbon XML.

将功能区从功能区设计器导出到功能区 XMLExport a ribbon from the Ribbon Designer to Ribbon XML

如果你通过使用功能区设计器中,创建一个功能区,然后决定你想要自定义功能区中的方法的功能区 (可视化设计器) 项不支持,可以将功能区导出到 XML。If you create a ribbon by using the Ribbon Designer, and then decide that you want to customize the ribbon in ways that the Ribbon (Visual Designer) item does not support, you can export the ribbon to XML.

Visual Studio 自动创建功能区 (XML) 项,然后填充功能区上的每个控件的元素和属性的功能区 XML 文件。Visual Studio automatically creates a Ribbon (XML) item and populates the Ribbon XML file with elements and attributes for each control on the ribbon.

并非所有属性中的属性的功能区设计器窗口被传输到功能区 XML 文件。Not all of the properties that are in the Properties window of the Ribbon Designer are transferred to the Ribbon XML file. 例如,Visual Studio 不会导出的值图像文本属性。For example, Visual Studio does not export the value of the Image or Text property. 这是因为你必须在导出项目的功能区代码文件中创建一个回调方法,以分配图像或设置控件的文本。That is because you must create a callback method in the Ribbon code file of the exported project to assign an image or set the text of a control. 在导出过程中,Visual Studio 不会自动生成回调方法。Visual Studio does not automatically generate callback methods as part of the export process.

此外,任何未更改的默认属性值都不会出现在生成的功能区 XML 文件中。In addition, any unchanged default property values do not appear in the resulting Ribbon XML file.

有关如何将功能区导出到 XML 的详细信息,请参阅如何:将功能区从功能区设计器导出到功能区 XMLFor more information about how to export the Ribbon to XML, see How to: Export a ribbon from the Ribbon Designer to Ribbon XML.

更新代码Update the code

新的功能区代码文件添加到解决方案资源管理器A new Ribbon code file is added to Solution Explorer. 此文件包含功能区 XML 类。This file contains the Ribbon XML class. 必须在此类的 Ribbon Callbacks 区域中创建回调方法,以处理用户操作(例如单击某个按钮)。You must create callback methods in the Ribbon Callbacks region of this class to handle user actions, such as clicking a button. 将你的代码从事件处理程序移动到这些回调方法,并修改此代码以使用功能区扩展性 (RibbonX) 编程模型。Move your code from the event handlers to these callback methods and modify the code to work with the Ribbon extensibility (RibbonX) programming model. 有关更多信息,请参见 Ribbon XMLFor more information, see Ribbon XML.

还必须将代码添加到替代 CreateRibbonExtensibilityObject 方法并将功能区 XML 类返回到 Office 应用程序的 ThisAddInThisWorkbookThisDocument 类。You must also add code to the ThisAddIn, ThisWorkbook, or ThisDocument class that overrides the CreateRibbonExtensibilityObject method and returns the Ribbon XML class to the Office application.

有关更多信息,请参见 Ribbon XMLFor more information, see Ribbon XML.

将多个功能区项添加到项目Add multiple Ribbon items to a project

可以将多个功能区项添加到单个项目中。You can add more than one Ribbon item to a single project. 如果想要执行以下两项任务之一,这会非常有用:This is useful if you want to perform either of the following two tasks:

  • 为 Outlook 创建功能区检查器Create ribbons for Outlook Inspectors. 有关详细信息,请参阅为 Outlook 中自定义功能区For more information, see Customize a ribbon for Outlook.

    Note

    检查器是当用户执行某些任务时(例如,创建一封电子邮件时)打开的窗口。An Inspector is a window that opens when users perform certain tasks, such as creating an e-mail message.

  • 选择要在运行时显示的功能区。Select which ribbon to display at runtime.

选择要在运行时显示的功能区Select which ribbons to display at runtime

由于一个项目可以包含多个功能区,您可以选择要在运行时显示的功能区。Because a project can contain more than one ribbon, you can select which ribbon to display at runtime.

若要选择功能区显示在运行时,重写CreateRibbonExtensibilityObject中的方法ThisAddinThisWorkbook,或ThisDocument的项目,并返回想要显示的功能区类。To select a ribbon to display at runtime, override the CreateRibbonExtensibilityObject method in the ThisAddin, ThisWorkbook, or ThisDocument class of your project and return the ribbon that you want to display. 以下示例检查名为的字段的值myCondition,并返回相应的功能区。The following example checks the value of a field named myCondition and returns the appropriate ribbon.

Note

在此示例中所使用的语法返回已通过使用功能区功能区 (可视化设计器) 项。The syntax used in this example returns a ribbon that was created by using the Ribbon (Visual Designer) item. 返回通过使用创建的功能区的语法功能区 (XML) 项会略有不同。The syntax for returning a ribbon that is created by using a Ribbon (XML) item is slightly different. 返回有关详细信息功能区 (XML) 项,请参阅功能区 XMLFor more information about returning a Ribbon (XML) item, see Ribbon XML.

添加以下代码:Add the following code:

Protected Overrides Function CreateRibbonExtensibilityObject() As  _
    Microsoft.Office.Core.IRibbonExtensibility
    If myCondition = True Then
        Return Globals.Factory.GetRibbonFactory().CreateRibbonManager _
            (New Microsoft.Office.Tools.Ribbon.IRibbonExtension() _
                 {New Ribbon1()})
    Else
        Return Globals.Factory.GetRibbonFactory().CreateRibbonManager _
            (New Microsoft.Office.Tools.Ribbon.IRibbonExtension() _
                 {New Ribbon2()})
    End If
End Function
    protected override Microsoft.Office.Core.IRibbonExtensibility
CreateRibbonExtensibilityObject()
    {
        if (myCondition == true)
        {
            return Globals.Factory.GetRibbonFactory().CreateRibbonManager(
                new Microsoft.Office.Tools.Ribbon.IRibbonExtension[] { new Ribbon1() });
        }
        else
        {
            return Globals.Factory.GetRibbonFactory().CreateRibbonManager(
                new Microsoft.Office.Tools.Ribbon.IRibbonExtension[] { new Ribbon2() });
        }
    }
标题Title 说明Description
如何:开始自定义功能区How to: Get started customizing the ribbon 演示如何自定义 Microsoft Office 应用程序的功能区中,添加功能区 (可视化设计器)功能区 (XML) 到 Office 项目的项。Shows you how to customize the ribbon of a Microsoft Office application, add a Ribbon (Visual Designer) or Ribbon (XML) item to an Office project.
功能区设计器Ribbon designer 介绍如何使用功能区设计器将自定义选项卡、 组和控件添加到 Microsoft Office 应用程序的功能区。Describes how you can use the Ribbon Designer to add custom tabs, groups, and controls to the ribbon of a Microsoft Office application.
演练:使用功能区设计器创建自定义选项卡Walkthrough: Create a custom tab by using the Ribbon Designer 显示如何通过使用功能区设计器创建自定义功能区选项卡。Shows you how to create a custom Ribbon tab by using the Ribbon Designer. 可使用功能区设计器将控件添加和放置到自定义选项卡上。You can use the Ribbon Designer to add and position controls on the custom tab.
功能区对象模型概述Ribbon object model overview 概述可用于获取和设置在运行时的功能区控件的属性的强类型化的对象模型。Provides an overview of the strongly typed object model that you can use to get and set the properties of Ribbon controls at runtime.
演练:更新在运行时功能区上的控件Walkthrough: Update the controls on a ribbon at runtime 演示如何使用功能区对象模型在功能区加载到 Office 应用程序之后,更新该功能区上的控件。Demonstrates how to use the Ribbon object model to update the controls on a ribbon after the ribbon is loaded into the Office application.
为 Outlook 中自定义功能区Customize a ribbon for Outlook 提供自定义 Microsoft Office Outlook 中的功能区的指南。Provides guidance for customizing the ribbon in Microsoft Office Outlook.
自定义 InfoPath 功能区Customize a ribbon for InfoPath 提供自定义 Microsoft Office InfoPath 中的功能区的指南。Provides guidance for customizing the ribbon in Microsoft Office InfoPath.
在运行时在功能区的访问Access the ribbon at runtime 演示如何显示、 隐藏和修改功能区中,和使用户能够从自定义任务窗格、 操作窗格或 Outlook 窗体区域中的控件运行代码。Shows how to show, hide, and modify the ribbon, and enable users to run the code from controls in a custom task pane, actions pane, or Outlook form region.
如何:更改功能区上选项卡的位置How to: Change the position of a tab on the ribbon 演示如何更改功能区上选项卡的顺序。Shows how to change the order of tabs on a ribbon.
如何:自定义内置选项卡How to: Customize a built-in tab 显示如何将组和控件添加到内置选项卡。Shows how to add groups and controls to a built-in tab.
如何:将控件添加到 Backstage 视图How to: Add controls to the Backstage View 演示如何将控件添加到单击时打开的菜单文件Shows how to add controls to the menu that opens when you click the File.
如何:将对话框启动器添加到功能区组How to: Add a dialog box launcher to a Ribbon group 说明了如何将对话框启动器添加到功能区上的任何组。Shows to add a dialog box launcher to any group on a ribbon.
如何:将功能区从功能区设计器导出到功能区 XMLHow to: Export a ribbon from the Ribbon Designer to Ribbon XML 演示如何以高级方式将功能区从设计器导出到功能区 XML 自定义功能区。Shows how to customize the ribbon in advanced ways by exporting the ribbon from the designer to Ribbon XML.
Ribbon XMLRibbon XML 介绍如何通过使用功能区 XML 自定义功能区。Explains how you can customize a ribbon by using Ribbon XML.
演练:使用功能区设计器创建自定义选项卡Walkthrough: Create a custom tab by using the Ribbon Designer 演示如何通过创建自定义功能区选项卡功能区 (XML) 项。Demonstrates how to create a custom Ribbon tab by using the Ribbon (XML) item.