细品RibbonX(26):库(Gallery)控件详解

细品RibbonX(26):库(Gallery)控件详解

资料整理来自于论坛
完整版下载地址:http://download.csdn.net/download/nodeman/10264659

 Loading ...

 

下面介绍如何使用库,Office 2007用户界面中最酷的新功能之一。

库被设计作为图形化显示用户选项的一种方式,例如样式库,能够通过查看该样式图形化的呈现使用户选择一种样式。可以使用库来组织照片、访问样式、或者存储调色板。在需要并提供可视效果的定制中,库是完美的控件。

此外,在库的底部也可以添加按钮。

库的XML标记如下:

<gallery>

    <!—放置XML代码>

</gallery>

库由静态的、动态的和可选的子属性组成,需要下表1所示的静态属性。

表1:库的静态属性

静态属性

允许值

VBA回调签名

columns

1至1024

N/A

itemHeight

1至4096

Sub getItemHeight(control As IRibbonControl,

ByRef height)

itemWidth

1至4096

Sub getItemWidth(control As IRibbonControl,

ByRef width)

rows

1至1024

N/A

sizeString

1至1024个字符

N/A

showItemImage

true,false,1,0

N/A

showItemLabel

true,false,1,0

N/A

库也可以包含下表2所示的动态属性。

表2:库的动态属性

动态属性

允许值

VBA回调签名

getItemCount

0至1000

Sub GetItemCount(control As IRibbonControl,

ByRef count)

getItemID

 

Sub GetItemID(control As IRibbonControl,

index As Integer,

ByRef id)

getItemImage

 

Sub GetItemImage(control As IRibbonControl,

index As Integer,

ByRef image)

getItemLabel

1024

Sub GetItemLabel(control As IRibbonControl,

index As Integer,

ByRef label)

getItemScreentip

1024

Sub GetItemScreenTip(control As IRibbonControl,

index As Integer,

ByRef screentip)

getItemSupertip

1024

Sub GetItemSuperTip(control As IRibbonControl,

index As Integer,

ByRef supertip)

getSelectedItemID

1至1024个字符

Sub GetSelectedItemID(control As IRibbonControl,

ByRef index)

getSelectedItemIndex

1至1024

Sub GetSelectedItemIndex(control As IRibbonControl,

ByRef index)

onAction

 

Sub OnAction(control As IRibbonControl,

selectedId As String,

selectedIndex As Integer)

库还能够接受下表3所列的一个或全部对象作为其子元素,也可以具有每个对象的多个实例。

对象

作用

button

在库中添加一个可单击的按钮

item

在库中添加一个项目。该项目可以图形化地呈现希望执行的操作,例如图表布局的图形化呈现

下面,介绍一个示例,创建下图所示的库。

 

(1)创建一个新的Excel工作簿,保存后关闭。

(2)在CustomUI Editor中打开该文件,输入下列XML代码:

<customUI xmlns=http://schemas.microsoft.com/office/2006/01/customui>

    <ribbon startFromScratch=false>

     <tabs>

      <tab

      id=rxtab

      insertBeforeMso=TabHome

      label=My CustomTab>

     

      <group

       id=rxgrp

       label=My PhotoGallery>

 

       <!– Starts the definition of our gallery–>

       <gallery

         id=rxgal

         label=My PhotoGallery

         image=img4

              columns=2

         rows=2

         itemWidth=200

         itemHeight=150

         showItemLabel=false

              size=large>

 

       <!– Insert the photo gallery–>

       <!– Import the photos you want to use first–>

       <item id=rxitem0label=London 1image=img0/>

       <item id=rxitem1label=London 2image=img1 />

       <item id=rxitem2label=London 3image=img2 />

       <item id=rxitem3label=London 4image=img3 />

       <item id=rxitem4label=London 5image=img4 />

       <item id=rxitem5label=London 6image=img5 />

 

       <!– Insert a button at the end of the gallery–>

       <button id=rxbtn

         imageMso=RefreshStatus

              label=Visit excelperfect…

         onAction=rxbtn_Click/>     

          </gallery>

 

      </group>

 

      </tab>

     </tabs>

    </ribbon>

</customUI>

(3)在CustomUI Editor中装载名为img0至img5的图像。

(4)保存并关闭CustomUI Editor,然后重新打开该工作簿,即可看到上图所示的自定义库。

使用内置库控件

