Using C# resources Icons as IMaxIcon ?

 
 
 
Posted by:jonathan.beaubien@thq.com
Data created:3 March 2011

Hi...

I am creating a bunch of ActionItems for a toolbar.

And I want to create an icon for them (the public override IMaxIcon Icon property).

Is there a way to load the C# resources icons and convert them as IMaxIcons ?

I'm new to the Max SDK, so I'm not really sure how to procede.

Thank you !

I tried implementing the IMaxIcon interface.

But I'm not sure I'm doing it right.

I added my code below.

I create a managed ImageList and used its Handle properties for the implementation of the IMaxIcon properties.

The GetLocalizedImage function is just a wrapper that get the images from the resources. I created four icons, in PNG format with a transparent background. Two of these are the large 24x24 normal and disabled icons and the other are the small 16x16 icons.

What is not clear for me is on how to implement the following:

IBaseInterface  IInterfaceServer.GetInterface(IInterface_ID id)
bool  IEquatable<IInterfaceServer>.Equals(IInterfaceServer other)

IntPtr  INativeObject.Handle

When I add this class in my ActionItem as the icon and that I try to customize the user interface to add them as tool bar, I get the following warning:

System.InvalidCastException: Impossible d'effectuer un cast d'un objet de type 'JigSaw_MaxTools.JigSawMaxIcon' en type 'Autodesk.Max.IImplementable'.
   A Autodesk.Max.Wrappers.MaxIcon.__ConvertToUnmanaged(IMaxIcon managed)
   A Autodesk.Max.Wrappers.ManagedWrapperActionItem.GetIcon(ManagedWrapperActionItem* )

It is probably due to the three functions mentioned above that I did not implement... Sorry for the french. It says that it cannot cast my JigSawMaxIcon instance to a IImplementable.

Can anyone help me ?

  
public class JigSawMaxIcon : IMaxIcon
    {
        //Constructors

        public JigSawMaxIcon(string p_LargeIconName, string p_LargeDisabledIconName, string p_SmallIconName, string p_SmallDisabledIconName)
        {
            m_LargeImageList = new ImageList();
            m_LargeImageList.Images.Add(JigSaw_Resources.GetLocalizedImage(typeof(JigSawToolsPlugin).Assembly, p_LargeIconName, "JigSaw_MaxTools.Resources"));
            m_LargeImageList.Images.Add(JigSaw_Resources.GetLocalizedImage(typeof(JigSawToolsPlugin).Assembly, p_LargeDisabledIconName, "JigSaw_MaxTools.Resources"));
            

            m_SmallImageList = new ImageList();
            m_SmallImageList.Images.Add(JigSaw_Resources.GetLocalizedImage(typeof(JigSawToolsPlugin).Assembly, p_SmallIconName, "JigSaw_MaxTools.Resources"));
            m_SmallImageList.Images.Add(JigSaw_Resources.GetLocalizedImage(typeof(JigSawToolsPlugin).Assembly, p_SmallDisabledIconName, "JigSaw_MaxTools.Resources"));
        }


        //Implement the IMaxIcon interface
    
        IntPtr  IMaxIcon.DefaultImageList
        {
            get
            {
                return m_LargeImageList.Handle;
            }
        }

        int  IMaxIcon.GetLargeImageIndex(bool enabledVersion, System.Drawing.Color backgroundColor)
        {
            if (enabledVersion)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }

        int  IMaxIcon.GetSmallImageIndex(bool enabledVersion, System.Drawing.Color backgroundColor)
        {
            if (enabledVersion)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }

        IntPtr  IMaxIcon.LargeImageList
        {
            get
            {
                return m_LargeImageList.Handle;
            }
        }

        IntPtr  IMaxIcon.SmallImageList
        {
            get
            {
                return m_SmallImageList.Handle;
            }
        }

        bool  IMaxIcon.UsesAlphaMask
        {
            get { return false; }
        }

        IBaseInterface  IInterfaceServer.GetInterface(IInterface_ID id)
        {
            //?
             return null;
        }

        bool  IEquatable<IInterfaceServer>.Equals(IInterfaceServer other)
        {
            //?
             return false;
        }

        void  IDisposable.Dispose()
        {
            m_LargeImageList.Dispose();
            m_SmallImageList.Dispose();
        }

        IntPtr  INativeObject.Handle
        {
            //?
            get
            {
                return IntPtr.Zero;
            }
        }


        //Members

        ImageList m_LargeImageList;
        ImageList m_SmallImageList;
    }

 

 

Ah, I see why you tried implementing that now. Unfortunately it will not work that way. The only 'pluggable' types you're allowed to implement/send back to Max are located inside Autodesk.Max.Plugins namespace. Implementing an interface and passing it directly will not work.

I don't think its directly possible to pass managed icons. Perhaps try to create a new instance of MaxIcon, and then directly set its properties using marshalling?

Marsel Khadiyev (Software Developer, EPHERE Inc.)

Thank you for your reply...

I will try that approach and if it works, I will type in the code here for the rest of the users.