HeaderPane
Main
Hide
Share Link
Print

ItemImages Sample (C++/MFC)

This sample illustrates pictures in dropdown list combo boxes, hosted in an MFC dialog through CSftBox, using the small icons Windows provides. The first combo box shows a hierarchy with default item pictures for expandable, expanded and leaf items (SetPictures), individual item pictures (SetItemPicture), tree lines and expand/collapse buttons; the hierarchy is defined using SetItemLevel, Collapse and Expand, and an item is expanded or collapsed when the user clicks its expand/collapse button (SFTBOXN_LBUTTONDOWN_BUTTON and SFTBOXN_LBUTTONDBLCLK_BUTTON handled through ON_CONTROL, GetExpandCollapseIndex, SetItemExpand). The second combo box has two columns, a picture for each item and a picture on the dropdown button (SetDropDownButtonPicture).

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

/****************************************************************************/
/* 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"

#include <shellapi.h>

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

/////////////////////////////////////////////////////////////////////////////
// Data

struct FOLDERITEM {
    int level;                          // 0 is the top level
    LPCTSTR lpszText;
    int icon;                           // -1 for the default item picture
};

struct DEVICE {
    LPCTSTR lpszText;
    LPCTSTR lpszType;
    int icon;
};

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;
    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_LBUTTONDOWN_BUTTON, IDC_FOLDERS, OnFoldersExpandCollapse)
    ON_CONTROL(SFTBOXN_LBUTTONDBLCLK_BUTTON, IDC_FOLDERS, OnFoldersExpandCollapse)
END_MESSAGE_MAP()

CMainDlg::CMainDlg(CWnd* pParent) : CDialog(CMainDlg::IDD, pParent)
{
    // This sample uses the small icons Windows provides. Bitmaps, image lists,
    // GDI+ images and color samples can be used in the same way, see the
    // Sft_SetPictureXxx functions in SftPicture2.h.
    static const SHSTOCKICONID aIconIds[ICON_COUNT] = {
        SIID_FOLDER, SIID_FOLDEROPEN, SIID_DOCNOASSOC, SIID_IMAGEFILES, SIID_AUDIOFILES, SIID_VIDEOFILES,
        SIID_DRIVEFIXED, SIID_DRIVECD, SIID_DRIVENET, SIID_PRINTER, SIID_FIND,
    };

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

    for (int i = 0 ; i < ICON_COUNT ; ++i) {
        SHSTOCKICONINFO sii;
        memset(&sii, 0, sizeof(sii));
        sii.cbSize = sizeof(sii);
        m_ahIcons[i] = NULL;
        if (SUCCEEDED(SHGetStockIconInfo(aIconIds[i], SHGSI_ICON | SHGSI_SMALLICON, &sii)))
            m_ahIcons[i] = sii.hIcon;
    }
}

CMainDlg::~CMainDlg()
{
    // Pictures are owned by the application and can be deleted
    // once the controls using them have been destroyed.
    for (int i = 0 ; i < ICON_COUNT ; ++i) {
        if (m_ahIcons[i])
            DestroyIcon(m_ahIcons[i]);
    }
}

void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_FOLDERS, m_Folders);
    DDX_Control(pDX, IDC_DEVICES, m_Devices);
}

void CMainDlg::InitFolders()
{
    static const FOLDERITEM aFolders[] = {
        { 0, _T("Documents"), -1 },
        { 1, _T("Letters"), -1 },
        { 2, _T("Welcome"), -1 },
        { 2, _T("Reminder"), -1 },
        { 1, _T("Reports"), -1 },
        { 2, _T("Budget"), -1 },
        { 2, _T("Forecast"), -1 },
        { 0, _T("Pictures"), -1 },
        { 1, _T("Beach"), ICON_IMAGE },
        { 1, _T("Mountains"), ICON_IMAGE },
        { 0, _T("Music"), -1 },
        { 1, _T("Favorites"), -1 },
        { 2, _T("Morning"), ICON_AUDIO },
        { 2, _T("Evening"), ICON_AUDIO },
        { 0, _T("Videos"), -1 },
        { 1, _T("Birthday"), ICON_VIDEO },
    };
    const int count = sizeof(aFolders) / sizeof(aFolders[0]);
    SFT_PICTURE aThreeItemPictures[3];
    int i;

    // The default item pictures: an item with dependents, the same item
    // when expanded and an item without dependents. All item pictures
    // of a control are the same size.
    Sft_InitPicture(&aThreeItemPictures[0]);
    Sft_InitPicture(&aThreeItemPictures[1]);
    Sft_InitPicture(&aThreeItemPictures[2]);
    Sft_SetPictureIcon(&aThreeItemPictures[0], m_ahIcons[ICON_FOLDER]);    /* Expandable picture */
    Sft_SetPictureIcon(&aThreeItemPictures[1], m_ahIcons[ICON_FOLDEROPEN]);/* Expanded picture */
    Sft_SetPictureIcon(&aThreeItemPictures[2], m_ahIcons[ICON_DOCUMENT]);  /* Leaf picture */
    m_Folders.SetPictures(aThreeItemPictures);

    m_Folders.SetTreeLineStyle(SFTBOX_TREELINE_AUTOMATIC);// Dotted or invisible tree lines, following the Windows theme
    m_Folders.SetShowButtons(TRUE);     // Expand/collapse buttons (level 1..n)
    m_Folders.SetShowButton0(TRUE);     // Expand/collapse buttons (level 0)
    m_Folders.SetButtons(SFTBOX_BUTTON_AUTOMATIC);// Button style follows the Windows theme
    m_Folders.SetDroppedHeight(12);     // Show up to 12 items
    m_Folders.SetDarkMode(SFTBOX_DARKMODE_AUTO);

    // The level of an item defines the hierarchy: an item is a
    // dependent of the closest preceding item with a lower level.
    for (i = 0 ; i < count ; ++i) {
        LONG index = m_Folders.AddString(aFolders[i].lpszText);
        m_Folders.SetItemLevel(index, aFolders[i].level);
        if (aFolders[i].icon >= 0) {
            SFT_PICTURE Picture;
            Sft_InitPicture(&Picture);
            Sft_SetPictureIcon(&Picture, m_ahIcons[aFolders[i].icon]);
            m_Folders.SetItemPicture(index, &Picture);// Override the default item picture
        }
    }

    // Collapse the top level items
    for (i = 0 ; i < count ; ++i) {
        if (aFolders[i].level == 0)
            m_Folders.Collapse(i, TRUE);
    }
    m_Folders.Expand(0, TRUE, FALSE);   // Show the dependents of the first item

    m_Folders.RecalcHorizontalExtent(0);
    m_Folders.SetCurSel(0);
}

