Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Generic Well Control (ColorPicker, ArrowPicker and more)

0.00/5 (No votes)
24 Nov 2002 1  
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