Addin Button Icon

Addin Button Icon

ravindrathorat
Participant Participant
123 Views
2 Replies
Message 1 of 3

Addin Button Icon

ravindrathorat
Participant
Participant

Hi Team

@YuhanZhang 

I am trying to crate addin in C#. I have created sample addin. It works too. But I am trying to add icon for button. It not happening. Could you please provide code snippet to add button icon.

0 Likes
124 Views
2 Replies
Replies (2)
Message 2 of 3

JhoelForshav
Mentor
Mentor

Hi @ravindrathorat 
You need to convert your Icon to IPictureDisp in the two sizes 32x32 and 16x16. I don't know how you've set it up but I'm guessing you have an ico-file in your resources with these two sizes, or two different files maybe.

I don't typically write in C# but I made an attempt to give you an example here with a converter and how to set a button up:

using System.Drawing;
using System.Runtime.InteropServices;
using stdole;

// Step 1: The PictureDispConverter - converts .NET icons to COM IPictureDisp
// Inventor's API is COM-based so it needs IPictureDisp, not System.Drawing.Icon
public sealed class PictureDispConverter
{
    // P/Invoke to OleAut32.dll - this is the Win32 COM function that creates IPictureDisp
    [DllImport("OleAut32.dll", EntryPoint = "OleCreatePictureIndirect", 
               ExactSpelling = true, PreserveSig = false)]
    private static extern IPictureDisp OleCreatePictureIndirect(
        [MarshalAs(UnmanagedType.AsAny)] object picdesc,
        ref Guid iid,
        [MarshalAs(UnmanagedType.Bool)] bool fOwn);

    private static Guid iPictureDispGuid = typeof(IPictureDisp).GUID;

    // PICTDESC structs describe the picture to COM
    private static class PICTDESC
    {
        public const short PICTYPE_ICON = 3;

        [StructLayout(LayoutKind.Sequential)]
        public class Icon
        {
            internal int cbSizeOfStruct = Marshal.SizeOf(typeof(Icon));
            internal int picType = PICTYPE_ICON;
            internal IntPtr hicon = IntPtr.Zero;
            internal int unused1;
            internal int unused2;

            public Icon(System.Drawing.Icon icon)
            {
                // Convert the .NET icon to a Win32 HICON handle
                this.hicon = icon.ToBitmap().GetHicon();
            }
        }
    }

    public static IPictureDisp ToIPictureDisp(System.Drawing.Icon icon)
    {
        var pictIcon = new PICTDESC.Icon(icon);
        return OleCreatePictureIndirect(pictIcon, ref iPictureDispGuid, true);
    }
}

// Step 2: In your Activate method - load the icon and create the button
public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime)
{
    var app = addInSiteObject.Application;

    // Load icon at two sizes - Inventor expects both large (32x32) and small (16x16)
    var largeIcon = new Icon(Properties.Resources.AppIcon, 32, 32);
    var smallIcon = new Icon(Properties.Resources.AppIcon, 16, 16);

    // Convert to COM IPictureDisp that Inventor understands
    IPictureDisp largePicture = PictureDispConverter.ToIPictureDisp(largeIcon);
    IPictureDisp smallPicture = PictureDispConverter.ToIPictureDisp(smallIcon);

    // Create the button definition with both icons
    var controlDefs = app.CommandManager.ControlDefinitions;
    var button = controlDefs.AddButtonDefinition(
        "My Button",                          // Display name
        "cmd_my_button",                      // Internal ID - must be unique
        CommandTypesEnum.kShapeEditCmdType,   // Command type
        "{your-addin-client-id}",             // Your addin's GUID
        "Tooltip text",                       // Short tooltip
        "Longer description tooltip",         // Long tooltip
        smallPicture,                         // 16x16 icon
        largePicture,                         // 32x32 icon
        ButtonDisplayEnum.kDisplayTextInLearningMode
    );

    if (firstTime)
        AddToUserInterface(button);
}

 If you don't get it to work - feel free to share more information about your project structure etc. Maybe even share your entire solution. Then I'm sure we'll figure it out

0 Likes
Message 3 of 3

TylerWarner33
Advocate
Advocate

@ravindrathorat 

You can review the sample provided with the SDK that shows how to do this, update 2026 to the version you are using:

%PUBLIC%\Documents\Autodesk\Inventor 2026\SDK\DeveloperTools\Samples\VCSharp.NET\AddIns\SimpleAddIn

 
If you don't have the 'Developer Tools' installed, you would run the 'developertools.msi' file, again, update 2026 to the version you are using:

%PUBLIC%\Documents\Autodesk\Inventor 2026\SDK

 

A common error that prevents the image from loading is forgetting to include the 'StandardIcon' or 'LargeIcon' parameter when defining the ControlDefinition.

If this answered your question, click 'Accept Solution'.
If this was helpful, click 'Like'.

Tyler Warner

0 Likes