Click here to Skip to main content
15,888,351 members
Articles / Desktop Programming / MFC
Article

A Generic Well Control (ColorPicker, ArrowPicker and more)

Rate me:
Please Sign up or sign in to vote.
4.00/5 (9 votes)
24 Nov 20023 min read 73.7K   1.8K   30   7
Provides and demonstrates the use of a generic "well" control that can be used to facilitate color-pickers, hash-style selectors amongst others

Sample Image - maximum width is 600 pixels Sample Image Sample Image - maximum width is 600 pixels Sample Image - maximum width is 600 pixels

Introduction

I need a color picker for a vector drawing program I am developing. After trying several color picker examples from CodeGuru and CodeProject, I found what I really need is a general 'well' control. It can not only serve as the base class of color picker, but also others. Also, I want this well control can be used as button in a dialog or from view, as well as a popup window. I also think it is good to use bitmap resource to draw each well if owner draw is not necessary.

Using the code

Copy all the *.h file and *.cpp to your project. If you want to use the Office-style color picker, please add the resource for the dialogs and bitmaps to your project also. There are four well controls in the demo project:

CSVizColorWell
, CSVizArrowWell, CSVizLineStyleWell, CSVizHatchWell. Because the color well is not only choosing color but also the pattern of filling (GD+ HatchBrush), you need to download Microsoft SDK for the GDI+ support. These four well controls are derived from
CSVizWellCtrl.
If you want to implement your own well control, you can derived your CXXXWell from CSVizWellCtrl. In most of cases, you only need to override the following three virtual functions and constructor:

virtual void Initialise ();
virtual CString GetWellName (int nIndex) { return "well"; }
virtual void DrawCellContent (CDC* pDC, int nIndex, CRect rect,BYTE state);

In Initialise(), you define default size of the well, number of columns, number ofrows, and the look and feel of the well control. There are currently only two kinds of look and feel for this well control, one is common Windows control look and one is Office 2000 style. GetWellName() is a very simple function to return a name of current select well.  The key function of this well control is DrawCellContent(), which draws content of each well.

There are two scenarios to use CSVizWellCtrl. One use is in popup windows and the other is use in dialogs. The four example well controls demonstrate both of them. The CSVizArrowWell is also good example code to show how to use bitmap resource in drawing the cell content.

  1. To use in a popup window (like the CSVizColorPopupWell,

    CSVizArrowWell
    ) you have to use helper classes to pup up the well control windows. For CSVizColorWell , it is CColorPikcer and for CSVizArrowWell , it is CArrowPicker . You should create the well control window in the OnClicked() function of
    CColorPikcer
    and CArrowPicker . In this case the parent window of well control is the helper class window.

    new CSVizArrowWell(CPoint(rect.left, rect.bottom), // Point to display popup
    	GetArrow(), // Selected arrow
    	this , // parent
    	WELL_STYLE_OFFICE | WELL_STYLE_AUTODELETE | WELL_STYLE_POPUP
    );
  2. To use as a button. You don't need to create the control in your code. Using class wizard or manually add

    DDX_Control(pDX, IDC_YOUR_WELLBTN, m_Yourwellbtn);

    to the dialog's DoDataExchange(CDataExchange* pDX) function. And in this case, well control's parent is the dialog class.

Points of Interest

This well control is derived from CButton instead of CWnd directly. It is convenient when you use it in dialog based application, because you can use the standard DDX_Control() function. It can also be used as popup window like

CSVizColorWell 
will do.

Using bitmap resources, this well control can be used as toolbar in a dialog or you may create a control bar with a well control in it.

The CSVizColorWell itself might be useful for customizing the filling brush of GDI+. It supports the configuration of GLinearGradientBrush, PathGradientBrush, TextureBrush and HatchBrush.

To Do List

  • Record customized color in the color well
  • Integrate color dropper like Photoshop and FrontPage 2000

Acknowledgments

Some great ideas of the color picker and general well control come from the posts of Chris Maunder,  Alexander Bischofberger, Paul Wilkerson, and Geir Arne Trillhus. All appreciative feedback should also be passed to them, complaints to me. Those contributions can also be found in CodeProject.
 

License Agreement

This code may be used in compiled form in any way you desire (including commercial use). The code may be redistributed unmodified by any means providing it is not sold for profit without the authors written consent, and providing that this notice and the authors name and all copyright notices remains intact.

This software is provided "as is" without express or implied warranty. Use it at your own risk!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRelease EXE doesn't show arrows Pin
Super Garrison9-Feb-11 17:06
Super Garrison9-Feb-11 17:06 
GeneralI will be happy to know how to programm a modal loop for a menu Pin
sanjit_rath3-Oct-04 8:11
sanjit_rath3-Oct-04 8:11 
QuestionWell control? Pin
Philippe Lhoste3-Dec-02 0:47
Philippe Lhoste3-Dec-02 0:47 
Call me stupid or non-native English speaker Wink | ;-) but I don't know what is a well control...
So what is it and why is it called this way?

Thanks.

Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/

AnswerRe: Well control? Pin
tao6-Dec-02 11:18
tao6-Dec-02 11:18 
AnswerRe: Well control? Pin
OMalleyW19-Aug-05 5:09
OMalleyW19-Aug-05 5:09 
GeneralCool Pin
NormDroid25-Nov-02 20:59
professionalNormDroid25-Nov-02 20:59 
GeneralMFC7.0 Issue Pin
tao25-Nov-02 14:03
tao25-Nov-02 14:03 

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.