HeaderPane
Main
Hide
Share Link
Print

Colors Sample (C++)

This sample illustrates using SftBox/OCX for color selection.

The source code is located at C:\Program Files (x86)\Softelvdm\SftBox OCX 6.0\Samples\VC++\Colors\ColorsDlg.cpp.

// ColorsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Colors.h"
#include "ColorsDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CColorsDlg dialog

CColorsDlg::CColorsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CColorsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CColorsDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CColorsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CColorsDlg)
	DDX_Control(pDX, IDC_SFTBOX1, m_Box1);
	DDX_Control(pDX, IDC_SFTBOX2, m_Box2);
	DDX_Control(pDX, IDC_SFTBOX3, m_Box3);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CColorsDlg, CDialog)
	//{{AFX_MSG_MAP(CColorsDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorsDlg message handlers

void CColorsDlg::AddColor(ISftBoxPtr vCombo, BOOL fSmallSample, LPCTSTR lpszDescription, COLORREF color)
{
    long index = vCombo->Items->Add(lpszDescription);      // add a new item
    ISftBoxCellPtr OneCell = vCombo->Cell[index][0]; // get the first cell

    if (fSmallSample) {
        OneCell->Image->Height = 13;
        OneCell->Image->Width = 26;
    } else {
        OneCell->Image->Height = 13;
        OneCell->Image->Width = 13;
    }
    OneCell->Image->SetColorSample(color, RGB(0,0,0)); // set the cell's color sample
    // We'll save the color value in the cell's Data property so
    // we can use the Items.FindCellData method if needed
    OneCell->Data = color;
}

void CColorsDlg::AddColors(ISftBoxPtr vCombo, BOOL fSmallSample)
{
    AddColor(vCombo, fSmallSample, _T("Black"), RGB(0,0,0));
    AddColor(vCombo, fSmallSample, _T("Blue"), RGB(0,0,255));
    AddColor(vCombo, fSmallSample, _T("Cyan"), RGB(0,255,255));
    AddColor(vCombo, fSmallSample, _T("Green"), RGB(0,255,0));
    AddColor(vCombo, fSmallSample, _T("Magenta"), RGB(255,0,255));
    AddColor(vCombo, fSmallSample, _T("Red"), RGB(255,0,0));
    AddColor(vCombo, fSmallSample, _T("White"), RGB(255,255,255));
    AddColor(vCombo, fSmallSample, _T("Yellow"), RGB(255,255,0));
    AddColor(vCombo, fSmallSample, _T("Scroll bar color"), COLOR_SCROLLBAR | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Desktop color"), COLOR_DESKTOP | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Color of the title bar for the active window"), COLOR_ACTIVECAPTION | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Color of the title bar for the inactive window"), COLOR_INACTIVECAPTION | 0x80000000L);

    AddColor(vCombo, fSmallSample, _T("Menu background color"), COLOR_MENUBAR | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Window background color"), COLOR_WINDOW | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Window frame color"), COLOR_WINDOWFRAME | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Color of text on menus"), COLOR_MENUTEXT | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Color of text in windows"), COLOR_WINDOWTEXT | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Color of text in caption, size box, and scroll arrow"), COLOR_CAPTIONTEXT | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Border color of active window"), COLOR_ACTIVEBORDER | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Border color of inactive window"), COLOR_INACTIVEBORDER | 0x80000000L);

    AddColor(vCombo, fSmallSample, _T("Background color of multiple-document interface (MDI) applications"), COLOR_APPWORKSPACE | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Background color of items selected in a control"), COLOR_HIGHLIGHT | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Text color of items selected in a control"), COLOR_HIGHLIGHTTEXT | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Color of shading on the face of command buttons"), COLOR_BTNFACE | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Color of shading on the edge of command buttons"), COLOR_BTNSHADOW | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Grayed (disabled) text"), COLOR_GRAYTEXT | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Text color on push buttons"), COLOR_BTNTEXT | 0x80000000L);

    AddColor(vCombo, fSmallSample, _T("Color of text in an inactive caption"), COLOR_INACTIVECAPTIONTEXT | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Highlight color for 3D display elements"), COLOR_3DHILIGHT | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Darkest shadow color for 3D display elements"), COLOR_3DDKSHADOW | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Second lightest of the 3D colors after vb3Dhighlight"), COLOR_3DLIGHT | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Color of text face"), COLOR_3DFACE | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Color of text shadow"), COLOR_3DSHADOW | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Color of text in ToolTips"), COLOR_INFOTEXT | 0x80000000L);
    AddColor(vCombo, fSmallSample, _T("Background color of ToolTips"), COLOR_INFOBK | 0x80000000L);
}

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

    SftDarkMode_ApplyToDialog(m_hWnd);
    for (CWnd* pWnd = GetWindow(GW_CHILD); pWnd != NULL; pWnd = pWnd->GetWindow(GW_HWNDNEXT)) {
        ISftBoxPtr pBox(pWnd->GetControlUnknown());
        if (pBox != NULL) {
            pBox->DarkMode = darkSftBoxAuto;                       // follow the Windows dark mode setting
            pBox->HighContrastMode = highContrastSftBoxAuto;      // follow the Windows High Contrast setting
        }
    }

	// 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
	
    // Initialize combo box 1
    ISftBoxPtr vCombo1 = m_Box1.GetControlUnknown();

    vCombo1->putref_Font(NULL); // use the form's default font
    AddColors(vCombo1, TRUE);
    // Select "red"
    long index;
    short column;
    vCombo1->Items->FindCellData(RGB(255,0,0), 0, -1, 0, &index, &column);
    vCombo1->Items->Selection = index;
    // make sure we get a horizontal scroll bar
    vCombo1->Items->RecalcHorizontalExtent(0);

    // Initialize combo box 2
    ISftBoxPtr vCombo2 = m_Box2.GetControlUnknown();

    vCombo2->putref_Font(NULL); // use the form's default font
    AddColors(vCombo2, FALSE);
    // Select "red"
    vCombo2->Items->FindCellData(RGB(255,0,0), 0, -1, 0, &index, &column);
    vCombo2->Items->Selection = index;
    // make sure we get a horizontal scroll bar
    vCombo2->Items->RecalcHorizontalExtent(0);
	
    // Initialize combo box 3
    ISftBoxPtr vCombo3 = m_Box3.GetControlUnknown();

    vCombo3->putref_Font(NULL); // use the form's default font
    vCombo3->Headers->putref_Font(NULL);
    AddColors(vCombo3, TRUE);
    // Select "red"
    vCombo3->Items->FindCellData(RGB(255,0,0), 0, -1, 0, &index, &column);
    vCombo3->Items->Selection = index;
    // make sure we get a horizontal scroll bar
    vCombo3->Items->RecalcHorizontalExtent(0);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CColorsDlg::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 CColorsDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CColorsDlg::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
    INT_PTR dmResult;
    if (SftDarkMode_HandleDialogMessage(m_hWnd, message, wParam, lParam, &dmResult)) {
        if (pResult) *pResult = dmResult;
        return TRUE;
    }
    return CDialog::OnWndMsg(message, wParam, lParam, pResult);
}

Last Updated 11/22/2026 - (email)
© 2026 Softel vdm, Inc.