Link to home
Start Free TrialLog in
Avatar of DavidRothan
DavidRothanFlag for United Kingdom of Great Britain and Northern Ireland

asked on

This is a .NET AddIn for a COM application. How do I get bitmap resources into toolbar buttons?

Im creating an add-in using VB.NET (VS2008) for an application that has a COM interface, referenced via interops.

I have added a toolbar to the application which is functioning correctly, but I cant find a way of getting bitmaps to display on the buttons. The buttons are simply black squares!

When I created an add-in for this same application in VB6, this was not a problem. Using the resource editor, I was able to create toolbar images that resided in addin.res which was then compiled into the dll.  The images displayed correctly on the buttons.

The code in VB6 looked like this:
Object.SetAddInInfo(InstanceHandle, EnvironmentCatID, CategoryName, IDColorBitmapMedium, IDColorBitmapLarge, IDMonochromeBitmapMedium, IDMonochromeBitmapLarge, NumberOfCommands, CommandNames, CommandIDs)

ObjectRequired. The object to which the method applies.
InstanceHandleRequired Long.
EnvironmentCatIDRequired String. Specifies the ID of the environment to which the command bar button is to be added.
CategoryNameRequired String.
IDColorBitmapMediumRequired Long.
IDColorBitmapLargeRequired Long.
IDMonochromeBitmapMediumRequired Long.
IDMonochromeBitmapLargeRequired Long.
NumberOfCommandsRequired Long.
CommandNamesRequired Array of Strings.
CommandIDsRequired Array of Longs.

In VB.NET I can get the instance handle of my dll like this...
Dim aModule As System.Reflection.Module = Me.GetType().Module
Dim aModuleInstance as integer = Marshal.GetHINSTANCE(aModule).ToInt32

The difficulty I now have is trying to add a bitmap resource to my project that can then be passed to the addin via the longs in the SetAddInInfo method.

Is there any way to achieve this in .NET?
Avatar of DavidRothan
DavidRothan
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I'm going to try and simplify this as nobody seems to help! I'm wondering if my original question started to ramble on a little!
In VB6, each bitmap resource was indexed in a resource file, which I'm gussing was ultimatley compiled into the dll.
The application I am writing an add-in for depends upon this index and an instance handle for the dll containing the resources in order to correctly display a bitmap in each of the add-in toolbar buttons.
Can I mimic this somehow  in .NET?
In dotNet it is certainly possible to include bitmap resources in a program. These can be inserted in a resource file

If you look into the application properties, in the Resources tab you will find an "Add resource" option with which you can add files. You can specify the names of the resources (e.g. "pic1", "pic2" etc)  and if the files are to be linked or embedded in a resource file, choose to embed them.

Now the bitmaps are included in the application such that you can address to them with the following code
            ResourceManager rm = Properties.Resources.ResourceManager;
            ResourceSet rs = rm.GetResourceSet(Application.CurrentCulture, false, true);
            pictureBox1.Image = (Bitmap)rs.GetObject("pic2");

Open in new window

a more conveniant way of handling the bitmaps would be

pictureBox1.Image = Properties.Resources.pic1;

but that would not make use of an index
Herein lies the problem. Because the application I'm developing for has quite an old COM API, it's expecting me to pass an instance handle to a dll and then an index for a toolbar image. In fact, it only requires one bitmap for all toolbar icons. The bitmap will contain ALL toolbar images in a 16x48 pixel arrangement (for say, three buttons).
This is what the add-in needs to create the toolbar:
MyAddin.SetAddInInfo(InstanceHandle, EnvironmentCatID, CategoryName, IDColorBitmapMedium, IDColorBitmapLarge, IDMonochromeBitmapMedium, IDMonochromeBitmapLarge, NumberOfCommands, CommandNames, CommandIDs)
Everything 'not bold' is working as expected. I can see the toolbar and each of its buttons, but the button icons display as solid black squares.
In order to get a bitmap onto the icons, the only possible way is by passing a dll instance handle as in integer (InstanceHandle) and an integer that represents the integer index of a bitmap in the dll.
As I mentioned above, I can get the intancehandle of my dll like this:
Dim aModule As System.Reflection.Module = Me.GetType().Module
Dim aModuleInstance as integer = Marshal.GetHINSTANCE(aModule).ToInt32
But how can I compile a bitmap resource into my dll that can be reference via an integer index?
I expected that I might have been able to fudge a solution by naming my resource files 1,2 etc, but I can't use an integer as a name of a resource!
The simplest way of explaining is by considering how the above was achieved with VB6 and the 'resource editor'. In VB6 it was possible to create a resource file containing images that were indexed (see attached image). I need to be able to somehow emulate this in .NET.

res.JPG
have you tried including the VB6 image dll into the dotnet project ?
(it looks like tinkering a CD-rom into a floppy disk drive)


ASKER CERTIFIED SOLUTION
Avatar of DavidRothan
DavidRothan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial