RibbonButton 类 (2007 system)

更新:2007 年 11 月

表示自定义功能区上的按钮。

命名空间:  Microsoft.Office.Tools.Ribbon
程序集:  Microsoft.Office.Tools.Common.v9.0(在 Microsoft.Office.Tools.Common.v9.0.dll 中)

语法

声明
<ToolboxBitmapAttribute(GetType(RibbonButton), "RibbonButton.bmp")> _
Public NotInheritable Class RibbonButton _
    Inherits RibbonControl
用法
Dim instance As RibbonButton
[ToolboxBitmapAttribute(typeof(RibbonButton), "RibbonButton.bmp")]
public sealed class RibbonButton : RibbonControl

备注

在将“Button”控件从“工具箱”的“Office 功能区控件”选项卡拖入功能区设计器时,Visual Studio Tools for Office 将创建 RibbonButton 类的实例。

您还可以在运行时创建此控件的实例并将它添加到动态菜单。如果将菜单的 Dynamic 属性设置为 true,则该菜单是动态菜单。

此控件的某些属性只能在运行时设置,并且只能在功能区加载到 Office 应用程序之前,或者该控件添加到动态菜单之前进行设置。有关如何设置这些属性的信息,请参见功能区对象模型概述

常见任务

下表列出了对常见任务有用的成员。

任务

成员

在运行时,将按钮添加到其 Dynamic 属性设置为 true 的菜单。

使用 RibbonButton 构造函数创建 RibbonButton 类的实例。

将此实例添加到菜单的 Items 集合。

更改按钮的大小。

ControlSize 设置为所需的值。例如,对于大按钮,将其设置为 Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge

在按钮上显示图像。

有三种显示图像的方式:

  • 使用 Image 属性。此属性提供了最大控制。

  • OfficeImageId 设置为您要将其显示为按钮图标的内置 Microsoft Office 图像的 ID。

  • 可将 ImageName 属性与 LoadImage 事件一起使用。此属性会带来一些性能好处。

禁用或隐藏按钮。

若要禁用按钮,请将 Enabled 设置为 false。

若要隐藏按钮,请将 Visible 设置为 false。

在用户单击按钮时运行代码。

将代码添加到 Click 事件处理程序。

示例

下面的示例创建一个新按钮,设置该按钮的各种属性,然后将该按钮添加到菜单中。若要运行此代码示例,必须先执行下列步骤:

  1. 将一个“功能区(可视化设计器)”项添加到 Visual Studio Tools for Office 项目。

  2. 在自定义选项卡中添加一个组。

  3. 将一个菜单添加到该组。

  4. 将该菜单的“Dynamic”属性设置为 true。

  5. 将该菜单的“ItemSize”属性设置为“RibbonControlSizeLarge”。

Private Sub AddButtonsToMenu()
    Dim tempButton As New RibbonButton()
    tempButton.Label = "Button 1"
    tempButton.ControlSize = _
        Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge
    tempButton.Description = "My Ribbon Button"
    tempButton.ShowImage = True
    tempButton.ShowImage = True
    tempButton.Image = My.Resources.Image1
    tempButton.Image = My.Resources.Image2
    tempButton.KeyTip = "A1"
    menu1.Items.Add(tempButton)
End Sub
private void AddButtonsToMenu()
{
    RibbonButton tempButton = new RibbonButton();
    tempButton.Label = "Button 1";
    tempButton.ControlSize = 
        Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;
    tempButton.Description = "My Ribbon Button";
    tempButton.ShowImage = true;
    tempButton.ShowImage = true;
    tempButton.Image = Properties.Resources.Image1;
    tempButton.Image = Properties.Resources.Image2;
    tempButton.KeyTip = "A1";
    menu1.Items.Add(tempButton);

}

继承层次结构

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      Microsoft.Office.Tools.Ribbon.RibbonComponent
        Microsoft.Office.Tools.Ribbon.RibbonControl
          Microsoft.Office.Tools.Ribbon.RibbonButton

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

另请参见

参考

RibbonButton 成员

Microsoft.Office.Tools.Ribbon 命名空间

RibbonMenu

RibbonButton

其他资源

功能区概述

功能区设计器

功能区对象模型概述

如何:开始自定义功能区