<!-- Article image -->
<!-- Add the rest of your HTML here -->
This article details a custom MFC control that allows the user to manipulate 32-bit RGBA colour attributes. More specifically, the control allows manipulation of the red, blue, green and alpha (opacity) components of a color.
While there are a couple of color picker dialogs out there, including the standard dialog, there is no control that allows you to specify an alpha component for a colour (or at least I was too lazy/incompetent to find one!).
Clicking the control (a sunken-edged box showing the color) produces a drop-down window allowing the user to change the reg, green, blue and alpha components either via sliders or by specifying values (0-255) for each component. The current color is displayed in a box within the dropdown. Once the colour is chosen, clicking 'OK' confirms the choice and the RGBA color control changes accordingly. Otherwise, if 'Cancel' is clicked, the original colour is retained.
The control was written and tested (just a little bit!) with VS.NET running on Win2000 and on WinXP. If anyone wishes to try it on VS 6 and other Windows platforms please go ahead and let us all know!
This is all optional but here goes anyway!
I basically needed this control for a 3D modelling tool I am working on and:
- I didn't find a suitable control
- I have never written a custom control
- I have never created an MFC extention DLL
So I decided it was time to extend my knowledge.. and this is the result.
Using the code
It is up to the developer whether to include the control's sources and dialog resource or whether to use the precompiled library headers and files. I recommend going for the latter option and that is the method I will describe here:
- Include the directory containing the header files
resource.h within the C++ compiler options.
- Add the import library
RgbaCtrl.lib in the 'Additional Dependences' setting within the C++ linker options (Input section)
- Include the directory containing the import library RgbaCtrl.lib in the 'Additional Library Directories' setting within the C++ linker options (General section)
- Include the header file
RgbColorCtrl.h in the dialog/view/child window class header file.
- Add a static text control that will represent the RGBA colour control, but change the control ID from IDC_STATIC to a more meaningful name, e.g.
IDC_RGBA_AMBIENT. Also, make sure that the
Notify property is set to
TRUE, otherwise your control will not respond to the mouse!!
- Use the class wizard to add a control variable for the control, but change the type from
To set the control's colour value, use the
To read the control's colour value, use the
BYTE bGreen = m_rgbaAmbient.GetGreen();
BYTE bBlue = m_rgbaAmbient.GetBlue();
BYTE bAlpha = m_rgbaAmbient.GetAlpha();
Points of Interest
The apparent translucency effect in the RGBA colour control is achieved by drawing a chequered region whose alternating dark and light colours are a function of the RGB component of the color, the alpha blending value and the grayscale values of the chequered region. In other words, I'm not relying on any fancy alpha blending functions thus making it easer to port this control to earlier VS releases.
The color display within the slider dropdown is in itself an RGBA color control with the
Notify property disabled to prevent a slider dropdown within the dropdown ad infinitum!
For the time being I have no plans to release further functionality as the control satisfies my requirements. However I have provided all the code so you may freely extend the control. If you come up with any snazzy features please post it here!