菜單提供給用戶可供從中選取的預定義選項列表,與dropDown控件一樣,能夠組合圖像和文本。
表面上,菜單的用途與下拉控件的用途非常相似。然而,下拉控件的一個主要局限是僅能包含“項目”,而菜單控件可以包含各種各樣的其它控件,包括按鈕、復選框、庫、甚至是另一個菜單。此外,下拉控件的默認顯示是一個空框,而菜單可以設置一張“臉”,而不會依賴出現(xiàn)在其列表中的實際項目,因此菜單控件能夠給人良好的印象。
1、menu元素必需的屬性
菜單控件需要下表1所列的id屬性之一。
表1:menu元素必需的屬性
屬性
何時使用
id
當創(chuàng)建自已的菜單時
idMso
當使用現(xiàn)有的Microsoft菜單時
idQ
當在命名空間之間創(chuàng)建共享的菜單時
2、帶有回調(diào)簽名的可選的靜態(tài)屬性和動態(tài)屬性
菜單控件可以使用下表2列出的任一insert屬性。
表2:menu元素可選的insert屬性
INSERT屬性
允許值
默認值
何時使用
insertAfterMso
有效的Mso組
在組末尾插入
在Microsoft控件之后插入
insertBeforeMso
有效的Mso組
在組末尾插入
在Microsoft控件之前插入
insertAfterQ
有效的組idQ
在組末尾插入
在共享的命名空間控件之后插入
insertBeforeQ
有效的組idQ
在組末尾插入
在共享的命名空間控件之前插入
也可以為菜單控件設置下表3所列的任何靜態(tài)屬性或等價的動態(tài)屬性。
表3:menu元素可選的屬性和回調(diào)
靜態(tài)屬性
動態(tài)屬性
允許值
默認值
動態(tài)屬性的VBA回調(diào)簽名
description
getDescription
1至1024個字符
(none)
Sub GetDescription(control As
IRibbonControl,
ByRef returnedVal)
enabled
getEnabled
true,false,1,0
true
Sub GetEnabled(control
As IRibbonControl,
ByRef returnedVal)
image
getImage
1至1024個字符
(none)
Sub GetImage(control As
IRibbonControl,
ByRef returnedVal)
imageMso
getImage
1至1024個字符
(none)
同上
itemSize
(none)
normal,large
normal
(none)
keytip
getKeytip
1至3個字符
(none)
Sub GetKeytip(control As
IRibbonControl,
ByRef returnedVal)
label
getLabel
1至1024個字符
(none)
Sub GetLabel(control As
IRibbonControl,
ByRef returnedVal)
screentip
getScreentip
1至1024個字符
(none)
Sub GetScreentip(control
As IRibbonControl,
ByRef returnedVal)
showImage
getShowImage
true,false,1,0
true
Sub GetShowImage(control
As IRibbonControl,
ByRef returnedVal)
showLabel
getShowLabel
true,false,1,0
true
Sub GetShowLabel(control As
IRibbonControl,
ByRef returnedVal)
size
getSize
normal,large
normal
Sub GetSize(control As
IRibbonControl,
ByRef returnedVal)
supertip
getSupertip
1至1024個字符
(none)
Sub GetSupertip(control
As IRibbonControl,
ByRef returnedVal)
tag
(none)
1至1024個字符
(none)
(none)
visible
getVisible
true,false,1,0
true
Sub GetVisible(control As
IRibbonControl,
ByRef returnedVal)
3、menu元素允許的子對象
menu元素接受下列子對象的任意組合:
n button
n checkbox
n control
n dynamicMenu
n gallery
n menu
n menuSeparator
n splitButton
n toggleButton
4、menu元素的父對象
在下列任何控件內(nèi)都能放置menu控件:
n box
n buttonGroup
n dynamicMenu
n group
n menu
n officeMenu
n splitButton
5、使用內(nèi)置的菜單控件
(1)創(chuàng)建一個新的.xlsx文件,并將其保存為Excel Built In menu Example.xlsx。
(2)關閉該文件并在CustomUI Editor中打開。
(3)輸入下列XML代碼:
<customUI xmlns=“http://schemas.microsoft.com/office/2006/01/customui“>
<ribbon startFromScratch=“false“>
<tabs>
<tab id=“txtabDemo“
label=“Demo“
insertBeforeMso=“TabHome“>
<group id=“rxgrpDemo“
label=“Demo“>
<menu idMso=“FilePrepareMenu“
size=“large“/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
下圖為上述代碼創(chuàng)建的組。
6、創(chuàng)建自定義菜單控件
本例中,創(chuàng)建一個菜單,包含一些有用的網(wǎng)站鏈接,并且將在名為“Ribbon Help”且放置在“開發(fā)工具”選項卡末尾的自定義組中包含該菜單。
(1)新建一個工作簿,以.xlsm為擴展名保存后關閉。
(2)在CustomUI Editor中打開該工作簿,并輸入下列XML代碼:
<customUI xmlns=“http://schemas.microsoft.com/office/2006/01/customui“>
<ribbon startFromScratch=“false“>
<tabs>
<tab idMso=“TabDeveloper“>
<group id=“rxgrpRibbonHelp“
label=“Ribbon Help“>
<menu id=“munResources“
imageMso=“HyperlinkInsert“
size=“large“
label=“Useful Links“>
<menuSeparator id=“rxmSepRibbon“
title=“RibbonX Resources“/>
<button id=“rxbtnMSDN“
label=“MSDN Ribbon Developer Centre“
onAction=“rxsharedLinks_click“
tag=“http://msdn2.microsoft.com/en-us/office/aa905530.aspx“/>
<button id=“rxbtnKenPuls“
label=“The Ken Puls blog (Excel MVP)“
onAction=“rxsharedLinks_click“
tag=“http://www.excelguru.ca/blog/2006/12/01/ribbon-example-table-of-contents“/>
<button id=“rxbtnRondeBruin“
label=“Ron deBruin’s Site (Excel MVP)“
onAction=“rxsharedLinks_click“
tag=“http://www.rondebruin.nl/ribbon.htm“/>
<button id=“rxbtnAccessFreak“
label=“Access Freak (Access MVP)“
onAction=“rxsharedLinks_click“
tag=“http://www.access-freak.com“/>
<button id=“rxbtnPatrickSchmid“
label=“Patrick Schmid’s RibbonX Forum“
onAction=“rxsharedLinks_click“
tag=“http://pschmid.net/office2007/forums“/>
<menuSeparator id=“rxmSepAuthors“
title=“Authors Sites“/>
<button id=“rxbtnAuthorRobertMartin“
label=“Robert Martin“
onAction=“rxsharedLinks_click“
tag=“http://www.msofficegurus.com“/>
<button id=“rxbtnAuthorKenPuls“
label=“Ken Puls“
onAction=“rxsharedLinks_click“
tag=“http://www.excelguru.ca/“/>
<button id=“rxbtnAuthorTeresaHenning“
label=“Teresa Henning“
onAction=“rxsharedLinks_click“
tag=“http://www.DataDyanmicsNW.com/“/>
<button id=“rxbtnExcelPerfect“
label=“Excel Perfect“
onAction=“rxsharedLinks_click“
tag=“http://www.excelperfect.com/“/>
</menu>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
代碼創(chuàng)建了一個菜單,列出了九個網(wǎng)站。menuSeparator有效地將這些網(wǎng)站分成了兩組。
注意,我們?yōu)槊總€menu控件使用了相同的回調(diào),這使得VBA代碼非常簡潔。同時,我們將實際的URLs存儲在tag屬性中。
(3)生成回調(diào)簽名,并復制代碼后關閉CustomUI Editor。
(4)在Excel中打開該工作簿,打開VBE并將回調(diào)簽名代碼粘貼到一個標準模塊中。下面編輯該回調(diào)以響應希望的操作。
‘rxbtnMSDN onAction回調(diào)
Sub rxsharedLinks_click(control As IRibbonControl)
ActiveWorkbook.FollowHyperlink Address:=control.Tag, NewWindow:=True
End Sub
代碼在新的Web窗口中啟動鏈接。當觸發(fā)回調(diào)時,control對象被傳遞到過程。該對象具有三個屬性:context,id,tag?;卣{(diào)通常查詢該控件的ID并相應地作出響應,但在本例中,查詢標簽,因為標簽中存儲了URL。接著鏈接被提供給單擊的控件,啟動合適的鏈接。
現(xiàn)在,就可以測試了。切換到Excel界面,單擊“開發(fā)工具”選項卡,在末尾有一個“Useful Links”的菜單,試著單擊其中的菜單項。
下面,再看一個示例。本示例在自定義的菜單中組合一些內(nèi)置控件以方便使用,其XML代碼如下:
<customUI xmlns=“http://schemas.microsoft.com/office/2006/01/customui“>
<ribbon startFromScratch=“false“>
<tabs>
<tab id=“rxtabDemo“
label=“Demo“
insertBeforeMso=“TabHome“>
<group id=“rxgrpViews“
label=“Document Views“>
<menu id=“rxmnuViewsMenu“
itemSize=“normal“
imageMso=“FilePrintPreview“
label=“Views“
size=“large“>
<toggleButton idMso=“ViewPageBreakPreviewView“/>
<toggleButton idMso=“ViewFullScreenView“/>
<checkBox idMso=“ViewHeadings“/>
<checkBox idMso=“ViewFormulaBar“/>
<checkBox idMso=“GridlinesExcel“/>
</menu>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Excel中的自定義界面如下圖: