HeaderPane
Main
Hide
Share Link
Print

Colors Sample (C)

This sample illustrates two dropdown list combo boxes used to select a color, each item showing a color sample as cell picture. It covers registering with SftBox/DLL, placing the combo boxes from a dialog resource, 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 (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\C\Colors\Colors.c.

/****************************************************************************/
/* 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 <windows.h>
#include <commdlg.h>

#include "SftBox.h"                     /* SftBox/DLL Header File */

#define SFTDARKMODE_IMPLEMENTATION
#include "SftDarkMode.h"                /* Dark mode support */

#include "resource.h"                   // application resources

HINSTANCE g_hInst;                      // App Instance Handle

/**********************************************************************/
/*                              Colors                                */
/**********************************************************************/

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

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

static const COLORENTRY m_aColors[] = {
    { TEXT("Black"),      TEXT(""), RGB(0, 0, 0) },
    { TEXT("White"),      TEXT(""), RGB(255, 255, 255) },
    { TEXT("Red"),        TEXT(""), RGB(255, 0, 0) },
    { TEXT("Green"),      TEXT(""), RGB(0, 160, 0) },
    { TEXT("Blue"),       TEXT(""), RGB(0, 0, 255) },
    { TEXT("Yellow"),     TEXT(""), RGB(255, 255, 0) },
    { TEXT("Orange"),     TEXT(""), RGB(255, 140, 0) },
    { TEXT("Purple"),     TEXT(""), RGB(128, 0, 128) },
    { TEXT("Teal"),       TEXT(""), RGB(0, 128, 128) },
    { TEXT("Navy"),       TEXT(""), RGB(0, 0, 128) },
    { TEXT("Maroon"),     TEXT(""), RGB(128, 0, 0) },
    { TEXT("Gray"),       TEXT(""), RGB(128, 128, 128) },
    { TEXT("Silver"),     TEXT(""), RGB(192, 192, 192) },
    { TEXT("Window"),     TEXT("Window background"), SYSCOLOR(COLOR_WINDOW) },
    { TEXT("WindowText"), TEXT("Text in windows"), SYSCOLOR(COLOR_WINDOWTEXT) },
    { TEXT("Highlight"),  TEXT("Selected items"), SYSCOLOR(COLOR_HIGHLIGHT) },
    { TEXT("HighlightText"), TEXT("Text of selected items"), SYSCOLOR(COLOR_HIGHLIGHTTEXT) },
    { TEXT("BtnFace"),    TEXT("Face of push buttons"), SYSCOLOR(COLOR_BTNFACE) },
    { TEXT("InfoBk"),     TEXT("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 m_aCustomColors[16];    // custom colors of the color dialog
static HBRUSH m_hbrPreview;             // preview background

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

/**********************************************************************/
/*                         Combo Box Setup                            */
/**********************************************************************/

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;
}

static void SetColorSample(HWND hwndBox, 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;
    SftBox_GetCellInfo(hwndBox, &CellInfo);
    Sft_SetPictureColorSample(&CellInfo.Cell.CellPicture1, SAMPLE_SIZE, SAMPLE_SIZE, color, SYSCOLOR(COLOR_WINDOWTEXT));
    SftBox_SetCellInfo(hwndBox, &CellInfo);
}

static void InitColorList(HWND hwndBox, COLORREF custom, COLORREF selected)
{
    SFTBOX_COLUMN aCols[2];
    LONG index;
    int i;

    InitColumn(&aCols[0], 0, 100, TEXT("Color"));
    InitColumn(&aCols[1], 1, 100, TEXT("Description"));
    SftBox_SetColumns(hwndBox, 2, aCols);
    SftBox_SetShowHeaderDropDown(hwndBox, TRUE);// Column titles in the dropdown only
    SftBox_SetDroppedHeight(hwndBox, 12);       // Show up to 12 items
    SftBox_SetDropDownOptimalWidth(hwndBox, TRUE);// The dropdown is as wide as its columns
    SftBox_SetDisableNoScroll(hwndBox, TRUE);   // Scroll bars always shown, needed for the sizing box
    SftBox_SetShowResizer(hwndBox, TRUE);       // The user can resize the dropdown
    SftBox_SetDarkMode(hwndBox, 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));
        SftBox_SetCellInfo(hwndBox, &CellInfo);
    }

    // The color of each item is saved as its item data.
    index = SftBox_AddString(hwndBox, TEXT("Custom..."));
    SftBox_SetText(hwndBox, index, 1, TEXT("Any color"));
    SftBox_SetItemData(hwndBox, index, custom);
    SetColorSample(hwndBox, index, custom);

    for (i = 0 ; i < COLOR_COUNT ; ++i) {
        index = SftBox_AddString(hwndBox, m_aColors[i].lpszName);
        SftBox_SetText(hwndBox, index, 1, m_aColors[i].lpszDesc);
        SftBox_SetItemData(hwndBox, index, m_aColors[i].color);
        SetColorSample(hwndBox, index, m_aColors[i].color);
    }

    SftBox_MakeColumnOptimal(hwndBox, -1, 0);   // Make all columns optimal
    SftBox_RecalcHorizontalExtent(hwndBox, 0);

    // Select the item with the requested color, searching after the custom color
    index = SftBox_FindItem(hwndBox, CUSTOM_INDEX + 1, selected);
    SftBox_SetCurSel(hwndBox, index >= 0 ? index : CUSTOM_INDEX);
}

static COLORREF GetSelectedColor(HWND hwndBox)
{
    LONG index = SftBox_GetCurSel(hwndBox);
    if (index < 0)
        return RGB(0, 0, 0);
    return ResolveColor((COLORREF) SftBox_GetItemData(hwndBox, index));
}

static void ChooseCustomColor(HWND hwndDlg, HWND hwndBox)
{
    CHOOSECOLOR cc;

    memset(&cc, 0, sizeof(cc));
    cc.lStructSize = sizeof(cc);
    cc.hwndOwner = hwndDlg;
    cc.rgbResult = (COLORREF) SftBox_GetItemData(hwndBox, CUSTOM_INDEX);
    cc.lpCustColors = m_aCustomColors;
    cc.Flags = CC_RGBINIT | CC_FULLOPEN;
    if (ChooseColor(&cc)) {
        SftBox_SetItemData(hwndBox, CUSTOM_INDEX, cc.rgbResult);
        SetColorSample(hwndBox, CUSTOM_INDEX, cc.rgbResult);
    }
}

/**********************************************************************/
/*                              Preview                               */
/**********************************************************************/

static void UpdatePreview(HWND hwndDlg)
{
    if (m_hbrPreview)
        DeleteObject(m_hbrPreview);
    m_hbrPreview = CreateSolidBrush(GetSelectedColor(GetDlgItem(hwndDlg, IDC_BACKGROUND)));
    InvalidateRect(GetDlgItem(hwndDlg, IDC_PREVIEW), NULL, TRUE);
}

/**********************************************************************/
/*                           Dialog Proc                              */
/**********************************************************************/

INT_PTR CALLBACK MainDialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg) {
    case WM_INITDIALOG: {
        int i;
        SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_APP)));
        for (i = 0 ; i < 16 ; ++i)
            m_aCustomColors[i] = RGB(255, 255, 255);
        InitColorList(GetDlgItem(hwndDlg, IDC_BACKGROUND), RGB(255, 250, 205), RGB(0, 0, 128));
        InitColorList(GetDlgItem(hwndDlg, IDC_TEXT), RGB(70, 130, 180), RGB(255, 255, 0));
        UpdatePreview(hwndDlg);
        SftDarkMode_ApplyToDialog(hwndDlg);
        return TRUE;
     }

    case WM_DESTROY:
        if (m_hbrPreview)
            DeleteObject(m_hbrPreview);
        m_hbrPreview = NULL;
        break;

    case WM_CTLCOLORSTATIC:
        if ((HWND) lParam == GetDlgItem(hwndDlg, IDC_PREVIEW)) {
            SetTextColor((HDC) wParam, GetSelectedColor(GetDlgItem(hwndDlg, IDC_TEXT)));
            SetBkMode((HDC) wParam, TRANSPARENT);
            return (INT_PTR) m_hbrPreview;
        }
        break;

    case WM_SYSCOLORCHANGE:
        UpdatePreview(hwndDlg);         // system colors may be selected
        break;

    case WM_COMMAND:
        switch (LOWORD(wParam)) {
        case IDC_BACKGROUND:
        case IDC_TEXT:
            switch (HIWORD(wParam)) {
            case SFTBOXN_SELENDOK:      // an item was picked in the dropdown
                if (SftBox_GetCurSel((HWND) lParam) == CUSTOM_INDEX)
                    ChooseCustomColor(hwndDlg, (HWND) lParam);
                UpdatePreview(hwndDlg);
                break;
            case SFTBOXN_SELCHANGE:     // the selection changed (keyboard, mouse)
                UpdatePreview(hwndDlg);
                break;
            }
            break;
        case IDCANCEL:
            EndDialog(hwndDlg, 0);
            return TRUE;
        }
        break;
    }

    {
        INT_PTR dmResult;
        if (SftDarkMode_HandleDialogMessage(hwndDlg, msg, wParam, lParam, &dmResult))
            return dmResult;
    }

    return FALSE;
}

/**********************************************************************/
/*                              WinMain                               */
/**********************************************************************/

int PASCAL WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int cmdShow)
{
    g_hInst = hinst;

    SftDarkMode_Init();                 /* Dark mode support */
    SftBox_RegisterApp(hinst);          /* Register with SftBox/DLL */

    DialogBox(g_hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDialogProc);

    SftBox_UnregisterApp(hinst);        /* Unregister from SftBox/DLL */

    return 0;
}

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