Click here to Skip to main content
Click here to Skip to main content
Go to top

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 -->

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! Smile | :)

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)

Share

About the Author

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

Comments and Discussions

 
GeneralTypo: include headers in article are missing 'a' PinmemberDonGuitar7-Dec-05 12:22 

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

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

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