Click here to Skip to main content
15,895,746 members
Articles / Desktop Programming / MFC

Color components editor control

Rate me:
Please Sign up or sign in to vote.
4.93/5 (11 votes)
2 Apr 20025 min read 79.7K   2.7K   31  
This control lets you edit RGB or HSL components of a color, like in Paint Shop Pro.
// This is a part of the GWC Library
// Copyright (C) 2001-2002 Aircom software
// All rights reserved.
//
// This source code can be used, distributed or modified
// free of charge only if this header copyright is not removed.
// In other cases, contact us at:
// web:   http://www.aircom.org
// email: info@aircom.org
//

#ifndef __GWCCOLORCOMPONENTEDITCTRL_H
#define __GWCCOLORCOMPONENTEDITCTRL_H

#include "GWCCCELink.h"

// Use this as the classname when inserting this control as a custom control
// in the MSVC++ dialog editor
#define COLORCOMPEDITCTRL_CLASSNAME    _T("ColorCompEditCtrl")  // Window class name

extern UINT WM_GWCCOLORCOMPEDIT_CHANGED;

class GWCColorComponentSet;
class GWCGradientColorWnd;

class GWCCCECTRL_LINKAGE GWCColorComponentEditCtrl : public CWnd
{
	// Constants
	public:
		typedef enum { RED, GREEN, BLUE, HUE, SAT, LIGHT };

	// Construction
	public:
		GWCColorComponentEditCtrl();
		virtual ~GWCColorComponentEditCtrl();

	// Attributes
	protected:
		// The central edit control of this window. Allows the user to edit manually the
		// component value.
		CEdit m_edit;

		// A utility spin control to change the value shown in the edit control with
		// a click of the mouse.
		CSpinButtonCtrl m_spin;

		// Color of the bar that is displayed just under the edit control.
		COLORREF m_colorBar;

		// True when the left mouse button is pressed on the right button.
		bool m_buttonPushed;

		// Value maintained by this control (0 .. 255)
		unsigned char m_value;

		// Type of this color component. Values are in the enumeration above.
		int m_type;

		// Internal variable used to know if the user typed a new character (false) or if
		// the value is changed by a method call (true).
		bool m_externalEditUpdate;

		// True when a mouse drag is performed on the color bar.
		bool m_dragInProgress;

		// An object that keeps track of all control components of one color, including ourself.
		GWCColorComponentSet* m_ownerSet;

		// Pointer to a static window poped up when clicking on the right button.
		GWCGradientColorWnd* m_gradientChooser;

		// Internal variable used to know if the control gains the focus by clicking
		// on the right button.
		bool m_buttonFocus;

	// Operations
	public:
		BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);
		void SetOwnerSet(GWCColorComponentSet* owner);
		GWCColorComponentSet* GetOwnerSet();
		void SetType(int type);
		int GetType();
		void SetColorBar(COLORREF color);
		void SetColorBar(BYTE hue, BYTE sat, BYTE light);
		void SetValue(unsigned char value);
		unsigned char GetValue();
		void OnColorChanged(COLORREF color);
		void OnColorChanged(BYTE hue, BYTE sat, BYTE light);
		CRect GetButtonRect();

	protected:
		BOOL RegisterWindowClass();
		CRect GetColorRect();
		void UpdateEditValue(CString& str);

	// Overrides
		// ClassWizard generated virtual function overrides
		//{{AFX_VIRTUAL(GWCColorComponentEditCtrl)
	protected:
	virtual void PreSubclassWindow();
	virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
	//}}AFX_VIRTUAL

	// Generated message map functions
	protected:
	//{{AFX_MSG(GWCColorComponentEditCtrl)
	afx_msg void OnPaint();
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	afx_msg void OnKillFocus(CWnd* pNewWnd);
	afx_msg void OnSetFocus(CWnd* pOldWnd);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // __GWCCOLORCOMPONENTEDITCTRL_H

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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