访问功能区的 2013 自定义图像 [英] Access 2013 Custom Images for the Ribbon

查看:40
本文介绍了访问功能区的 2013 自定义图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始将 Access Ribbon 用于数据库中的菜单,但现在恐怕我陷入了僵局.

I've started using the Access Ribbon for menus in my database, but now I'm afraid I've come to an impasse.

我想将自己的图像用于功能区中的图标,但在完成此任务时遇到很多困难.

I'd like to use my own images for the icons in the ribbon, but am having loads of difficulty accomplishing this task.

代码就在下面,但我不断收到的错误消息是 Access 无法运行宏或回调函数getImages".

The code is just below, but the error message I keep getting is that Access cannot run the macro or callback function 'getImages'.

功能区xml代码:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
      <ribbon startFromScratch="true"> 
        <tabs>          
          <tab id="tMainMenu" label="Main Menu"> 
            <group id="Developer"> 
              <button id="DevTools1"/> 
            </group>
            <group id="MainMenuSpacer1">
            </group>
            <group id="gSupport">
              <button id="Support1" label="Custom Image" getImage="getImages"/>
            </group>
            <group id="MainMenuSpacer2">
            </group>
            <group id="gShutdown"> 
              <button id="Shutdown"/>
            </group>
          </tab>
        </tabs> 
      </ribbon>
    </customUI>

VBA 程序代码:

Public Sub getImages(control As IRibbonControl, ByRef image)
If irc.ID = "Support1" Then
    MyPath = "J:\Images\OperatorAssistantButton1.png"
    Set image = LoadPicture(MyPath)
End If
End Sub

我的公司没有 12.0 参考.我们使用的是 15.0

My company does not have the 12.0 Reference. We are using 15.0

推荐答案

请记住,图片加载有两种类型.

Keep in mind that there are TWO types of image loads.

一次性图片加载用于大多数"图片加载.当您需要自定义图标但不会在加载后更改图像时使用此选项.因此,此方法用于功能区的大多数"自定义图像加载.

A one-time image load is used for "most" image loads. This is used when you want a custom icon, but are NOT going to change the image once loaded. This approach is thus used for "most" custom image loads for the ribbon.

当您不仅希望图像能够被加载,而且还希望图像发生变化时,可以使用此方法 - 假设图像将天气显示为晴天,然后将其更改为多云的云.

This is used when you want the image to be able to not only be loaded, but ALSO want to have the image change - say an image that shows the weather as sunny, and then you change it to a cloud for cloudy.

区分您的目标以及您需要和想要的图像加载类型非常重要.

So VERY important to distinguish between your goal, and the type of image load you need and want.

对于功能区中的常规"图像加载和设置,您因此只需一次指定将使用的例程(在功能区 xml 的开头).然后,此例程用于所有图像设置,您无需为每个控件指定回调例程.

For "general" image load and setting in the ribbon, you thus ONLY ONE TIME specify the routine you will use (at the start of the ribbon xml). This routine is then used for ALL of your image settings and you do NOT specify the call back routine for each control.

因此,您可以为每个控件使用图像"属性设置,但只指定一次回调例程.

You thus use the "image" attribute setting for each control, but ONLY specify the call back routine one time.

功能区代码将如下所示:

The ribbon code will thus look like this:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
 loadImage="CallBackLoadImage"
 onLoad="MyRibbonLoadA">

    <group id="group1" label="Basic Button Attributes Examples">
      <button id="button1" label="Button1" 
             size="large"
             getEnabled="MyEnable"
             getVisible="MyVisible" 
             image="CokeClassic.png"
             onAction="=Bt1()"
             supertip= "example load a image without ability to change"
        />
      <button id="button2" label="button2"  
             size="large"
             getEnabled="MyEnable"
             image="CokeZero.png"
             onAction="=Bt2()"
       />

注意上面我们如何设置第一个按钮的图像.

Note in above HOW we set the image for the first button.

image="CokeClassic.png"

对于第二个按钮,我们使用:

and for second button we ue:

image="CokeZero.png"

因此,实际上,我们硬编码并在功能区的 XML 中指定图像.在上面的开头,我们设置了用于我们在功能区中指定的所有图像的全局图像加载例程.

So in effect, we hard code and specify the image in the XML of the ribbon. And in above at the start we set the GLOBAL image load routine that is to be use for ALL images we specify in the ribbon.

我们将用于/调用所有这些图像的例程因此在开始时指定:

The routine we will use/call for all these images is thus specified at the start:

 loadImage="CallBackLoadImage"

请注意这仅在功能区 XML 的开头执行一次 - 您没有为每个按钮指定此回调.

Note how this is ONLY done ONE TIME and at the start of the ribbon XML – you do NOT specify this call back for each button.

VBA 例程(在我们的示例中为 CallBAckLoadImage)必须放置在标准 VBA 代码模块中(不是类,也不是表单代码模块).我们的色带加载代码是这样的:

The VBA routine (CallBAckLoadImage in our example) has to be placed in a standard VBA code module (not a class, and not in a forms code module). The ribbon load code we have is thus this:

Sub CallbackLoadImage(strImage As String, _
                 ByRef image)

  Dim strImagePath  As String
   ' Callback loadImage

  strImagePath = CurrentProject.Path & "\ribbon\" & strImage
  Set image = LoadPicture(strImagePath)

