HeaderPane
Main
Hide
Share Link
Print

Colors Sample (C++/MFC)

This sample illustrates two dropdown list combo boxes used to select a color, each item showing a color sample as cell picture, hosted in an MFC dialog through CSftBox and DDX_Control. It covers registering with SftBox/DLL in InitInstance, defining two columns with column headers in the dropdown portion, a dropdown portion the user can resize with the sizing box (SetDisableNoScroll, SetShowResizer), adding items with item data, cell pictures created using Sft_SetPictureColorSample, handling the SFTBOXN_SELENDOK and SFTBOXN_SELCHANGE notifications through ON_CONTROL (the first item opens the color dialog) and dark mode support.

The source code is located at C:\Program Files (x86)\Softelvdm\SftBox DLL 6.0\Samples\MFC\Colors.

/****************************************************************************/
/* SftBox/DLL 6.0 - ComboBox Control for Windows                            */
/* Copyright (C) 1997, 2026  Softel vdm, Inc. All Rights Reserved.          */
/****************************************************************************/
/* SftBox/DLL 6.0 sample. Guide: https://softelvdm.com/Vault/Softelvdm.com/llms/SftBox-DLL-6.0.txt, API reference: https://softelvdm.com/Vault/Softelvdm.com/llms/SftBox-DLL-6.0-reference.txt - see AGENTS.md. */

#include "stdafx.h"
#include "MainDlg.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Colors

#define SYSCOLOR(index) (0x80000000L | (index))

struct COLORENTRY {
    LPCTSTR lpszName;
    LPCTSTR lpszDesc;
    COLORREF color;
};

static const COLORENTRY m_aColors[] = {
    { _T("Black"),      _T(""), RGB(0, 0, 0) },
    { _T("White"),      _T(""), RGB(255, 255, 255) },
    { _T("Red"),        _T(""), RGB(255, 0, 0) },
    { _T("Green"),      _T(""), RGB(0, 160, 0) },
    { _T("Blue"),       _T(""), RGB(0, 0, 255) },
    { _T("Yellow"),     _T(""), RGB(255, 255, 0) },
    { _T("Orange"),     _T(""), RGB(255, 140, 0) },
    { _T("Purple"),     _T(""), RGB(128, 0, 128) },
    { _T("Teal"),       _T(""), RGB(0, 128, 128) },
    { _T("Navy"),       _T(""), RGB(0, 0, 128) },
    { _T("Maroon"),     _T(""), RGB(128, 0, 0) },
    { _T("Gray"),       _T(""), RGB(128, 128, 128) },
    { _T("Silver"),     _T(""), RGB(192, 192, 192) },
    { _T("Window"),     _T("Window background"), SYSCOLOR(COLOR_WINDOW) },
    { _T("WindowText"), _T("Text in windows"), SYSCOLOR(COLOR_WINDOWTEXT) },
    { _T("Highlight"),  _T("Selected items"), SYSCOLOR(COLOR_HIGHLIGHT) },
    { _T("HighlightText"), _T("Text of selected items"), SYSCOLOR(COLOR_HIGHLIGHTTEXT) },
    { _T("BtnFace"),    _T("Face of push buttons"), SYSCOLOR(COLOR_BTNFACE) },
    { _T("InfoBk"),     _T("Tooltip background"), SYSCOLOR(COLOR_INFOBK) },
};
#define COLOR_COUNT (sizeof(m_aColors) / sizeof(m_aColors[0]))

#define CUSTOM_INDEX    0               // the first item opens the color dialog
#define SAMPLE_SIZE     13              // size of a color sample, in pixels

static COLORREF ResolveColor(COLORREF color)
{
    if (color & 0x80000000L)
        return GetSysColor((int)(color & 0xff));
    return color;
}

static void InitColumn(LPSFTBOX_COLUMN lpCol, int position, int width, LPCTSTR lpszTitle)
{
    memset(lpCol, 0, sizeof(SFTBOX_COLUMN));
    lpCol->width = width;
    lpCol->style = ES_LEFT | SFTBOX_TOOLTIP;
    lpCol->styleTitle = ES_LEFT | SFTBOX_HEADER_UP;
    lpCol->lpszTitle = (LPTSTR) lpszTitle;
    lpCol->colorBg = lpCol->colorFg = lpCol->colorBgSel = lpCol->colorFgSel = SFTBOX_NOCOLOR;
    lpCol->colorBgEnd = lpCol->colorBgSelEnd = SFTBOX_NOCOLOR;
    lpCol->colorProgress = lpCol->colorProgressEnd = SFTBOX_NOCOLOR;
    lpCol->realPos = lpCol->dispPos = position;
}

/////////////////////////////////////////////////////////////////////////////
// CMainDlg dialog

BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
    ON_CONTROL(SFTBOXN_SELENDOK, IDC_BACKGROUND, OnBackgroundSelEndOK)
    ON_CONTROL(SFTBOXN_SELENDOK, IDC_TEXT, OnTextSelEndOK)
    ON_CONTROL(SFTBOXN_SELCHANGE, IDC_BACKGROUND, OnSelChange)
    ON_CONTROL(SFTBOXN_SELCHANGE, IDC_TEXT, OnSelChange)
    ON_WM_SYSCOLORCHANGE()
END_MESSAGE_MAP()

CMainDlg::CMainDlg(CWnd* pParent) : CDialog(CMainDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    for (int i = 0 ; i < 16 ; ++i)
        m_aCustomColors[i] = RGB(255, 255, 255);
}

void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_BACKGROUND, m_Background);
    DDX_Control(pDX, IDC_TEXT, m_Text);
}

void CMainDlg::SetColorSample(CSftBox& box, LONG index, COLORREF color)
{
    // The color sample is the picture of the cell in the first column.
    SFTBOX_CELLINFOPARM CellInfo;

    memset(&CellInfo, 0, sizeof(CellInfo));
    CellInfo.index = index;
    CellInfo.iCol = 0;
    box.GetCellInfo(&CellInfo);
    Sft_SetPictureColorSample(&CellInfo.Cell.CellPicture1, SAMPLE_SIZE, SAMPLE_SIZE, color, SYSCOLOR(COLOR_WINDOWTEXT));
    box.SetCellInfo(&CellInfo);
}

void CMainDlg::InitColorList(CSftBox& box, COLORREF custom, COLORREF selected)
{
    SFTBOX_COLUMN aCols[2];
    LONG index;

    InitColumn(&aCols[0], 0, 100, _T("Color"));
    InitColumn(&aCols[1], 1, 100, _T("Description"));
    box.SetColumns(2, aCols);
    box.SetShowHeaderDropDown(TRUE);    // Column titles in the dropdown only
    box.SetDroppedHeight(12);           // Show up to 12 items
    box.SetDropDownOptimalWidth(TRUE);  // The dropdown is as wide as its columns
    box.SetDisableNoScroll(TRUE);       // Scroll bars always shown, needed for the sizing box
    box.SetShowResizer(TRUE);           // The user can resize the dropdown
    box.SetDarkMode(SFTBOX_DARKMODE_AUTO);

    // Register the size of the cell pictures before adding items.
    // All pictures in the cells of a control are the same size.
    {
        SFTBOX_CELLINFOPARM CellInfo;
        memset(&CellInfo, 0, sizeof(CellInfo));
        CellInfo.index = -1;
        CellInfo.iCol = 0;
        CellInfo.Cell.colorBg = CellInfo.Cell.colorFg = SFTBOX_NOCOLOR;
        CellInfo.Cell.colorBgSel = CellInfo.Cell.colorFgSel = SFTBOX_NOCOLOR;
        CellInfo.Cell.colorBgEnd = CellInfo.Cell.colorBgSelEnd = SFTBOX_NOCOLOR;
        CellInfo.Cell.colorProgress = CellInfo.Cell.colorProgressEnd = SFTBOX_NOCOLOR;
        Sft_SetPictureColorSample(&CellInfo.Cell.CellPicture1, SAMPLE_SIZE, SAMPLE_SIZE, RGB(0, 0, 0), RGB(0, 0, 0));
        box.SetCellInfo(&CellInfo);
    }

    // The color of each item is saved as its item data.
    index = box.AddString(_T("Custom..."));
    box.SetText(index, 1, _T("Any color"));
    box.SetItemData(index, custom);
    SetColorSample(box, index, custom);

    for (int i = 0 ; i < COLOR_COUNT ; ++i) {
        index = box.AddString(m_aColors[i].lpszName);
        box.SetText(index, 1, m_aColors[i].lpszDesc);
        box.SetItemData(index, m_aColors[i].color);
        SetColorSample(box, index, m_aColors[i].color);
    }

    box.MakeColumnOptimal(-1, 0);       // Make all columns optimal
    box.RecalcHorizontalExtent(0);

    // Select the item with the requested color, searching after the custom color
    index = box.FindItem(CUSTOM_INDEX + 1, selected);
    box.SetCurSel(index >= 0 ? index : CUSTOM_INDEX);
}

