Search code examples
c#buttoniconsribbonrevit-api

Why isn't my ribbon button icon showing?


I've created a button to add to the Revit ribbon, under the Modify tab, under the Properties tab. The button shows up and functions without a problem. The only issue that we've encountered is that the icon for the button will not display. We've tried several approaches to remedy this issue, but nothing seems to work.

Code:

Autodesk.Windows.RibbonControl pRibbon = Autodesk.Windows.ComponentManager.Ribbon;
foreach (var pTab in pRibbon.Tabs)
{
    if (pTab.Id == "Modify")
    {
        foreach (var pPanel in pTab.Panels)
        {
            if (pPanel.Source.Id == "properties_shr")
            {
                // Add button.
                pIcon = Properties.Resources.AS_Revit_UI_Taps_Tees32x32.GetHbitmap();
                var pBtn = CreatePanelButton(pIcon, "Taps/Tees", "id_astapteebutton", "Taps/Tees", 32, 32);

                Autodesk.Windows.ComponentManager.UIElementActivated += RunSwitchTapTee;
                pPanel.Source.Items.Add(pBtn);
                break;
            }
        }
    }
}

public Autodesk.Windows.RibbonButton CreatePanelButton(IntPtr pIcon, string sBtnName, string sID, string sText, int iHeight, int iWidth)
{
    var pBtn = new Autodesk.Windows.RibbonButton()
    {
        Name = sBtnName,
        Image = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(pIcon, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(32, 32)),
        Id = sID,
        AllowInStatusBar = true,
        AllowInToolBar = true,
        GroupLocation = Autodesk.Private.Windows.RibbonItemGroupLocation.Middle,
        MinHeight = 0,
        MinWidth = 0,
        Height = iHeight,
        Width = iWidth,
        IsEnabled = true,
        IsVisible = true,
        IsCheckable = true,
        ShowImage = true,
        ShowText = true,
        Orientation = System.Windows.Controls.Orientation.Vertical,
        Text = sText,
        Size = Autodesk.Windows.RibbonItemSize.Large,
        ResizeStyle = Autodesk.Windows.RibbonItemResizeStyles.HideText
    };

    return pBtn;
}

The result is this:

enter image description here

As you can see, the button is placed. It works as intended. It just wont show the icon... Any help would be appreciated. Thanks.


Solution

  • I found that my error was in the setting of the Image. I wrote:

    Image = System.Windows.Interop....
    .
    .
    .
    Size = Autodesk.Windows.RibbonItemSize.Large,
    

    I needed to adjust one or the other - either Image to LargeImage or RibbonItemSize.Large to RibbonItemSize.Standard...

    It would only make sense that large images need large sizes, and standard images need standard sizes.

    What's the difference?

    In Revit, your ribbon buttons can either be 16x16 pixels (standard) or 32x32 pixels (large). Depending on your desired UI layout will help you decide which you need.