Click here to Skip to main content
15,896,557 members
Articles / Desktop Programming / MFC

XColorDialog - an MFC color picker control that displays a color hexagon and a color spectrum

Rate me:
Please Sign up or sign in to vote.
4.94/5 (9 votes)
5 Apr 2008CPOL4 min read 64K   2.5K   44  
XColorDialog displays a color hexagon and a color spectrum that allows user selection, and provides APIs for color based on RGB and HSL color models.
// CXDC.h  Version 1.0
//
// Author:  Hans Dietrich
//          hdietrich@gmail.com
//
// History
//     Version 1.0 - 2008 March 12
//     - Initial public release
//
// License:
//     This software is released into the public domain.  You are free to use
//     it in any way you like, except that you may not sell this source code.
//
//     This software is provided "as is" with no expressed or implied warranty.
//     I accept no liability for any damage or loss of business that this 
//     software may cause.
//
// Public Members:
//              NAME                             DESCRIPTION
//   ---------------------------  ---------------------------------------------
//   Construction
//      CXDC()                    Ctor
//      CXDC(HWND)                Ctor from HWND
//   Attributes
//      HDC()                     Operator HDC - returns HDC
//      m_hDC                     HDC member variable
//   Operations
//      Attach()                  Attaches a HDC
//      Detach()                  Detaches HDC
//      GetPixel()                Gets RGB color at pixel
//      SetPixelV()               Sets RGB color
//      BitBlt()                  Copies bits from source DC to this DC
//      FillSolidRect()           Fills rect with RGB color
//      CreateCompatibleDC()      Creates a memory DC compatible with specified device
//      CreateCompatibleBitmap()  Creates a bitmap compatible with the device that 
//                                is associated with the specified DC
//      SelectObject()            Selects an object into the specified DC
//
///////////////////////////////////////////////////////////////////////////////

#ifndef CXDC_H
#define CXDC_H

#include <crtdbg.h>

#pragma warning(disable : 4127)	// for _ASSERTE: conditional expression is constant

#ifndef NO_COLOR
#define NO_COLOR ((COLORREF)-1)
#endif

class CXDC
{
// Construction
public:
	CXDC() : m_hDC(0), m_hWnd(0), m_bRelease(FALSE), m_bDelete(FALSE) {}
	CXDC(HWND hWnd) : m_hDC(0), m_hWnd(0), m_bRelease(FALSE), m_bDelete(FALSE)
	{
		m_hWnd = hWnd;
		_ASSERTE(m_hWnd && IsWindow(m_hWnd));
		if (m_hWnd && IsWindow(m_hWnd))
		{
			m_hDC = ::GetDC(m_hWnd);
			_ASSERTE(m_hDC);
			m_bRelease = m_hDC != 0;
		}
	}
	CXDC(HDC hdc) : m_hDC(hdc), m_hWnd(0), m_bRelease(FALSE), m_bDelete(FALSE) {}

	virtual ~CXDC()
	{
		if (m_hDC && m_bRelease)
		{
			::ReleaseDC(m_hWnd, m_hDC);
		}
		if (m_hDC && m_bDelete)
		{
			::DeleteDC(m_hDC);
		}
		m_hDC = 0;
		m_hWnd = 0;
	}

// Attributes
public:
	HDC m_hDC;
	operator HDC() const
	{
		return this == 0 ? 0 : m_hDC;
	}

// Operations
public:
	BOOL Attach(HDC hdc)
	{
		BOOL rc = hdc != 0;
		_ASSERTE(hdc);
		_ASSERTE(m_hDC == 0);
		m_hDC = hdc;
		m_bRelease = FALSE;
		m_bDelete = FALSE;
		return rc;
	}

	HDC Detach()
	{
		HDC rc = m_hDC;
		m_hDC = 0;
		return rc;
	}

	COLORREF GetPixel(int x,	// x-coordinate of pixel
					  int y)	// y-coordinate of pixel
	{
		COLORREF cr = NO_COLOR;
		_ASSERTE(m_hDC);
		if (m_hDC)
			cr = ::GetPixel(m_hDC, x, y);
		return cr;
	}

	BOOL SetPixelV(int x,		// x-coordinate of pixel
				   int y,		// y-coordinate of pixel
				   COLORREF cr)	// new pixel color
	{
		BOOL rc = FALSE;
		_ASSERTE(m_hDC);
		if (m_hDC)
			rc = ::SetPixelV(m_hDC, x, y, cr);
		return rc;
	}

	BOOL BitBlt(int x,			// x-coord of destination upper-left corner
				int y,			// y-coord of destination upper-left corner
				int nWidth,		// width of destination rectangle
				int nHeight,	// height of destination rectangle
				CXDC *pSrcDC,	// pointer to source CXDC object
				int xSrc,		// x-coordinate of source upper-left corner
				int ySrc,		// y-coordinate of source upper-left corner
				DWORD dwRop)	// raster operation code
	{
		BOOL rc = FALSE;
		_ASSERTE(m_hDC);
		_ASSERTE(pSrcDC);
		if (m_hDC && pSrcDC && pSrcDC->m_hDC)
			rc = ::BitBlt(m_hDC, x, y, nWidth, nHeight, 
						pSrcDC->m_hDC, xSrc, ySrc, dwRop);
		return rc;
	}

	void FillSolidRect(int x, int y, int cx, int cy, COLORREF cr)
	{
		_ASSERTE(m_hDC);
		if (m_hDC)
		{
			::SetBkColor(m_hDC, cr);
			RECT rect = { x, y, x + cx, y + cy };
			::ExtTextOut(m_hDC, 0, 0, ETO_OPAQUE, &rect, 0, 0, 0);
		}
	}

	BOOL CreateCompatibleDC(HDC hdc)
	{
		BOOL rc = FALSE;
		_ASSERTE(hdc);
		_ASSERTE(m_hDC == 0);
		if (hdc)
		{
			m_hDC = ::CreateCompatibleDC(hdc);
			_ASSERTE(m_hDC);
			rc = m_hDC != 0;
			m_bDelete = TRUE;
			m_bRelease = FALSE;
		}
		return rc;
	}

	BOOL CreateCompatibleDC(CXDC *pDC)
	{
		BOOL rc = FALSE;
		_ASSERTE(pDC && pDC->m_hDC);
		_ASSERTE(m_hDC == 0);
		if (pDC && pDC->m_hDC)
		{
			m_hDC = ::CreateCompatibleDC(pDC->m_hDC);
			_ASSERTE(m_hDC);
			rc = m_hDC != 0;
			m_bDelete = TRUE;
			m_bRelease = FALSE;
		}
		return rc;
	}

	HBITMAP CreateCompatibleBitmap(int nWidth, int nHeight)
	{
		HBITMAP hbitmap = 0;
		_ASSERTE(m_hDC);
		if (m_hDC)
			hbitmap = ::CreateCompatibleBitmap(m_hDC, nWidth, nHeight);
		return hbitmap;
	}

	HGDIOBJ SelectObject(HGDIOBJ hgdiobj)
	{
		HGDIOBJ old = 0;
		_ASSERTE(m_hDC);
		if (m_hDC)
			old = ::SelectObject(m_hDC, hgdiobj);
		return old;
	}

// Implementation
protected:
	HWND	m_hWnd;
	BOOL	m_bRelease;
	BOOL	m_bDelete;
};

#endif //CXDC_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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions