External picture for Button

External picture for Button

rikard.nilsson
Collaborator Collaborator
606 Views
1 Reply
Message 1 of 2

External picture for Button

rikard.nilsson
Collaborator
Collaborator

Hi,

 

I wonder if there is someone who has a solution for having an external picture placed on the harddrive to be placed on a button.

My plan is to create a dynamic UI with an addin and create some buttons. I would like to create some 32x32 and 16x16 icons and then by programming choose what icon to use.

Is that possible?

 

Best Regards

Rikard

0 Likes
Accepted solutions (1)
607 Views
1 Reply
Reply (1)
Message 2 of 2

JhoelForshav
Mentor
Mentor
Accepted solution

Hi Rikard! Smiley Happy

I did some testing directly from ilogic and managed to change the icon of a button in my cutom ribbon panel with this code:

Imports Inventor
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
AddReference "stdole.dll"
AddReference "System.Drawing"
Class ThisRule
	
Sub Main
 Dim g_inventorApplication = ThisApplication
 Dim AssemblyRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("Assembly")
 Dim HaflaTab As RibbonTab = AssemblyRibbon.RibbonTabs.Item("id_haflaTAB")
 Dim fastenersPanel As RibbonPanel = HaflaTab.RibbonPanels.Item("FastenersRibbonPanel")
 Dim newPicture As New System.Drawing.Bitmap("C:\Users\hfljf\Desktop\skruv32x32.ico")
 fastenersPanel.CommandControls.Item(1).ControlDefinition.LargeIcon = PictureDispConverter.ToIPictureDisp(newPicture)
End Sub

End Class
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

 Is that something you could use or have i misinterpreted your question?Smiley Very Happy