COLORREF CMainDlg::GetSelectedColor(const CSftBox& box) const
{
    LONG index = box.GetCurSel();
    if (index < 0)
        return RGB(0, 0, 0);
    return ResolveColor((COLORREF) box.GetItemData(index));
}

void CMainDlg::ChooseCustomColor(CSftBox& box)
{
    CColorDialog dlg((COLORREF) box.GetItemData(CUSTOM_INDEX), CC_FULLOPEN, this);
    dlg.m_cc.lpCustColors = m_aCustomColors;
    if (dlg.DoModal() == IDOK) {
        box.SetItemData(CUSTOM_INDEX, dlg.GetColor());
        SetColorSample(box, CUSTOM_INDEX, dlg.GetColor());
    }
}

void CMainDlg::UpdatePreview()
{
    m_PreviewBrush.DeleteObject();
    m_PreviewBrush.CreateSolidBrush(GetSelectedColor(m_Background));
    GetDlgItem(IDC_PREVIEW)->Invalidate();
}

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

    SetIcon(m_hIcon, TRUE);   // Set big icon
    SetIcon(m_hIcon, FALSE);  // Set small icon

    InitColorList(m_Background, RGB(255, 250, 205), RGB(0, 0, 128));
    InitColorList(m_Text, RGB(70, 130, 180), RGB(255, 255, 0));
    UpdatePreview();

    // Dark mode
    SftDarkMode_ApplyToDialog(m_hWnd);

    return TRUE;
}

LRESULT CMainDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    LRESULT result;

    // the preview uses the selected colors
    if (message == WM_CTLCOLORSTATIC && (HWND) lParam == ::GetDlgItem(m_hWnd, IDC_PREVIEW) && m_Text.m_hWnd) {
        ::SetTextColor((HDC) wParam, GetSelectedColor(m_Text));
        ::SetBkMode((HDC) wParam, TRANSPARENT);
        return (LRESULT) m_PreviewBrush.m_hObject;
    }

    if (SftDarkMode_HandleWindowMessage(m_hWnd, message, wParam, lParam, &result))
        return result;
    return CDialog::WindowProc(message, wParam, lParam);
}

void CMainDlg::OnBackgroundSelEndOK()   // an item was picked in the dropdown
{
    if (m_Background.GetCurSel() == CUSTOM_INDEX)
        ChooseCustomColor(m_Background);
    UpdatePreview();
}

void CMainDlg::OnTextSelEndOK()         // an item was picked in the dropdown
{
    if (m_Text.GetCurSel() == CUSTOM_INDEX)
        ChooseCustomColor(m_Text);
    UpdatePreview();
}

void CMainDlg::OnSelChange()            // the selection changed (keyboard, mouse)
{
    UpdatePreview();
}

void CMainDlg::OnSysColorChange()
{
    CDialog::OnSysColorChange();
    UpdatePreview();                    // system colors may be selected
}

void CMainDlg::OnOK()
{
    // Enter must not close the sample
}

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