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

CColorBox

Rate me:
Please Sign up or sign in to vote.
4.55/5 (17 votes)
3 Aug 2004Ms-RL2 min read 130.7K   5.2K   57  
A very simple button that shows a color and lets the user change it by clicking on the button.
// ColorBoxSampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ColorBoxSample.h"
#include "ColorBoxSampleDlg.h"
#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorBoxSampleDlg dialog

CColorBoxSampleDlg::CColorBoxSampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CColorBoxSampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CColorBoxSampleDlg)
	m_enable1 = TRUE;
	m_change1 = TRUE;
	m_selected1 = FALSE;
	m_enable2 = TRUE;
	m_enable3 = TRUE;
	m_selected2 = FALSE;
	m_selected3 = FALSE;
	m_change2 = TRUE;
	m_change3 = TRUE;
	m_selected = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_boxes[0] = &m_color1;
	m_boxes[1] = &m_color2;
	m_boxes[2] = &m_color3;

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CColorBoxSampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CColorBoxSampleDlg)
	DDX_Control(pDX, IDC_BUTTON_LAMP, m_lamp);
	DDX_Control(pDX, IDC_BUTTON_COLOR3, m_color3);
	DDX_Control(pDX, IDC_BUTTON_COLOR2, m_color2);
	DDX_Control(pDX, IDC_BUTTON_COLOR1, m_color1);
	DDX_Check(pDX, IDC_CHECK_ENABLE1, m_enable1);
	DDX_Check(pDX, IDC_CHECK_ALLOWCHANGE1, m_change1);
	DDX_Check(pDX, IDC_CHECK_SELECTED1, m_selected1);
	DDX_Check(pDX, IDC_CHECK_ENABLE2, m_enable2);
	DDX_Check(pDX, IDC_CHECK_ENABLE3, m_enable3);
	DDX_Check(pDX, IDC_CHECK_SELECTED2, m_selected2);
	DDX_Check(pDX, IDC_CHECK_SELECTED3, m_selected3);
	DDX_Check(pDX, IDC_CHECK_ALLOWCHANGE2, m_change2);
	DDX_Check(pDX, IDC_CHECK_ALLOWCHANGE3, m_change3);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CColorBoxSampleDlg, CDialog)
	//{{AFX_MSG_MAP(CColorBoxSampleDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_CONTROL_RANGE(BN_CLICKED, IDC_CHECK_ENABLE1, IDC_CHECK_ENABLE3, OnSomeEnablebox)
	ON_CONTROL_RANGE(BN_CLICKED, IDC_CHECK_ALLOWCHANGE1, IDC_CHECK_ALLOWCHANGE3, OnSomeAllowChangebox)
	ON_CONTROL_RANGE(BN_CLICKED, IDC_CHECK_SELECTED1, IDC_CHECK_SELECTED3, OnSomeSelectedbox)	
	
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorBoxSampleDlg message handlers

BOOL CColorBoxSampleDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_color1.SetColor(RGB(255,128,0));
	m_color2.SetColor(RGB(0,128,255));
	m_color3.SetColor(RGB(128,255,0));

	for(int i=0; i<16; i++)
		m_customcolors[i] = RGB(255,255,255);

	m_color1.SetCustomColors(m_customcolors);
	m_color2.SetCustomColors(m_customcolors);
	m_color3.SetCustomColors(m_customcolors);

	SetTimer(0, 25, NULL);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CColorBoxSampleDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CColorBoxSampleDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CColorBoxSampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



void CColorBoxSampleDlg::OnSomeEnablebox(UINT id)
{
	UpdateData();
	BOOL en[] = {m_enable1, m_enable2, m_enable3};
	UINT box = id-IDC_CHECK_ENABLE1;

	m_boxes[box]->EnableWindow(en[box]);
}

void CColorBoxSampleDlg::OnSomeAllowChangebox(UINT id)
{
	UpdateData();
	BOOL change[] = {m_change1, m_change2, m_change3};
	UINT box = id-IDC_CHECK_ALLOWCHANGE1;

	m_boxes[box]->SetAllowChange(change[box]);
}

void CColorBoxSampleDlg::OnSomeSelectedbox(UINT id)
{
	UpdateData();
	BOOL selected[] = {m_selected1, m_selected2, m_selected3};
	UINT box = id-IDC_CHECK_SELECTED1;

	m_boxes[box]->SetSelected(selected[box]);
}

void CColorBoxSampleDlg::OnTimer(UINT nIDEvent) 
{
	static double x=0.0;
	x+= 0.05;

	int r	= (int) (120 + 100*sin(x));
	int g	= (int) (150 + 90*sin(x/0.8+0.5));
	int b	= (int) (160 + 50*sin(x*1.5+0.9));

	m_lamp.SetColor( RGB(r,g,b) );
	
	CDialog::OnTimer(nIDEvent);
}

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 Microsoft Reciprocal License


Written By
PEK
Sweden Sweden
PEK is one of the millions of programmers that sometimes program so hard that he forgets how to sleep (this is especially true when he has more important things to do). He thinks that there are not enough donuts in the world. He likes when his programs works as they should do, but dislikes when his programs is more clever than he is.

Comments and Discussions