在 Excel 中使用 RibbonX API 设置透明图标不起作用
Set transparent icon with RibbonX API in Excel not working
我正在尝试使用 RibbonX API 为我的 Excel 加载项设置透明图标。这是我的 XML:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" loadImage="gui_LoadImage">
<ribbon>
<tabs>
<tab id="CBtab" label="2019CBMaster">
<group id="visualizationGroup" label="Visualization">
<button id="btnHistogram" label ="Press here" enabled="true" screentip="Press and see how magic happens" image="icn_btnHistogram.jpg" size="large"/>
</group>
<group id="NewGroup" label="2019NewGroup" >
<box id="bxBo">
<button id="btnNew" label="Press" image="icn_btnHisto.jpg" size="large"/>
<menu id="mnMenu" image="btn_img_random.jpg">
</menu>
</box>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
这是设置图片的子程序:为了我的测试目的,它忽略了 xml 参数并简单地加载了 'fixed' 图片:
Sub gui_LoadImage(ImageName As String, ByRef Image)
Set Image = stdole.LoadPicture("C:\Office 2010 Developer Resources\icons\gg.ico")
End Sub
我在 GIMP 中将 gg.ico 保存为 24 bpp 和 1 位 alpha ico 文件。我也试过外部ico-s,但它们也没有显示。
正如您从上面了解到的那样,图标并不是完全透明显示的。
你能帮忙吗?
此外,这个 RibbonX API 仍然受支持吗?我想知道,因为我在做一件非常基本的事情时遇到困难,而且几乎找不到文档。如果没有,用于开发具有可自定义功能区的 Excel 加载项的现代框架是什么?
谢谢。
自定义功能区UI 图标可以是透明的。我相信它们一定是 .png 文件。以下是 UI 功能区图标设置的详细信息,其中包括透明背景。
https://docs.microsoft.com/en-us/office/dev/add-ins/design/add-in-icons
修订
在自定义 UI 编辑器中,您可以导入图像文件,然后只需在 XML 代码中引用文件名。文件类型和图像本身是否具有透明背景是我认为决定它是否透明显示在功能区上的因素。这就是 XML 的样子。 <button id="customButton1" label="Label 1" size="large" onAction="Macro1" image="picture1" />
在查看您的 XML 代码时,我发现您的文件扩展名包含在图像中。我没有在我的自定义图像图标中包含文件扩展名。在我链接的自定义 UI 编辑器中,您可以添加导入图像文件,它出现在侧窗格中,然后您仅引用 XML 中的文件名(无文件扩展名)。我还相信是文件类型本身以及图像是否实际上具有透明背景,这决定了它是否透明地显示在功能区中。 jpg 文件不能透明,所以这可能是你的问题。如果您的 UI 编辑器无法容纳 PNG 文件,请尝试使用 GIF。图标很小;图像质量应该不是问题。
这是一个例子:
<splitButton id="splitButton2" size="large" >
<button id="InsertTools" label="Insert" image="InsertTools_Transparent" />
<menu id="menu2">
<button id="buttonInsertMultiSheets" onAction="ModRibbon.Run_InsertMultipleSheets" label="Insert Multiple Sheets" screentip="Add multiple worksheets at one time. An input box will appear for you to enter the number of sheets to be added." image="WorksheetAddMultipleSheets" />
</menu>
</splitButton>
以下是我学习时的一些个人笔记,可能对你有帮助,也可能没有帮助。
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<backstage>
<button id="MyCustomButton1" label="My Macro"
imageMso="HappyFace" isDefinitive="true" onAction="ModRibbon.Run_Macro1"/>
<!-- 'button id' each must be unique -->
<!-- 'My Macro' is the text that will appear on your button -->
<!-- Use 'imageMso' or 'image' to include a picture on your button. -->
<!-- 'imageMso' uses built-in Office images; do not insert into this file as icons. -->
<!-- 'image' uses your own image as your button's icon. Insert it into this file as an icon. -->
<!-- 'HappyFace' is the name of the built-in Office image (for custom images, enter the file name). -->
<!-- 'ModRibbon' is the name of the VBA module in which you will paste this call-back sub. You can name this anything. -->
<!-- 'Run_' is a custom prefix added to the macro name, so you don't have to rename your macros. -->
<!-- 'Macro1' is the macro that should be run when the button is clicked. -->
</backstage>
</customUI>
对于我使用的自定义 UI 编辑器,回调将是:
'Callback for buttonInsertMultiSheets onAction
Sub Run_InsertMultipleSheets(control As IRibbonControl)
End Sub
从您的代码来看,您似乎还需要调用来加载图像,因此自定义UI 控件有两个回调:
Sub LoadImage (imageID As String, ByRef image)
Sub OnLoad(ribbon As IRibbonUI)
如果你也有一个[Content_Types].xml,那么默认类型是PNG,你需要在文件的最后添加一行,但在之前,然后保存您的更改。
<Default Extension="jpg" ContentType="application/octet-stream"/>
功能区上图标的推荐文件格式 UI 是 PNG。 Microsoft Office 功能区和工具栏支持 ICO、BMP 和 ICO 文件。因此,我建议将您的 VBA 宏转换为 COM add-in,您可以在其中轻松加载此类图像。
您可能会发现 Graphics formats for Microsoft Office add-ins 文章很有帮助。 Fluent UI 在以下系列文章中有深入介绍:
我正在尝试使用 RibbonX API 为我的 Excel 加载项设置透明图标。这是我的 XML:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" loadImage="gui_LoadImage">
<ribbon>
<tabs>
<tab id="CBtab" label="2019CBMaster">
<group id="visualizationGroup" label="Visualization">
<button id="btnHistogram" label ="Press here" enabled="true" screentip="Press and see how magic happens" image="icn_btnHistogram.jpg" size="large"/>
</group>
<group id="NewGroup" label="2019NewGroup" >
<box id="bxBo">
<button id="btnNew" label="Press" image="icn_btnHisto.jpg" size="large"/>
<menu id="mnMenu" image="btn_img_random.jpg">
</menu>
</box>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
这是设置图片的子程序:为了我的测试目的,它忽略了 xml 参数并简单地加载了 'fixed' 图片:
Sub gui_LoadImage(ImageName As String, ByRef Image)
Set Image = stdole.LoadPicture("C:\Office 2010 Developer Resources\icons\gg.ico")
End Sub
我在 GIMP 中将 gg.ico 保存为 24 bpp 和 1 位 alpha ico 文件。我也试过外部ico-s,但它们也没有显示。
正如您从上面了解到的那样,图标并不是完全透明显示的。
你能帮忙吗?
此外,这个 RibbonX API 仍然受支持吗?我想知道,因为我在做一件非常基本的事情时遇到困难,而且几乎找不到文档。如果没有,用于开发具有可自定义功能区的 Excel 加载项的现代框架是什么?
谢谢。
自定义功能区UI 图标可以是透明的。我相信它们一定是 .png 文件。以下是 UI 功能区图标设置的详细信息,其中包括透明背景。
https://docs.microsoft.com/en-us/office/dev/add-ins/design/add-in-icons
修订
在自定义 UI 编辑器中,您可以导入图像文件,然后只需在 XML 代码中引用文件名。文件类型和图像本身是否具有透明背景是我认为决定它是否透明显示在功能区上的因素。这就是 XML 的样子。 <button id="customButton1" label="Label 1" size="large" onAction="Macro1" image="picture1" />
在查看您的 XML 代码时,我发现您的文件扩展名包含在图像中。我没有在我的自定义图像图标中包含文件扩展名。在我链接的自定义 UI 编辑器中,您可以添加导入图像文件,它出现在侧窗格中,然后您仅引用 XML 中的文件名(无文件扩展名)。我还相信是文件类型本身以及图像是否实际上具有透明背景,这决定了它是否透明地显示在功能区中。 jpg 文件不能透明,所以这可能是你的问题。如果您的 UI 编辑器无法容纳 PNG 文件,请尝试使用 GIF。图标很小;图像质量应该不是问题。
这是一个例子:
<splitButton id="splitButton2" size="large" >
<button id="InsertTools" label="Insert" image="InsertTools_Transparent" />
<menu id="menu2">
<button id="buttonInsertMultiSheets" onAction="ModRibbon.Run_InsertMultipleSheets" label="Insert Multiple Sheets" screentip="Add multiple worksheets at one time. An input box will appear for you to enter the number of sheets to be added." image="WorksheetAddMultipleSheets" />
</menu>
</splitButton>
以下是我学习时的一些个人笔记,可能对你有帮助,也可能没有帮助。
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<backstage>
<button id="MyCustomButton1" label="My Macro"
imageMso="HappyFace" isDefinitive="true" onAction="ModRibbon.Run_Macro1"/>
<!-- 'button id' each must be unique -->
<!-- 'My Macro' is the text that will appear on your button -->
<!-- Use 'imageMso' or 'image' to include a picture on your button. -->
<!-- 'imageMso' uses built-in Office images; do not insert into this file as icons. -->
<!-- 'image' uses your own image as your button's icon. Insert it into this file as an icon. -->
<!-- 'HappyFace' is the name of the built-in Office image (for custom images, enter the file name). -->
<!-- 'ModRibbon' is the name of the VBA module in which you will paste this call-back sub. You can name this anything. -->
<!-- 'Run_' is a custom prefix added to the macro name, so you don't have to rename your macros. -->
<!-- 'Macro1' is the macro that should be run when the button is clicked. -->
</backstage>
</customUI>
对于我使用的自定义 UI 编辑器,回调将是:
'Callback for buttonInsertMultiSheets onAction
Sub Run_InsertMultipleSheets(control As IRibbonControl)
End Sub
从您的代码来看,您似乎还需要调用来加载图像,因此自定义UI 控件有两个回调:
Sub LoadImage (imageID As String, ByRef image)
Sub OnLoad(ribbon As IRibbonUI)
如果你也有一个[Content_Types].xml,那么默认类型是PNG,你需要在文件的最后添加一行,但在之前,然后保存您的更改。
<Default Extension="jpg" ContentType="application/octet-stream"/>
功能区上图标的推荐文件格式 UI 是 PNG。 Microsoft Office 功能区和工具栏支持 ICO、BMP 和 ICO 文件。因此,我建议将您的 VBA 宏转换为 COM add-in,您可以在其中轻松加载此类图像。
您可能会发现 Graphics formats for Microsoft Office add-ins 文章很有帮助。 Fluent UI 在以下系列文章中有深入介绍: