Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / MFC
Article

Custom 32-bit RGBA Color MFC Control

Rate me:
Please Sign up or sign in to vote.
4.72/5 (17 votes)
30 Apr 2003CPOL3 min read 65.1K   3.8K   26   2
A custom MFC control that allows the user to manipulate 32-bit RGBA colour attributes.

Sample Image - RGBA_Colour_Control.jpg

Introduction

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!

Background

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!

History

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!

License

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


Written By
Software Developer (Senior)
Malta Malta
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAmazing Work! Pin
_Future_28-Oct-14 9:06
_Future_28-Oct-14 9:06 
GeneralTypo: include headers in article are missing 'a' Pin
DonGuitar7-Dec-05 12:22
DonGuitar7-Dec-05 12: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.