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