End Sub

在上面,请注意我们的代码如何假设"名为ribbon 的文件夹将放置在与运行前端 (accDB/accDE) 的文件夹相同的文件夹中.因此,我们不对任何路径名进行硬编码,并假设功能区图标"文件夹将始终存在于我们运行应用程序所在的同一文件夹中.因此,我们可以复制、重命名文件夹或将其移动到任何地方,而我们的图像加载代码仍会运行.

In above, note how our code "assumes" that a folder called ribbon will be placed in the SAME folder as where the front end (accDB/accDE) is running. Thus we don’t hard code any path names, and assume that a ribbon "icon" folder will always exist in the same folder as where we run the application from. So we can copy, rename the folder, or move it anywhere, and our image load code will still run.

所以请记住,您没有为每个按钮指定回叫——只有一次.并注意图像名称是如何自动传递给我们调用的例程的.所以图像 = 一些图像"相当不错,因为我们不需要特殊的代码来提取或找出我们想要的图像.

So just keep in mind that you do NOT specify the call back for EACH button – only one time. And NOTE HOW the image name is automatic passed to the routine we call. So the "image = some image" is rather nice, since we don’t require SPECIAL code to pluck out or figure out what image we want.

我们想在运行时更改图标.如前所述,对于大多数图像,我们使用方法 #1,但在某些情况下,我们需要并想要一个动态例程.但是,在 Ribbon 启动时,这意味着代码必须提供某种默认类型的图像,因此在本示例中,我使用了tag"设置来设置默认 Ribbon.

We want to CHANGE the icons at runtime. As noted, for most images, we use approach #1, but in some cases we need and want a dynamic routine. However, on ribbon startup, this means the code will have to supply some default type of image, and thus for this example, I used the "tag" setting to set a default ribbon.

所以我们的按钮代码现在看起来像:

So our button code now looks like:

      <button id="Weather" label = "The Weather button" 
             size="large"
             getImage="MyImage" tag="sunny.png"
             onAction= "=BtWeather()"
       />

所以在上面我们确实为每个按钮等设置了回调

So in above WE DO set the call back for EACH button etc.

回调代码如下:

Public Sub MyImage(control As IRibbonControl, ByRef image)

  Dim strImagePath  As String
  Dim strImage      As String

   ' for the 1st call when ribbon loads there no images set. So we
   ' use the tag as kludge workaround to set the starting image

   If strGlobalImage = "" then   
      strImage = control.Tag
   End If

  If strImage <> "" Then
     If Left(strImage, 4) = "mso." Then
        image = Mid(strImage, 5)
     Else
        strImagePath = CurrentProject.Path & "\ribbon\" & strImage
        Set image = LoadPicture(strImagePath)
     End If
  End If
End Sub

注意上面,我也有使用内置图像的规定——因为有用的图像太多了,所以我采用了可以为内置图标放置文本iso.ImageName"的标准.因此,如果您使用iso.BuiltInImageName",则上述内容将起作用.

Note in above, I also have provisions for using built-in images – because there are so many useful ones, then I adopted the standard that I can place the text "iso.ImageName" for built-in icons. So if you use "iso.BuiltInImageName", then above will work.

我也为上面的过程 #1 这样做,因此我有:

I also do this for process #1 above, and thus I have:

Sub CallbackLoadImage(strImage As String, _
                 ByRef image)

  Dim strImagePath  As String
   ' Callback loadImage


  If Left(strImage, 4) = "mso." Then
     strImage = Mid(strImage, 5)
     image = strImage
  Else
     strImagePath = CurrentProject.Path & "\ribbon\" & strImage
     Set image = LoadPicture(strImagePath)
  End If

End Sub

所以上面允许我使用 Office 内置的许多"图标中的任何一个 - 有很多有用的.

So above allows me to use any of the "many" icons built into office – there are lot of useful ones.

因此在两种图像加载之间保持强烈"区别:

So keep a "STRONG" distinction between the two kinds of image loads:

1) 你想要一个按钮的图片等等.

1) you want a image for a button etc.

2) 您想在加载到其他类型的图像后更改图像以获取状态等.

2) you want to CHANGE the image after loaded to other kinds of images for status etc.

最后但并非最不重要的:对于任何按钮,必须声明一组保存单个按钮状态的变量真的很痛苦.你有:可见的,启用,标签文字图片

Last but not least: For any button, it is REALLY a pain to have to declare a set of variables that hold the state of a SINGLE button. You have: Visible, Enabled, Label text Image

因此对于每个按钮,您通常必须声明一个变量来保存所有状态.对于 2-3 个按钮,您需要 12 个全局变量来保存按钮的这些状态.因此,我构建了一个自动创建并为您完成所有繁琐工作的类.

So for each button, you in general have to declare a variable to hold all the states. For just 2-3 buttons, you need like 12 global variables to hold these states of the button. So I build a class that automatic creates and does all the dirty work for you.

你可以在我的这个示例中找到上面相同的代码,还有更多:

You can find the above same code, and more in this sample working of mine here:

http://www.kallal.ca/Ribbon/ribbon.htm

上面的主要优点是您不必为每个新按钮等声明新变量,您希望动态更改图像或标签文本.

The main advantage of above is you don’t have to declare new variables for each new button etc. that you want dynamic change of the image or label text.

这篇关于访问功能区的 2013 自定义图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