Whether you want to add your own custom controls to the Office Fluent Ribbon, or reuse one of the many built-in controls, you use a combination of XML and programming code.
XML provides a hierarchical, declarative model of the Office Fluent Ribbon. You add controls, such as buttons and menus, to the Office Fluent Ribbon by using XML elements to specify the type of component. For example, you add a single button by using the button element. You assign property values to the button by using attributes such as the label attribute. Examine the following sample.
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" loadImage="LoadImage" >
<ribbon startFromScratch="false">
<tabs>
<tab id="tab1" label="Menu Demo" keytip="x" >
<group id="group1" label="Demo Group">
<menu id="menu1"
label="Text Handler"
itemSize="large"
getScreentip="GetScreenTip"
supertip="This is a super tip for the menu."
image="camera.bmp" >
<button id="button1"
imageMso="HappyFace"
getDescription="GetDescription"
label="My Button"
onAction="OnAction" />
<dynamicMenu id="dynamicMenu1"
label="Dynamic Menu"
getContent="GetContent" />
</menu>
</group>
</tab>
</tabs>
</ribbon>
</customUI>"
This sample adds a custom tab titled Menu Demo to the Office Fluent Ribbon by assigning text to the tab element's label attribute. This tab contains the Demo Group group, which contains a menu control named menu1. The menu control contains a custom button named button1 and a dynamic menu named dynamicMenu1.
You define properties for the controls by using attributes such as label, itemSize, and image. You assign these properties explicitly by setting the attribute equal to a string, such as the supertip attribute, or indirectly by pointing to a programming code procedure such as the getScreentip attribute. Figure 1 shows the result of applying this XML to the Office Fluent Ribbon in Microsoft Office Excel 2007.
Figure 1. Modified Ribbon Example in Office Excel 2007
Examining the other attributes of the controls, first, you assign the id attribute for the menu control. You can create custom user-defined controls on the Office Fluent Ribbon or use built-in functionality that is part of Microsoft Office. By using the id attribute, you indicate to Microsoft Office that this is a custom control. However, if you use the idMso attribute and point to the name of a built-in feature, this indicates to Microsoft Office that this control uses built-in functionality. For example, if you added a button to the Office Fluent Ribbon and set the idMso attribute equal to bold, clicking this button applies bold to whatever text you highlighted in the worksheet.
Note:
|
|
To find the name of the built-in Microsoft Office commands in Office Excel 2007, click the Microsoft Office Button, click Excel Options, click the Customize tab, and move the mouse pointer over the command. The name of the command is in parentheses.
|
Next, you explicitly set the label attribute equal to text instead of pointing the attribute to a procedure. Then set the itemsize attribute to large to indicate that size of the menu control. The choices for a control's size when set explicitly are normal and large. You can also set the size by using a callback procedure. Callback procedures are described in Assigning Functionality to Ribbon Components.
Next, you see the getScreentip attribute. ScreenTips are those small boxes that appear when you move the mouse pointer over an object on the Office Fluent Ribbon. They provide information about the object you are pointing to. Likewise, the supertip attribute (or the getSupertip attribute, if you are pointing to a callback procedure) provides additional information about the object.
Another thing to notice about the getScreentip and the supertip attributes is that whenever you see an attribute prefixed by the word get, that attribute points to a callback procedure. Therefore, in the code, the getScreentip attribute points to the callback procedure named GetScreentip.
Note:
|
|
You are not required to name callback procedures the same as the attribute with which they are used. You can just as easily use the following line of code: getScreentip="DoSomething". Also, you are not required to prefix attributes that point to callback procedures with the word get. For example, in the XML code, the onAction attribute points to a callback named OnAction. For more information about which attributes are assigned explicitly and which attributes point to callbacks, see Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3).
|
Next, the image attribute specifies a custom image for the button. Likewise, the imageMso attribute for button2 specifies a built-in image for the button. To find a spreadsheet of built-in images, see the 2007 Office System Add-In: Icons Gallery.
Inside the menu control, I defined a button named button1. After assigning the id attribute, I assigned the imageMso attribute equal to HappyFace. Because I used the imageMso attribute instead of the image attribute, the image must be one that is built into Microsoft Office.
Next, you see the getDescription attribute to which I assigned the GetDescription callback procedure. This attribute adds descriptive text when the button is displayed. Figure 1 shows the descriptive text for the button.
The onAction attribute points to a callback procedure that executes when you click the button. This procedure is discussed in the next section.
Following the button, I also added a dynamicMenu control to which I assigned three attributes. I already discussed the id and label attributes. The getContent attribute points to a callback procedure that creates a menu at run time, hence the name dynamic menu. The GetContent callback procedure is also discussed in the next section.
In the previous XML sample, some attributes point to callback procedures. For example, the button element has an onAction attribute. When the button is clicked, the OnAction method, or callback procedure, is called. The code in the OnAction method gives the button its functionality. These procedures are called callbacks because when the button is clicked, this action alerts Microsoft Office that the control needs its attention. Microsoft Office then calls back to the method defined by the onAction attribute and performs whatever action is contained in the method. The following paragraphs describe these callback procedures in more detail.
The getScreentip attribute points to the GetScreenTip callback procedure. In this example, the procedure returns a string to Microsoft Office that appears when you move the pointer over the check box.
Public Function GetScreenTip(ByVal control As IRibbonControl) As String
Return "This is a screen tip for the menu."
End Function
public string GetScreenTip(IRibbonControl control)
{
return "This is a screen tip for the menu.";
}
The GetDescription callback procedure for the button control returns a String value to Microsoft Office that displays a possible description of the control.
Public Function GetDescription(ByVal control As IRibbonControl) As String
Return "Your description here."
End Function
public string GetPressed(IRibbonControl control)
{
return "Your description here.";
}
Next comes the LoadImage callback. When the Office Fluent Ribbon is initially loaded, the procedure that the imageLoad attribute points to in the customUI element is called. Then, whenever a control's custom image needs to be displayed for a control, the custom image is loaded from the embedded resource image file.
Public Function LoadImage(ByVal imageName As String) As Bitmap
Dim assembly As Assembly = Assembly.GetExecutingAssembly()
Dim stream As Stream = assembly.GetManifestResourceStream("RibbonDemo." & imageName)
Return New Bitmap(stream)
End Function
public Bitmap LoadImage(string imageName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("RibbonDemo." + imageName);
return new Bitmap(stream);
}
The OnAction callback procedure is called when you click the button. In this example, the procedure tests the Id property of the calling control and either displays a dialog box or inserts text that is specific to that control into the A1 cell in the worksheet.
Public Sub OnAction(ByVal control As IRibbonControl)
Select Case control.Id
Case "button1" : MessageBox.Show("You clicked the Happy Face button.")
Case "button2" : applicationObject.Range("A1").Value = "You selected the Insert Text button."
Case "button3" : applicationObject.Range("A1").Value = "You selected the Insert More Text button."
End Select
End Sub
public void OnAction(IRibbonControl control, bool pressed)
{
switch (control.Id)
{
case "button1" : MessageBox.Show("You clicked the Happy Face button."); break;
case "button2" : applicationObject.Range("A1").Value = "You selected the Insert Text button."; break;
case "button3" : applicationObject.Range("A1").Value = "You selected the Insert More Text button."; break;
default: MessageBox.Show("There was a problem."); break;
}
}
The dynamicMenu control includes the getContent attribute that points to the GetContent callback procedure.
Public Function GetContent(ByVal control As IRibbonControl) As String
Dim MyStringBuilder As StringBuilder = New StringBuilder("<menu xmlns=""http://schemas.microsoft.com/office/2006/01/customui"" >")
MyStringBuilder.Append("<button id=""button2"" label=""Insert Text"" onAction=""OnAction"" imageMso=""SignatureLineInsert"" />")
MyStringBuilder.Append("<menuSeparator id=""menusep1"" getTitle=""GetTitle"" />")
MyStringBuilder.Append("<button id=""button3"" label=""Insert More Text"" onAction=""OnAction"" imageMso=""FileDocumentInspect"" />")
MyStringBuilder.Append("</menu>")
Return MyStringBuilder.ToString
End Function
public string GetContent(IRibbonControl control)
{
StringBuilder MyStringBuilder = new StringBuilder(@"<menu xmlns=""http://schemas.microsoft.com/office/2006/01/customui"" >");
MyStringBuilder.Append(@"<button id=""button1"" label=""Insert Text"" onAction=""OnAction"" imageMso=""SignatureLineInsert"" />");
MyStringBuilder.Append(@"<menuSeparator id=""menusep1"" getTitle=""GetTitle"" />");
MyStringBuilder.Append(@"<button id=""button2"" label=""Insert More Text"" onAction=""OnAction"" imageMso=""FileDocumentInspect"" />");
MyStringBuilder.Append(@"</menu>");
return MyStringBuilder.ToString();
}
This procedure uses the StringBuilder object to create a menu control as a String. The menu includes two buttons and a menuSeparator control. You can also see that I set several properties for the controls, which were discussed earlier. This control is created at run time, which means that it is not created until the menu control on the Office Fluent Ribbon is clicked.
Finally, the menuSeparator control calls the GetText callback procedure instead of explicitly assigning text to the attribute.
Public Function GetTitle(ByVal control As IRibbonControl) As String
Return "menu separator title"
End Function
public string GetTitle(IRibbonControl control)
{
return "menu separator title"
}