This sample illustrates font selection using three combo boxes for font names, font styles and font sizes, hosted in an MFC dialog through CSftBox. The contents are added by SftBox/DLL using the Fonts, FontStyles and FontSizes functions, for the display or a printer. The font names combo box has two columns and shows each font name using the font itself (cell fonts, released in a delete callback set using SetDeleteCallback). The column headers shown above the static portion of the font names and above the edit control portion of the font sizes are used to sort (SFTBOXN_MAIN_NOTIFICATION through ON_CONTROL, SortItemsEx) and show sort indicators set by the application.
The source code is located at C:\Program Files (x86)\Softelvdm\SftBox DLL 6.0\Samples\MFC\Fonts.
/****************************************************************************/
/* 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 <commdlg.h>
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Sorting
static int CALLBACK SortStringsAscending(HWND hwnd, LPCTSTR lpszString1, LPCTSTR lpszString2,
SFTBOX_DWORD_PTR item1, SFTBOX_DWORD_PTR item2, SFTBOX_DWORD_PTR cell1, SFTBOX_DWORD_PTR cell2)
{
return lstrcmpi(lpszString1, lpszString2);
}
static int CALLBACK SortStringsDescending(HWND hwnd, LPCTSTR lpszString1, LPCTSTR lpszString2,
SFTBOX_DWORD_PTR item1, SFTBOX_DWORD_PTR item2, SFTBOX_DWORD_PTR cell1, SFTBOX_DWORD_PTR cell2)
{
return lstrcmpi(lpszString2, lpszString1);
}
static int CALLBACK SortCellDataAscending(HWND hwnd, LPCTSTR lpszString1, LPCTSTR lpszString2,
SFTBOX_DWORD_PTR item1, SFTBOX_DWORD_PTR item2, SFTBOX_DWORD_PTR cell1, SFTBOX_DWORD_PTR cell2)
{
// The cell data of a font size is its numeric value
if (cell1 > cell2)
return 1;
else if (cell1 < cell2)
return -1;
return 0;
}
static int CALLBACK SortCellDataDescending(HWND hwnd, LPCTSTR lpszString1, LPCTSTR lpszString2,
SFTBOX_DWORD_PTR item1, SFTBOX_DWORD_PTR item2, SFTBOX_DWORD_PTR cell1, SFTBOX_DWORD_PTR cell2)
{
return SortCellDataAscending(hwnd, lpszString2, lpszString1, item2, item1, cell2, cell1);
}
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->flag = SFTBOX_BMP_RIGHT;
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_WM_PAINT()
ON_CONTROL(SFTBOXN_SELCHANGE, IDC_FONTS, OnFontsSelChange)
ON_CONTROL(SFTBOXN_MAIN_NOTIFICATION(SFTBOXN_LBUTTONDOWN_COLUMN), IDC_FONTS, OnFontsColumn)
ON_CONTROL(SFTBOXN_SELENDOK, IDC_FONTS, OnFontsSelEnd)
ON_CONTROL(SFTBOXN_SELENDCANCEL, IDC_FONTS, OnFontsSelEnd)
ON_CONTROL(SFTBOXN_SELCHANGE, IDC_FONTSTYLE, OnStyleSelChange)
ON_CONTROL(SFTBOXN_SELCHANGE, IDC_FONTSIZE, OnSizesSelChange)
ON_CONTROL(SFTBOXN_EDITCHANGE, IDC_FONTSIZE, OnSizesEditChange)
ON_CONTROL(SFTBOXN_MAIN_NOTIFICATION(SFTBOXN_LBUTTONDOWN_COLUMN), IDC_FONTSIZE, OnSizesColumn)
ON_BN_CLICKED(IDC_PRINTER, OnPrinter)
END_MESSAGE_MAP()
CMainDlg::CMainDlg(CWnd* pParent) : CDialog(CMainDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hDC = NULL;
m_hDevMode = m_hDevNames = NULL;
m_SizeSelected = 10;
m_fFontsDescending = m_fSizesDescending = FALSE;
}
CMainDlg::~CMainDlg()
{
ClearHDC();
}
void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_FONTS, m_Fonts);
DDX_Control(pDX, IDC_FONTSTYLE, m_Style);
DDX_Control(pDX, IDC_FONTSIZE, m_Sizes);
}
void CMainDlg::ClearHDC()
{
if (m_hDC)
DeleteDC(m_hDC);
if (m_hDevMode != NULL)
GlobalFree(m_hDevMode);
if (m_hDevNames != NULL)
GlobalFree(m_hDevNames);
m_hDC = NULL;
m_hDevMode = m_hDevNames = NULL;
}
void CALLBACK CMainDlg::FontsDeleteCallback(HWND hwnd, LONG index, SFTBOX_DWORD_PTR itemData, SFTBOX_DWORD_PTR UserData)
{
// As each item in the fonts combo box is deleted, we get a chance to
// do some cleanup processing. In this sample we delete the font
// handle that was created in AddFonts
CMainDlg* pThis = (CMainDlg*) UserData;
SFTBOX_CELLINFOPARM CellParm;
CellParm.index = index;
CellParm.iCol = 1;
pThis->m_Fonts.GetCellInfo(&CellParm);
DeleteObject(CellParm.Cell.hFont);
}
/////////////////////////////////////////////////////////////////////////////
// Sorting
void CMainDlg::SetSortIndicator(CSftBox& box, BOOL fDescending)
{
// The first column title shows the sort direction
LPSFTBOX_COLUMN lpCol;
int nCols = box.GetColumns(&lpCol);
if (nCols <= 0)
return;
lpCol[0].colFlag &= ~(SFTBOX_COL_SORTED_ASC | SFTBOX_COL_SORTED_DESC);
lpCol[0].colFlag |= fDescending ? SFTBOX_COL_SORTED_DESC : SFTBOX_COL_SORTED_ASC;
box.SetColumns(nCols, lpCol);
}
void CMainDlg::SortFonts()
{
m_Fonts.SortItemsEx(-1, 0, m_fFontsDescending ? SortStringsDescending : SortStringsAscending);
}
void CMainDlg::SortSizes()
{
m_Sizes.SortItemsEx(-1, 0, m_fSizesDescending ? SortCellDataDescending : SortCellDataAscending);
}
/////////////////////////////////////////////////////////////////////////////
// Data
void CMainDlg::AddFonts()
{
// clear (in case of refresh)
m_Fonts.ResetContent();
// Add the font names, with a picture for the font type. Without a
// printer, the fonts of the display are added.
m_Fonts.Fonts(-1, TRUE, m_hDC, SFTBOX_FONT_ALL);
// Get the default font height
TEXTMETRIC tm;
{
CClientDC dc(&m_Fonts);
dc.GetTextMetrics(&tm);
}
// The second column shows each font name using the font itself
LONG total = m_Fonts.GetCount();
for (LONG index = 0 ; index < total ; ++index) {
SFTBOX_CELLINFOPARM CellParm;
TCHAR szFont[LF_FACESIZE];
if (m_Fonts.GetTextLength(index, 0) >= LF_FACESIZE)
continue;
m_Fonts.GetText(index, 0, szFont);
m_Fonts.SetText(index, 1, szFont);
// the font handle is saved as cell font and deleted in FontsDeleteCallback
CellParm.index = index;
CellParm.iCol = 1;
m_Fonts.GetCellInfo(&CellParm);
CellParm.Cell.hFont = CreateFont(tm.tmHeight-1, 0, 0, 0, FW_NORMAL, 0, 0, 0,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DRAFT_QUALITY, 0x4|DEFAULT_PITCH, szFont);
m_Fonts.SetCellInfo(&CellParm);
}
SortFonts();
// Make column widths optimal
m_Fonts.MakeColumnOptimal(-1, 0);
// recalc best width for horizontal scrolling
m_Fonts.RecalcHorizontalExtent(-1);
// Select a font
if (m_Fonts.SelectStringExact(-1, 0, _T("Segoe UI"), FALSE) < 0) {
if (m_Fonts.SelectStringExact(-1, 0, _T("Arial"), FALSE) < 0)
m_Fonts.SetCurSel(0);
}
}
void CMainDlg::AddStyles(LPCTSTR lpszFont)
{
// Add font styles, based on selected font
// get the current style
CString style;
m_Style.GetWindowText(style);
// clear (in case of refresh)
m_Style.ResetContent();
// Add font styles
m_Style.FontStyles(m_hDC, lpszFont, NULL, NULL);
// Select the previous style if possible
if (m_Style.SelectStringExact(-1, 0, style, FALSE) < 0)
m_Style.SetCurSel(0);
m_Style.RecalcHorizontalExtent(-1);
}
void CMainDlg::AddSizes(LPCTSTR lpszFont)
{
LONG saved = 0;
int diff = 9999;
// Add font sizes to combo box, based on selected font
// clear (in case of refresh)
m_Sizes.ResetContent();
// Add font sizes
m_Sizes.FontSizes(m_hDC, lpszFont);
m_Sizes.RecalcHorizontalExtent(-1);
SortSizes();
// try to find the closest match to the last size selected,
// the cell data of each item is the font size
LONG total = m_Sizes.GetCount();
for (LONG index = 0 ; index < total ; ++index) {
SFTBOX_CELLINFOPARM CellParm;
CellParm.index = index;
CellParm.iCol = 0;
m_Sizes.GetCellInfo(&CellParm);
if (diff > abs(m_SizeSelected - (int) CellParm.Cell.cellData)) {
diff = abs(m_SizeSelected - (int) CellParm.Cell.cellData);
saved = index;
}
}
int origSize = m_SizeSelected;
m_Sizes.SetCurSel(saved);
m_SizeSelected = origSize;
}
/////////////////////////////////////////////////////////////////////////////
// Font sample
void CMainDlg::PaintSample()
{
CClientDC dc(this);
CRect rect;
BOOL fDark = SftDarkMode_IsActive();
// determine where to paint the sample
CWnd* pSample = GetDlgItem(IDC_SAMPLE);
pSample->GetClientRect(&rect);
pSample->MapWindowPoints(this, &rect);
dc.FillSolidRect(&rect, fDark ? RGB(32, 32, 32) : GetSysColor(COLOR_WINDOW));
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(fDark ? RGB(230, 230, 230) : GetSysColor(COLOR_WINDOWTEXT));
// get the font name
CString font;
m_Fonts.GetWindowText(font);
// get the font weight, the cell data of each style is its weight and italic flag
LONG index = m_Style.GetCurSel();
if (index >= 0) {
SFTBOX_CELLINFOPARM CellParm;
CellParm.index = index;
CellParm.iCol = 0;
m_Style.GetCellInfo(&CellParm);
int weight = (int) (CellParm.Cell.cellData & 0xffff);
BOOL fItalic = (CellParm.Cell.cellData & 0x80000000L) != 0;
// get the font size
CString text;
m_Sizes.GetWindowText(text);
int size = _ttoi(text);
if (size <= 0)
size = 10;
// convert the point size to the actual size
size = MulDiv(size, dc.GetDeviceCaps(LOGPIXELSY), 72);
// Create the font and draw the sample
CFont sampleFont;
sampleFont.CreateFont(-size, 0, 0, 0, weight, (BYTE) fItalic, 0, 0,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DRAFT_QUALITY, 0x4|DEFAULT_PITCH, font);
CFont* pOldFont = dc.SelectObject(&sampleFont);
dc.DrawText(_T("AaBbCcYyZz"), 10, &rect, DT_CENTER|DT_VCENTER|DT_NOPREFIX|DT_SINGLELINE);
dc.SelectObject(pOldFont);
}
}
/////////////////////////////////////////////////////////////////////////////
// Combo box setup
void CMainDlg::InitControls()
{
SFTBOX_COLUMN aCols[2];
SFTBOX_DELETEPARM Parm;
// FONT NAMES combo box
m_Fonts.SetSelectionStyle(SFTBOX_SELECTION_CELL1);
m_Fonts.SetDroppedHeight(8);
m_Fonts.SetShowHeader(SFTBOX_COLSTYLE_BUTTONTEXT);
m_Fonts.EnableSortIndicators(TRUE, FALSE);// the application sets the sort indicators
m_Fonts.SetDarkMode(SFTBOX_DARKMODE_AUTO);
InitColumn(&aCols[0], 0, 174, _T("Font Name"));
InitColumn(&aCols[1], 1, 400, _T("Sample"));
m_Fonts.SetColumns(2, aCols);
SetSortIndicator(m_Fonts, m_fFontsDescending);
// Use a delete callback routine
Parm.lpfnDelete = FontsDeleteCallback;
Parm.UserData = (SFTBOX_DWORD_PTR) this;
m_Fonts.SetDeleteCallback(&Parm);
// FONT STYLE combo box
m_Style.SetSelectionStyle(SFTBOX_SELECTION_CELL1);
m_Style.SetDroppedHeight(4);
m_Style.SetResizeHeader(FALSE);
m_Style.SetReorderColumns(FALSE);
m_Style.SetShowHeader(SFTBOX_COLSTYLE_BUTTONTEXT);
m_Style.EnableSortIndicators(TRUE, FALSE);// not sorted, but the column title is as tall as the others
m_Style.SetDarkMode(SFTBOX_DARKMODE_AUTO);
InitColumn(&aCols[0], 0, 400, _T("Style"));
m_Style.SetColumns(1, aCols);
// FONT SIZES combo box
m_Sizes.SetSelectionStyle(SFTBOX_SELECTION_CELL1);
m_Sizes.SetDroppedHeight(8);
m_Sizes.SetResizeHeader(FALSE);
m_Sizes.SetReorderColumns(FALSE);
m_Sizes.SetShowHeader(SFTBOX_COLSTYLE_BUTTONTEXT);
m_Sizes.EnableSortIndicators(TRUE, FALSE);
m_Sizes.SetDarkMode(SFTBOX_DARKMODE_AUTO);
InitColumn(&aCols[0], 0, 400, _T("Size"));
m_Sizes.SetColumns(1, aCols);
SetSortIndicator(m_Sizes, m_fSizesDescending);
m_Sizes.LimitText(3);
}
void CMainDlg::EqualizeHeights()
{
// The height of a combo box follows the height of its items. The font
// names have a picture and the sizes an edit portion, so use the
// largest item height as minimum height in all three.
int height = m_Fonts.GetItemHeight(-1);
height = max(height, m_Style.GetItemHeight(-1));
height = max(height, m_Sizes.GetItemHeight(-1));
m_Fonts.SetItemHeightMinMax(-1, height, 0);
m_Style.SetItemHeightMinMax(-1, height, 0);
m_Sizes.SetItemHeightMinMax(-1, height, 0);
}
BOOL CMainDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
InitControls();
AddFonts(); // selects a font, which adds its styles and sizes
EqualizeHeights();
// 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);
}
/////////////////////////////////////////////////////////////////////////////
// Messages
void CMainDlg::OnPaint()
{
CDialog::OnPaint();
PaintSample();
}
void CMainDlg::OnFontsSelChange()
{
TCHAR szFont[LF_FACESIZE];
LONG index = m_Fonts.GetCurSel();
if (index >= 0 && m_Fonts.GetTextLength(index, 0) < LF_FACESIZE) {
m_Fonts.GetText(index, 0, szFont);
AddSizes(szFont);
AddStyles(szFont);
}
PaintSample();
}
void CMainDlg::OnFontsColumn() // a column title was clicked
{
if (m_Fonts.GetHeaderButton() == 0) {
m_fFontsDescending = !m_fFontsDescending;
SortFonts();
SetSortIndicator(m_Fonts, m_fFontsDescending);
}
}
void CMainDlg::OnFontsSelEnd()
{
m_Fonts.SetHorizontalOffset(0);
}
void CMainDlg::OnStyleSelChange()
{
PaintSample();
}
void CMainDlg::OnSizesSelChange()
{
LONG index = m_Sizes.GetCurSel();
if (index >= 0) {
SFTBOX_CELLINFOPARM CellParm;
CellParm.index = index;
CellParm.iCol = 0;
m_Sizes.GetCellInfo(&CellParm);
m_SizeSelected = (int) CellParm.Cell.cellData;
}
PaintSample();
}
void CMainDlg::OnSizesEditChange() // a size was typed
{
CString text;
m_Sizes.GetWindowText(text);
m_SizeSelected = _ttoi(text);
if (m_SizeSelected <= 0)
m_SizeSelected = 10;
PaintSample();
}
void CMainDlg::OnSizesColumn() // the column title was clicked
{
m_fSizesDescending = !m_fSizesDescending;
SortSizes();
SetSortIndicator(m_Sizes, m_fSizesDescending);
}
void CMainDlg::OnPrinter() // show the fonts of a printer
{
PRINTDLG pd;
memset(&pd, 0, sizeof(PRINTDLG));
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = m_hWnd;
pd.Flags = PD_RETURNDC;
if (PrintDlg(&pd)) {
ClearHDC();
m_hDC = pd.hDC;
m_hDevMode = pd.hDevMode;
m_hDevNames = pd.hDevNames;
AddFonts();
}
}
void CMainDlg::OnOK()
{
// Enter must not close the sample
}