Trying to add icon to button in Inventor plugin

Trying to add icon to button in Inventor plugin

saikiran.nT432F
Participant Participant
949 Views
4 Replies
Message 1 of 5

Trying to add icon to button in Inventor plugin

saikiran.nT432F
Participant
Participant

Hi,

Iam trying to add an icon in Inventor plugin button but for the following code added icon is not displaying or getting the error saying "Parameter is incorrect" or if i change the line  

Dim largeIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.Arturo_Wibawa_Akar_Icon) to

Dim largeIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp("D:\Desktop\Videos\Arturo-Wibawa-Akar-Icon.ico")

 

iam using icon with size 96X96 and tried wityh sizes 32X36. 16X16.

 

Any help in resloving in this issue is much appreciataed. Thanks in advance.

Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
' Initialize AddIn members.
g_inventorApplication = addInSiteObject.Application

' Connect to the user-interface events to handle a ribbon reset.
m_uiEvents = g_inventorApplication.UserInterfaceManager.UserInterfaceEvents

' TODO: Add button definitions.

'Sample to illustrate creating a button definition.
Dim largeIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.YourBigImage)
Dim smallIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.YourSmallImage)

Dim controlDefs As Inventor.ControlDefinitions = g_inventorApplication.CommandManager.ControlDefinitions
m_sampleButton = controlDefs.AddButtonDefinition("sampledef", "Toapp", CommandTypesEnum.kShapeEditCmdType, AddInClientID)

m_sampleButton.LargeIcon = largeIcon
m_sampleButton.SmallIcon = smallIcon

' Add to the user interface, if it's the first time.
If firstTime Then
AddToUserInterface()
End If
End Sub

 

Public NotInheritable Class PictureDispConverter
<DllImport("OleAut32.dll", EntryPoint:="OleCreatePictureIndirect", ExactSpelling:=True, PreserveSig:=False)> _
Private Shared Function OleCreatePictureIndirect( _
<MarshalAs(UnmanagedType.AsAny)> ByVal picdesc As Object, _
ByRef iid As Guid, _
<MarshalAs(UnmanagedType.Bool)> ByVal fOwn As Boolean) As stdole.IPictureDisp
End Function

Shared iPictureDispGuid As Guid = GetType(stdole.IPictureDisp).GUID

Private NotInheritable Class PICTDESC
Private Sub New()
End Sub

'Picture Types
Public Const PICTYPE_BITMAP As Short = 1
Public Const PICTYPE_ICON As Short = 3

<StructLayout(LayoutKind.Sequential)> _
Public Class Icon
Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Icon))
Friend picType As Integer = PICTDESC.PICTYPE_ICON
Friend hicon As IntPtr = IntPtr.Zero
Friend unused1 As Integer
Friend unused2 As Integer

Friend Sub New(ByVal icon As System.Drawing.Icon)
Me.hicon = icon.ToBitmap().GetHicon()
End Sub
End Class

<StructLayout(LayoutKind.Sequential)> _
Public Class Bitmap
Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Bitmap))
Friend picType As Integer = PICTDESC.PICTYPE_BITMAP
Friend hbitmap As IntPtr = IntPtr.Zero
Friend hpal As IntPtr = IntPtr.Zero
Friend unused As Integer

Friend Sub New(ByVal bitmap As System.Drawing.Bitmap)
Me.hbitmap = bitmap.GetHbitmap()
End Sub
End Class
End Class

Public Shared Function ToIPictureDisp(ByVal icon As System.Drawing.Icon) As stdole.IPictureDisp
Dim pictIcon As New PICTDESC.Icon(icon)
Return OleCreatePictureIndirect(pictIcon, iPictureDispGuid, True)
End Function

Public Shared Function ToIPictureDisp(ByVal bmp As System.Drawing.Bitmap) As stdole.IPictureDisp
Dim pictBmp As New PICTDESC.Bitmap(bmp)
Return OleCreatePictureIndirect(pictBmp, iPictureDispGuid, True)
End Function
End Class

0 Likes
950 Views
4 Replies
Replies (4)
Message 2 of 5

bradeneuropeArthur
Mentor
Mentor

use this way:

m_sampleButton = controlDefs.AddButtonDefinition("sampledef", "Toapp", CommandTypesEnum.kShapeEditCmdType, AddInClientID,"","",smallIcon,largeIcon,ButtonDisplayEnum.kDisplayTextInLearningMode)

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 5

JelteDeJong
Mentor
Mentor

I wrote a blog post/tutorial about addin icons to your buttons. You might find it interesting.

http://www.hjalte.nl/tutorials/61-adding-icons-to-your-buttons

.

some important points:

A “standard” size icon is 16 pixels wide and 16 pixels high. A large size icon is 32 wide by 32 high. Inventor is very strict about this. if you get it wrong then it won't work.

 

the method PictureDispConverter.ToIPictureDisp(...) will only accept objects of the type "System.Drawing.Icon" or "System.Drawing.Bitmap". Therefore this won't work:

PictureDispConverter.ToIPictureDisp("D:\Desktop\Videos\Arturo-Wibawa-Akar-Icon.ico")

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 4 of 5

saikiran.nT432F
Participant
Participant

Thank you so much for your quick relpy @JelteDeJong.

 

With the Blog you shared i was able to add standard icon, but whatever i do i am not able to make it work for large icon. In the screen shot shared below i want to create an icon like "Team Web" but what i created is "Display name".

 

I followed the 16X16 for Standard and 32X32 for Large Icon. I shared the Code i am using and the .png testing files as well.

 

Please go through them any suggestions will be a lot of help. Thanks in advance

0 Likes
Message 5 of 5

saikiran.nT432F
Participant
Participant

Hello Everyone,

 

About the Issue (Trying to add icon to button in Inventor plugin), i tried all the available methods i could find on the Forums and other online sites with different file sizes. But iam unable to make it work for Large Icon (32X32).

 

If any one have an idea as how to resolve this please guide me. Thanks in advance.

0 Likes