This is a very brief note on associating a bitmap image with a component or user control from a Managed C++ project. The C# technique is more straightforward and is already documented in the standard help. Doing this in C++ is initially a pain, because there are few (if any) sample projects around, that do this. It took me quite a while to find a post on one of the newsgroups that provided the solution. Once you know what to look for, you'll find the right posts, so the idea of this article is just to provide a quick hit for people searching on ToolboxBitmap.
Write a Managed C++ component (i.e. derived from System::ComponentModel::Component) or some variation of user control that has a customized toolbox bitmap associated with it. The bitmap should be embedded as a resource (not specified in a run-time distributed file).
The bitmap must be added as an embedded resource. (Various posts on the newsgroups describe using .resx files - I couldn't get this to work).
Namespace.ClassName.bmp
The .NET framework relies on this naming convention to find your resource when displaying the bitmap on the toolbox.
You need to add ToolboxBitmap attribute to your class. For example, if you have the following class:
__gc public class Port : public System:: System::ComponentModel::Component
...add the attribute as follows:
[ToolboxBitmap(__typeof(Port))]
__gc public class Port : public System:: System::ComponentModel::Component
You must also forward declare the class, otherwise the compiler will fail. E.g.:
__gc public class Port;[ToolboxBitmap(__typeof(Port))]
__gc public class Port : public System:: System::ComponentModel::Component
The ToolboxBitmap attribute resides in the System::Drawing namespace, so you may need to add a using clause at the top of the file. E.g.:
using namespace System::Drawing;
That's it. You can now build the project.
Once the project is built, close the solution. Then, on the Toolbox right-click and select Add/Remove Items. On the .NET Framework Components page, select Browse and find your DLL assembly. Select it, and close the Customize Toolbox dialog. A greyed-out image of your component will appear on the toolbox list. If a greyed out 'cog' is shown, it means that the framework has failed to find your image (see diagnostics below). Otherwise, your component is ready for dragging-and-dropping.
If you find that the icon does not show correctly on the toolbar, there is a quick way to determine if it is visible from your assembly.
.mresource public CDS.ParallelPortDrv.Port.bmp
{
}
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||