void CMainDlg::InitDevices()
{
    static const DEVICE aDevices[] = {
        { _T("System"), _T("Local disk"), ICON_DRIVE },
        { _T("Data"), _T("Local disk"), ICON_DRIVE },
        { _T("Backup"), _T("CD drive"), ICON_CDROM },
        { _T("Projects"), _T("Network drive"), ICON_NETWORK },
        { _T("Archive"), _T("Network drive"), ICON_NETWORK },
        { _T("Office"), _T("Printer"), ICON_PRINTER },
    };
    const int count = sizeof(aDevices) / sizeof(aDevices[0]);
    SFTBOX_COLUMN aCols[2];
    SFT_PICTURE Picture;

    InitColumn(&aCols[0], 0, 90, _T("Name"));
    InitColumn(&aCols[1], 1, 100, _T("Type"));
    m_Devices.SetColumns(2, aCols);
    m_Devices.SetShowHeaderDropDown(TRUE);// Column titles in the dropdown only
    m_Devices.SetDropDownOptimalWidth(TRUE);// The dropdown is as wide as its columns
    m_Devices.SetDarkMode(SFTBOX_DARKMODE_AUTO);

    // A picture instead of the arrow on the dropdown button
    Sft_InitPicture(&Picture);
    Sft_SetPictureIcon(&Picture, m_ahIcons[ICON_FIND]);
    m_Devices.SetDropDownButtonPicture(&Picture);

    // Register the item picture size. Only one picture needs to be
    // registered, even if several are used.
    Sft_InitPicture(&Picture);
    Sft_SetPictureIcon(&Picture, m_ahIcons[ICON_DRIVE]);
    m_Devices.SetItemPicture(-1, &Picture);

    for (int i = 0 ; i < count ; ++i) {
        LONG index = m_Devices.AddString(aDevices[i].lpszText);
        m_Devices.SetText(index, 1, aDevices[i].lpszType);
        Sft_InitPicture(&Picture);
        Sft_SetPictureIcon(&Picture, m_ahIcons[aDevices[i].icon]);
        m_Devices.SetItemPicture(index, &Picture);
    }

    m_Devices.RecalcHorizontalExtent(0);
    m_Devices.SetCurSel(0);
}

// The combo box reports a click on an expand/collapse button, the
// application expands or collapses the item.

void CMainDlg::OnFoldersExpandCollapse()
{
    int index = m_Folders.GetExpandCollapseIndex();
    if (index >= 0)
        m_Folders.SetItemExpand(index, !m_Folders.GetItemExpand(index), FALSE);
}

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

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

    InitFolders();
    InitDevices();

    // Dark mode
    SftDarkMode_ApplyToDialog(m_hWnd);

    return TRUE;
}

LRESULT CMainDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    LRESULT result;
    if (SftDarkMode_HandleWindowMessage(m_hWnd, message, wParam, lParam, &result))
        return result;
    return CDialog::WindowProc(message, wParam, lParam);
}

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

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