有时,可能想将开始、插入、页面布局选项卡中的库放置在自定义选项卡中以方便使用,而无须在选项卡之间来回移动。下面的例子将三个内置库控件集中放置在一个自定义选项卡中。XML代码为:

<customUI xmlns=http://schemas.microsoft.com/office/2006/01/customui>

  <ribbon startFromScratch=false>

    <tabs>

      <tab id=rxtab

      insertBeforeMso=TabHome

      label=GalleriesTab>

        <group

       id=rxgrp

       label=Gallery Group>

          <box id=rxbox1boxStyle=horizontal>

          <gallery idMso=FontColorPickerlabel=AAA/>

          <labelControl id=rxlbl1label=Font Color/>     

          </box>

          <box id=rxbox2boxStyle=horizontal>

          <gallery idMso=CellFillColorPicker />

          <labelControl id=rxlbl2label=Cell Color/>     

          </box>

          <box id=rxbox3boxStyle=horizontal>

          <gallery idMso=ChartTypeColumnInsertGallerysize=normal />   

          </box>

        </group>

      </tab>

    </tabs>

  </ribbon>

</customUI>

结果如下图所示。

 

在运行时创建图像库

如果使用XML代码装载图像的话,那么随着库中图像数量的增加,需要输入更多的XML代码。显然,这不仅浪费时间,而且使维护变得更复杂。

下面的示例演示如何在库中动态添加图像,以避免额外的输入。在代码中:

n         getImage:用于装载库控件的图片(不是真正的库里面的图片)。

n         getItemCount:用于返回库中的项目数。在VBA代码中使用常量来决定这个值,这样如果需要更多的图片项目,只需简单地修改其数值。

n         getItemImage:用于装载项目图像,也就是说,每一个显现在库中的图像。

注意,不会遍历每一项,相反,Ribbon将回调getItemImage属性直至遍历由getItemCount属性决定的所有项目。此时,返回代表每个项目的图像的索引值。接下来,使用这个索引来捕获并装载相应的图片。

定制Excel界面库控件的XML代码如下:

<customUI xmlns=http://schemas.microsoft.com/office/2006/01/customui>

    <ribbon startFromScratch=false>

        <tabs>

            <tab id=rxtab

                 insertBeforeMso=TabHome

                 label=My Custom Tab>

                

                 <group id=rxgrp

                        label=My Photo Gallery>

 

                       <!– Starts the definition of our gallery –>

                        <gallery id=rxgal

                                label=My Photo Gallery

                                columns=2

                                rows=2

                                itemWidth=200

                                itemHeight=150

                                getImage=rxgal_getImage

                                getItemCount=rxgal_getItemCount

                                getItemImage=rxgal_getItemImage

                                 onAction=rxgal_Click

                                showItemLabel=false

                                size=large>

   

                       <!– Inserts a button at the bottom of the gallery –>

                           <button id=rxbtn

                                   imageMso=RefreshStatus

                                   label=Visit excelperfect …

                                   onAction=rxbtn_Click/>

 

                        </gallery>

                </group>

            </tab>

        </tabs>

    </ribbon>

</customUI>

在Excel VBE中的代码如下:

Public Const gcItemCount =6

 

‘rxgal getImage回调

Sub rxgal_getImage(controlAs IRibbonControl, ByRef returnedVal)

    Set returnedVal =LoadPicture(ActiveWorkbook.Path & “\MUN.bmp”)

End Sub

 

‘rxgal getItemCount回调

Subrxgal_getItemCount(control As IRibbonControl, ByRef returnedVal)

    returnedVal = gcItemCount

End Sub

 

‘rxgal getItemImage回调

Sub rxgal_getItemImage(controlAs IRibbonControl, index As Integer, ByRef returnedVal)

    Set returnedVal =LoadPicture(ActiveWorkbook.Path & “\img” & index & “.bmp”)

End Sub

 

‘rxgal onAction回调

Sub rxgal_Click(control AsIRibbonControl, id As String, index As Integer)

    MsgBox “您单击的图像索引号为 ” & index

End Sub

 

‘rxbtn onAction回调

Sub rxbtn_Click(control AsIRibbonControl)

    MsgBox “更多技术请访问:http://www.excelperfect.com”

End Sub

当然,如果BMP格式效果不好,可以使用上篇文章中介绍的LoadImage函数装载PNG格式。

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值