Button color picker (like Excel)

I recently wanted to allow certain color selection in one of my 16 bits projects like Excel offers in it's toolbar. The following code acomplishes exactly that. My projet is a 16 bits application so the exemple is in 16 bits build with MSVC 1.52, but I sure you can recompile it in 32 bits.

Just include the ComboBtn.h and ComboBtn.cpp files in your project and create a control member variable. Here's a preview of the button and the implementation in a toolbar.

Download sample and source 85KB

// mainfrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////
#include "combobtn.h"

class CMainFrame : public CMDIFrameWnd
{
	DECLARE_DYNAMIC(CMainFrame)
public:
	CMainFrame();

// Attributes
public:
	CButtonCombo m_colorBtn;
	CButtonCombo m_bitmapBtn;

// Operations
public:

// Implementation
public:
	virtual ~CMainFrame();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:  // control bar embedded members
	CStatusBar  m_wndStatusBar;
	CToolBar    m_wndToolBar;

// Generated message map functions
protected:
	//{{AFX_MSG(CMainFrame)
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
		// NOTE - the ClassWizard will add and remove member functions here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

// in mainfrm.cpp

// toolbar buttons - IDs are command buttons
static UINT BASED_CODE buttons[] =
{
	// same order as in the bitmap 'toolbar.bmp'
	ID_FILE_NEW,
	ID_FILE_OPEN,
	ID_FILE_SAVE,
		ID_SEPARATOR,
	ID_EDIT_CUT,
	ID_EDIT_COPY,
	ID_EDIT_PASTE,
		ID_SEPARATOR,
		ID_SEPARATOR,	// Color Button
		ID_SEPARATOR,
		ID_SEPARATOR,	// Bitmap Button
		ID_SEPARATOR,
	ID_FILE_PRINT,
	ID_APP_ABOUT,
};

static UINT BASED_CODE indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

#define IDC_BTNCOLOR	1000
#define IDC_BTNBITMAP	1001

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	if (!m_wndToolBar.Create(this) ||
		!m_wndToolBar.LoadBitmap(IDR_MAINFRAME) ||
		!m_wndToolBar.SetButtons(buttons,
		  sizeof(buttons)/sizeof(UINT)))
	{
		TRACE("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	m_wndToolBar.SetButtonInfo(8, IDC_BTNCOLOR, TBBS_SEPARATOR, 48 );
	m_wndToolBar.SetButtonInfo(10, IDC_BTNBITMAP, TBBS_SEPARATOR, 48 );
	
	m_wndToolBar.SetButtonInfo(9, ID_SEPARATOR, TBBS_SEPARATOR, 12 );
	m_wndToolBar.SetButtonInfo(11, ID_SEPARATOR, TBBS_SEPARATOR, 12 );
	CRect rect;
	m_wndToolBar.GetItemRect(8, &rect);

	CString sText = ""; 
	char buffer[5];
	for (int i = 0; i < 72; i++)
		sText += "Color " + CString(itoa(i, buffer, 10)) + ((i < 71)? ";" : "");

	m_colorBtn.Create(rect, CSize(8, 6), CSize(16, 15), &m_wndToolBar, IDC_BTNCOLOR);
//	If you want the text for each color use this line 
//	m_colorBtn.Create(rect, CSize(8, 6), CSize(16, 15), &m_wndToolBar, IDC_BTNCOLOR, sText);

	sText.LoadString(IDS_STRINGCHART);	
	m_wndToolBar.GetItemRect(10, &rect);
	m_bitmapBtn.Create(rect, CSize(4, 4), IDB_CHART, 14, &m_wndToolBar, IDC_BTNBITMAP, sText);

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE("Failed to create status bar\n");
		return -1;      // fail to create
	}

	return 0;
}



Comments

  • how to keep MainFrame from losing foucs?

    Posted by Legacy on 09/18/2003 12:00am

    Originally posted by: hbai

    when the controls are clicked, the main frame and the all views lose focus,how to solve the problem?

    Reply
  • MFC Dialog

    Posted by Legacy on 02/25/2003 12:00am

    Originally posted by: aukbah

    helo,
    can you tell me mabe, how i do that with MFC Dialog?

    thanks

    Reply
  • color picker button

    Posted by Legacy on 11/05/2000 12:00am

    Originally posted by: Christoph Platz

    This seems to be some old 16 Bit stuff. Looks very good, respect! But do you have a 32Bit version of it?

    Reply
  • button color picker (like excel)

    Posted by Legacy on 03/28/2000 12:00am

    Originally posted by: Leudis

    Hola Benoif,
    I need your help. Would you send me by e-mail your source and sample
    code for button picker. I can�t download from the codeguru page

    Thanks, in advance,
    Leudis

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds