Custom command bitmaps 101
This is the tutorial I promised on how to create buttons with custom UI in the VS object model.
A common question that I get is how can you add a custom bitmap to a command placed on a tool bar and have that bitmap support transparency. Getting this to work is not very complicated, but getting everything just right so the transparency works can be a little tricky.
To make this work, you are going to need VS with VC for the DLL that contains the bitmap, and an Add-in written in any supported language; such as C#, VB, or VC. The first step is to run the Add-in wizard, making sure to select the option to create a command and put a button on the tool bar (check the first checkbox on page 4 of the wizard). After finishing the wizard, right click on the solution node and select Add | New Project. Select Visual C++ Project in the tree on the right of the Add New Project dialog box, and Win32 Project on the left of this dialog box. Enter a project name; I suggest something like ProjectNameUI, where ProjectName is the name of the Add-in you are creating.
In the wizard that appears, select Application Settings, and then select the DLL radio button. After pressing the Finish button, a VC DLL project will be added to the solution, the starting point for a Satellite DLL project. To create the resources, right click on the project you just added, select Add | Add New Item, and add a new “Resource File (.rc)” item to the project. This will add a resource file to the project and show the Resource View tool window. Right click the top node of the resource added, select Add | Add Resource, select Bitmap, and then click the New button. This will add a bitmap to the resources. By default, the bitmap is too big for our needs, so open the properties window and type 16 into both the Height and Width properties. You can also enter the value 1 for the ID property.
Using the drawing tools, draw your bitmap, but do not use the lime green color, we will use this for the transparent color. To set the correct transparent color, select lime green in the Colors tool window then select Image | Adjust Colors. In the dialog box shown, enter 0 for red and blue, and 254 for green, then press OK. You can now fill in the transparent areas with the lime green color. With your bitmap created, you are now ready to make the modifications to the Add-in so it can load the Satellite DLL and the custom bitmap.
In the Add-in source code, find the AddNamedCommand method call, which by default should look something like this:
Command command = commands.AddNamedCommand(addInInstance, "MyAddin9", "MyAddin9", "Executes the command for MyAddin9", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);
You will want to modify the true value so that it reads false, and change the value 59 to the value you entered for the ID value in the properties window, which, if you are following along with this tutorial, is the value 1.
The last steps are to make the changes so that your Add-in can load the Satellite DLL. When the Add-in wizard was run, a file named ReCreateCommands.reg was created in the project directory. If you open this file, it will look like this:
REGEDIT4 [HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\7.1\AddIns\MyAddin9.Connect]
"CommandPreload"=dword:1
Add these entries, supplying the appropriate Satellite DLL name where appropriate (the name of the project I created was MyAddin9), then merge the file ReCreateCommands.reg into the registry:
"SatelliteDLLName"="MyAddin9UI.DLL"
"SatelliteDLLPath"="C:\\Documents and Settings\\craigs\\My Documents\\Visual Studio Projects\\MyAddin9"
Lastly, you need to put the Satellite DLL in the correct location. In the value given by SatelliteDLLPath in the .reg file, create a folder with the name of the VS locale you are running. For example, I am running US English VS, so I created a folder named C:\Documents and Settings\craigs\My Documents\Visual Studio Projects\MyAddin9\1033 since 1033 is the code for US English. Then copy the Satellite DLL into that folder.
After following these steps, you will have a button on the tool bar which uses a custom bitmap, and has a transparent color.
Comments
Anonymous
March 11, 2004
I've been working on this problem for a good day now and still cannot get a bitmap to show up. I've tried adding a bitmap to your Chapter 7 CustomBitmap example and altered the untitled.bmp to have some scribble. I've made sure that the SatellitDLLPath is correct in the registry. I have it in a 1033 directory under the satellite directory. Still no go. Help!? tado@microsoft.comAnonymous
March 15, 2004
One step I missed here was to run "devenv /setup" after building all the DLLs and putting them in the right place. You need to do this to tell VS to rebuild the table of Commands and the UI elements associated with those commands.Anonymous
April 01, 2004
I would like to use custom command bitmaps, but from a vsnet macro instead of an addin. Is this possible? I have created a DLL for the satellite DLL. Since the DLL is associated with macros, instead of an addin, where should it be defined in the registry? Thanks!
fspafford@yahoo.comAnonymous
July 04, 2004
Any idea how to get a disabled icon set here? Disabled icons just show solid grey, which doesn't quite fit the pro look :-}...Anonymous
April 25, 2006
hi
i am working on the same problem but till now i didn't get any result. can u tell me is there any problem on what u give in the tutorial
Thanks
koteswarAnonymous
June 12, 2006
Great! I've being looking for this for a long!!!Anonymous
May 12, 2008
Hello! I have VS 2005 and there is no "[HKEY_CURRENT_USERSOFTWAREMicrosoftVisualStudio7.1AddInsMyAddin9.Connect]" There is no "AddIns" section at all, only [HKEY_CURRENT_USERSoftwareMicrosoftVisualStudio8.0PreloadAddinStateManaged] !!! Shell I create "AddInsMyAddin.Connect" manually or add "SatelliteDLLName" and "SatelliteDLLPath" to another section ???? Thanks!Anonymous
January 21, 2009
PingBack from http://www.keyongtech.com/2406002-add-in-migration-vs-2003-a