Click here to Skip to main content
13,299,898 members (62,919 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 30 Apr 2003

Custom 32-bit RGBA Color MFC Control

, 30 Apr 2003
Rate this:
Please Sign up or sign in to vote.
A custom MFC control that allows the user to manipulate 32-bit RGBA colour attributes.
<!-- Article image -->

Sample Image - RGBA_Colour_Control.jpg

<!-- 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 RgbColorCtrl.h, RgbColorCtrlDlg.h and 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 CStatic to CRgbaColorCtrl.

To set the control's colour value, use the SetValues() method:

// set colour to translucent orange (192 = 75% opacity)
// m_rgbaAmbient.SetValues(255, 128, 0, 192);

To read the control's colour value, use the GetRed(), GetGreen(), GetBlue() and GetAlpha() methods:

// get colour components
// BYTE bRed = m_rgbaAmbient.GetRed();
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!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Colin Vella
Software Developer (Senior)
Malta Malta
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionAmazing Work! Pin
_Future_28-Oct-14 10:06
member_Future_28-Oct-14 10:06 
GeneralTypo: include headers in article are missing 'a' Pin
DonGuitar7-Dec-05 13:22
memberDonGuitar7-Dec-05 13:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 1 May 2003
Article Copyright 2003 by Colin Vella
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid