Office 2007 Ribbon开发
首先,下载和安装VSTO,这是VS2005开发Office Add-In的扩展。
安装后打开VS2005创建Office Add-In 项目,这里我选择一个Word的Add-in:
然后为这个Add-In项目添加一个Ribbon:
VS中生成两个文件Ribbon1.cs和Ribbon1.xml,xml文件是对Ribbon描述:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad">
<ribbon>
<tabs>
<tab idMso="TabAddIns">
<group id="MyGroup"
label="My Group">
<toggleButton id="toggleButton1"
size="large"
label="My Button"
screentip="My Button Screentip"
onAction="OnToggleButton1"
imageMso="HappyFace" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
<ribbon>
<tabs>
<tab idMso="TabAddIns">
<group id="MyGroup"
label="My Group">
<toggleButton id="toggleButton1"
size="large"
label="My Button"
screentip="My Button Screentip"
onAction="OnToggleButton1"
imageMso="HappyFace" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
这个文件产生的效果是这样的:
将Ribbon1.cs文件中的这段代码取消注释:
public partial class ThisAddIn
{
private Ribbon1 ribbon;
protected override object RequestService(Guid serviceGuid)
{
if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID)
{
if (ribbon == null)
ribbon = new Ribbon1();
return ribbon;
}
return base.RequestService(serviceGuid);
}
}
{
private Ribbon1 ribbon;
protected override object RequestService(Guid serviceGuid)
{
if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID)
{
if (ribbon == null)
ribbon = new Ribbon1();
return ribbon;
}
return base.RequestService(serviceGuid);
}
}
按F5即可打开Word2007并运行这个AddIn。再看xml文件就明白了,一个Tab下面一个Group,Group中一个toggle button,当然也可以存在多个Tab,Tab下也可以有多个Group,一个Group下也可以存在多个控件,只要在这个xml文件中进行描述就可以了。点击这个按钮会弹出对话框,这个动作是由回调函数OnToggleButton1完成的,相当于Click事件,在Ribbon1.cs文件中可以找到这个函数:
public void OnToggleButton1(Office.IRibbonControl control, bool isPressed)
{
if (isPressed)
MessageBox.Show("Pressed");
else
MessageBox.Show("Released");
}
Xml文件中{
if (isPressed)
MessageBox.Show("Pressed");
else
MessageBox.Show("Released");
}
onAction="OnToggleButton1"
注册了这个回调函数。另外还有很多其他的控件:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad">
<ribbon>
<tabs>
<tab idMso="TabAddIns" label="MyRibbon">
<group id="ContentGroup" label="Content">
<button id="textButton" label="Insert Text"
screentip="Text" onAction="OnTextButton"
supertip="Inserts text at the cursor location."/>
<button id="tableButton" label="Insert Table"
screentip="Table" onAction="OnTableButton"
supertip="Inserts a table at the cursor location."/>
<toggleButton id="toggleButton1"
size="large"
label="My Button"
screentip="My Button Screentip"
onAction="OnToggleButton1"
imageMso="HappyFace" />
<checkBox id="checkbox1"
enabled="true"
getLabel="GetLabel"
keytip="A1"
getScreentip="GetScreentip"
supertip="This is a super tip for the checkBox."
visible="true"
getPressed="GetPressed"
onAction="OnCheckBoxAction" />
<dialogBoxLauncher>
<button id="button2"
screentip=
"Launched by the DialogBoxLauncher
control."
onAction="dialogBoxLauncherOnAction" />
</dialogBoxLauncher>
</group>
<group id="ContentGroup1" label="Content">
<editBox id="EditBox1" getText="ThisDocument.MyTextMacro"
label="My EditBox" onChange="ThisDocument.MyEditBoxMacro"/>
<comboBox id="Combo1" label="My ComboBox"
onChange="ThisDocument.MyComboBoxMacro">
<item id="Zip1" label="33455" />
<item id="Zip2" label="81611" />
<item id="Zip3" label="31561" />
</comboBox>
<comboBox id="comboBox2"
label="Insert More Text."
getText="GetText"
imageMso="TableDrawTable" />
<menu id="mnuProjectsReports" label="More Reports"
imageMso ="ViewsReportView" itemSize="large" >
<button id="cmdProjectsCompletedAndDeferred" label="Completed and Deferred Projects"
imageMso="ViewsReportView" onAction="Ribbon.ProjectsCompletedAndDeferred"/>
<button id="cmdProjectsBalanceSheet" label="Project Balance Sheet"
imageMso="ViewsReportView" onAction="Ribbon.ProjectsBalanceSheet"/>
</menu>
<button id="cmdProjectsCommonDeliverables" label="Common Deliverables"
imageMso="ReviewShareWorkbook" size="large" onAction="Ribbon.ProjectsCommonDeliverables"/>
<dialogBoxLauncher>
<button id="button3" screentip="show task pane." onAction="dialogBoxLauncherOnAction1" />
</dialogBoxLauncher>
</group>
<group id="ContentGroup2" label="SplitButton">
<splitButton id="cmdVendorSplitButton" size="large" >
<menu id="mnuVendor" imageMso ="RecordsAddFromOutlook" itemSize="large" >
<button id="cmdVendorList" label="Vendor List" imageMso="RecordsAddFromOutlook"
onAction="Ribbon.VendorList"/>
<button id="cmdVendorAddressBook" label="Address Book" imageMso="ViewsReportView"
onAction="Ribbon.VendorAddressBook"/>
<button id="cmdVendorPhoneList" label="Phone List" imageMso="ViewsReportView"
onAction="Ribbon.VendorPhoneList"/>
</menu>
</splitButton>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
<ribbon>
<tabs>
<tab idMso="TabAddIns" label="MyRibbon">
<group id="ContentGroup" label="Content">
<button id="textButton" label="Insert Text"
screentip="Text" onAction="OnTextButton"
supertip="Inserts text at the cursor location."/>
<button id="tableButton" label="Insert Table"
screentip="Table" onAction="OnTableButton"
supertip="Inserts a table at the cursor location."/>
<toggleButton id="toggleButton1"
size="large"
label="My Button"
screentip="My Button Screentip"
onAction="OnToggleButton1"
imageMso="HappyFace" />
<checkBox id="checkbox1"
enabled="true"
getLabel="GetLabel"
keytip="A1"
getScreentip="GetScreentip"
supertip="This is a super tip for the checkBox."
visible="true"
getPressed="GetPressed"
onAction="OnCheckBoxAction" />
<dialogBoxLauncher>
<button id="button2"
screentip=
"Launched by the DialogBoxLauncher
control."
onAction="dialogBoxLauncherOnAction" />
</dialogBoxLauncher>
</group>
<group id="ContentGroup1" label="Content">
<editBox id="EditBox1" getText="ThisDocument.MyTextMacro"
label="My EditBox" onChange="ThisDocument.MyEditBoxMacro"/>
<comboBox id="Combo1" label="My ComboBox"
onChange="ThisDocument.MyComboBoxMacro">
<item id="Zip1" label="33455" />
<item id="Zip2" label="81611" />
<item id="Zip3" label="31561" />
</comboBox>
<comboBox id="comboBox2"
label="Insert More Text."
getText="GetText"
imageMso="TableDrawTable" />
<menu id="mnuProjectsReports" label="More Reports"
imageMso ="ViewsReportView" itemSize="large" >
<button id="cmdProjectsCompletedAndDeferred" label="Completed and Deferred Projects"
imageMso="ViewsReportView" onAction="Ribbon.ProjectsCompletedAndDeferred"/>
<button id="cmdProjectsBalanceSheet" label="Project Balance Sheet"
imageMso="ViewsReportView" onAction="Ribbon.ProjectsBalanceSheet"/>
</menu>
<button id="cmdProjectsCommonDeliverables" label="Common Deliverables"
imageMso="ReviewShareWorkbook" size="large" onAction="Ribbon.ProjectsCommonDeliverables"/>
<dialogBoxLauncher>
<button id="button3" screentip="show task pane." onAction="dialogBoxLauncherOnAction1" />
</dialogBoxLauncher>
</group>
<group id="ContentGroup2" label="SplitButton">
<splitButton id="cmdVendorSplitButton" size="large" >
<menu id="mnuVendor" imageMso ="RecordsAddFromOutlook" itemSize="large" >
<button id="cmdVendorList" label="Vendor List" imageMso="RecordsAddFromOutlook"
onAction="Ribbon.VendorList"/>
<button id="cmdVendorAddressBook" label="Address Book" imageMso="ViewsReportView"
onAction="Ribbon.VendorAddressBook"/>
<button id="cmdVendorPhoneList" label="Phone List" imageMso="ViewsReportView"
onAction="Ribbon.VendorPhoneList"/>
</menu>
</splitButton>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
这里可以使用很多的回调函数,比如Combox下的GetLable,GetScreentip,GetPressed,可以让我们在程序中去定制显示内容.
Ribbon开发的相关资料可以从这里查找,微软同样也给出了部分代码实例。