This sample illustrates font selection using three combo boxes for font names, font styles and font sizes, which 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, 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\C\Fonts\Fonts.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 <stdlib.h> #include <commdlg.h> #include <tchar.h> #include "SftBox.h" /* SftBox/DLL Header File */ #define SFTDARKMODE_IMPLEMENTATION #include "SftDarkMode.h" /* Dark mode support */ #include "resource.h" // application resources /**********************************************************************/ /* Globals */ /**********************************************************************/ HINSTANCE g_hInst; // App Instance Handle HWND m_hwndFonts; // Fonts - SftBox combo box control HWND m_hwndStyle; // Font styles - SftBox combo box control HWND m_hwndSizes; // Font sizes - SftBox combo box control HDC m_hDC; // Current printer HGLOBAL m_hDevMode; HGLOBAL m_hDevNames; int m_SizeSelected = 10; // last explicitly selected font size BOOL m_fFontsDescending; // sort direction of the font names BOOL m_fSizesDescending; // sort direction of the font sizes /**********************************************************************/ /* Printer HDC */ /**********************************************************************/ static void ClearHDC(void) { 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; } static void SetHDC(HDC hDC, HGLOBAL hDevMode, HGLOBAL hDevNames) { ClearHDC(); m_hDC = hDC; m_hDevMode = hDevMode; m_hDevNames = hDevNames; } /**********************************************************************/ /* Delete Callback */ /**********************************************************************/ void CALLBACK 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 below SFTBOX_CELLINFOPARM CellParm; // get the font handle CellParm.index = index; CellParm.iCol = 1; SftBox_GetCellInfo(m_hwndFonts, &CellParm); DeleteObject(CellParm.Cell.hFont); } /**********************************************************************/ /* Sorting */ /**********************************************************************/ 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); } 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); } 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; } 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 SetSortIndicator(HWND hwndBox, BOOL fDescending) { // The first column title shows the sort direction LPSFTBOX_COLUMN lpCol; int nCols = SftBox_GetColumns(hwndBox, &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; SftBox_SetColumns(hwndBox, nCols, lpCol); } static void SortFonts(void) { SftBox_SortItemsEx(m_hwndFonts, -1, 0, m_fFontsDescending ? SortStringsDescending : SortStringsAscending); } static void SortSizes(void) { SftBox_SortItemsEx(m_hwndSizes, -1, 0, m_fSizesDescending ? SortCellDataDescending : SortCellDataAscending); } /**********************************************************************/ /* Data Routines */ /**********************************************************************/ static void AddFonts(void) { LONG index, total; TEXTMETRIC tm; HDC hDC; // clear (in case of refresh) SftBox_ResetContent(m_hwndFonts); // Add the font names, with a picture for the font type. Without a // printer, the fonts of the display are added. SftBox_Fonts(m_hwndFonts, -1, TRUE, m_hDC, SFTBOX_FONT_ALL); // Get the default font height hDC = GetDC(m_hwndFonts); GetTextMetrics(hDC, &tm); ReleaseDC(m_hwndFonts, hDC); // The second column shows each font name using the font itself total = SftBox_GetCount(m_hwndFonts); for (index = 0 ; index < total ; ++index) { SFTBOX_CELLINFOPARM CellParm; TCHAR szFont[LF_FACESIZE]; if (SftBox_GetTextLength(m_hwndFonts, index, 0) >= LF_FACESIZE) continue; SftBox_GetText(m_hwndFonts, index, 0, szFont); SftBox_SetText(m_hwndFonts, index, 1, szFont); // the font handle is saved as cell font and deleted in FontsDeleteCallback CellParm.index = index; CellParm.iCol = 1; SftBox_GetCellInfo(m_hwndFonts, &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); SftBox_SetCellInfo(m_hwndFonts, &CellParm); } SortFonts(); // Make column widths optimal SftBox_MakeColumnOptimal(m_hwndFonts, -1, 0); // recalc best width for horizontal scrolling SftBox_RecalcHorizontalExtent(m_hwndFonts, -1); // Select a font if (SftBox_SelectStringExact(m_hwndFonts, -1, 0, TEXT("Segoe UI"), FALSE) < 0) { if (SftBox_SelectStringExact(m_hwndFonts, -1, 0, TEXT("Arial"), FALSE) < 0) SftBox_SetCurSel(m_hwndFonts, 0); } } static void AddStyles(LPCTSTR lpszFont) { TCHAR szBuffer[256]; // Add font styles, based on selected font // get the current style GetWindowText(m_hwndStyle, szBuffer, sizeof(szBuffer)/sizeof(szBuffer[0])); // clear (in case of refresh) SftBox_ResetContent(m_hwndStyle); // Add font styles SftBox_FontStyles(m_hwndStyle, m_hDC, lpszFont, NULL, NULL); // Select the previous style if possible if (SftBox_SelectStringExact(m_hwndStyle, -1, 0, szBuffer, FALSE) < 0) SftBox_SetCurSel(m_hwndStyle, 0); SftBox_RecalcHorizontalExtent(m_hwndStyle, -1); } static void AddSizes(LPCTSTR lpszFont) { LONG index; LONG saved = 0; int origSize, diff = 9999; LONG total; // Add font sizes to combo box, based on selected font // clear (in case of refresh) SftBox_ResetContent(m_hwndSizes); // Add font sizes SftBox_FontSizes(m_hwndSizes, m_hDC, lpszFont); SftBox_RecalcHorizontalExtent(m_hwndSizes, -1); SortSizes(); // try to find the closest match to the last size selected, // the cell data of each item is the font size total = SftBox_GetCount(m_hwndSizes); for (index = 0 ; index < total ; ++index) { SFTBOX_CELLINFOPARM CellParm; CellParm.index = index; CellParm.iCol = 0; SftBox_GetCellInfo(m_hwndSizes, &CellParm); if (diff > abs(m_SizeSelected - (int) CellParm.Cell.cellData)) { diff = abs(m_SizeSelected - (int) CellParm.Cell.cellData); saved = index; } } origSize = m_SizeSelected; SftBox_SetCurSel(m_hwndSizes, saved); m_SizeSelected = origSize; } /**********************************************************************/ /* Font Sample */ /**********************************************************************/ static void PaintSample(HWND hwndDlg, HWND hwndCtl) { HDC hDC; RECT rect; HBRUSH hbr; HFONT hFont; LONG index; TCHAR szFont[80]; TCHAR szSize[20]; int size; int weight; BOOL fItalic; BOOL fDark = SftDarkMode_IsActive(); hDC = GetDC(hwndDlg); // determine where to paint the sample GetClientRect(hwndCtl, &rect); MapWindowPoints(hwndCtl, hwndDlg, (LPPOINT) &rect, 2); hbr = CreateSolidBrush(fDark ? RGB(32, 32, 32) : GetSysColor(COLOR_WINDOW)); FillRect(hDC, &rect, hbr); DeleteObject(hbr); SetBkMode(hDC, TRANSPARENT); SetTextColor(hDC, fDark ? RGB(230, 230, 230) : GetSysColor(COLOR_WINDOWTEXT)); // get the font name GetWindowText(m_hwndFonts, szFont, sizeof(szFont)/sizeof(szFont[0])); // get the font weight, the cell data of each style is its weight and italic flag index = SftBox_GetCurSel(m_hwndStyle); if (index >= 0) { SFTBOX_CELLINFOPARM CellParm; CellParm.index = index; CellParm.iCol = 0; SftBox_GetCellInfo(m_hwndStyle, &CellParm); weight = (int) (CellParm.Cell.cellData & 0xffff); fItalic = (CellParm.Cell.cellData & 0x80000000L) != 0; // get the font size GetWindowText(m_hwndSizes, szSize, sizeof(szSize)/sizeof(szSize[0])); size = _ttoi(szSize); if (size <= 0) size = 10; // convert the point size to the actual size size = MulDiv(size, GetDeviceCaps(hDC, LOGPIXELSY), 72); // Create the font and draw the sample hFont = CreateFont(-size, 0, 0, 0, weight, (BYTE) fItalic, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DRAFT_QUALITY, 0x4|DEFAULT_PITCH, szFont); hFont = (HFONT)SelectObject(hDC, hFont); DrawText(hDC, TEXT("AaBbCcYyZz"), 10, &rect, DT_CENTER|DT_VCENTER|DT_NOPREFIX|DT_SINGLELINE); hFont = (HFONT)SelectObject(hDC, hFont); DeleteObject(hFont); } ReleaseDC(hwndDlg, hDC); } /**********************************************************************/ /* 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; 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; } static void InitControls(void) { SFTBOX_COLUMN aCols[2]; SFTBOX_DELETEPARM Parm; // FONT NAMES combo box SftBox_SetSelectionStyle(m_hwndFonts, SFTBOX_SELECTION_CELL1); SftBox_SetDroppedHeight(m_hwndFonts, 8); SftBox_SetShowHeader(m_hwndFonts, SFTBOX_COLSTYLE_BUTTONTEXT); SftBox_EnableSortIndicators(m_hwndFonts, TRUE, FALSE);// the application sets the sort indicators SftBox_SetDarkMode(m_hwndFonts, SFTBOX_DARKMODE_AUTO); InitColumn(&aCols[0], 0, 174, TEXT("Font Name")); InitColumn(&aCols[1], 1, 400, TEXT("Sample")); SftBox_SetColumns(m_hwndFonts, 2, aCols); SetSortIndicator(m_hwndFonts, m_fFontsDescending); // Use a delete callback routine Parm.lpfnDelete = (SFTBOX_DELETEPROC) FontsDeleteCallback; Parm.UserData = 0; SftBox_SetDeleteCallback(m_hwndFonts, &Parm); // FONT STYLE combo box SftBox_SetSelectionStyle(m_hwndStyle, SFTBOX_SELECTION_CELL1); SftBox_SetDroppedHeight(m_hwndStyle, 4); SftBox_SetResizeHeader(m_hwndStyle, FALSE); SftBox_SetReorderColumns(m_hwndStyle, FALSE); SftBox_SetShowHeader(m_hwndStyle, SFTBOX_COLSTYLE_BUTTONTEXT); SftBox_EnableSortIndicators(m_hwndStyle, TRUE, FALSE);// not sorted, but the column title is as tall as the others SftBox_SetDarkMode(m_hwndStyle, SFTBOX_DARKMODE_AUTO); InitColumn(&aCols[0], 0, 400, TEXT("Style")); SftBox_SetColumns(m_hwndStyle, 1, aCols); // FONT SIZES combo box SftBox_SetSelectionStyle(m_hwndSizes, SFTBOX_SELECTION_CELL1); SftBox_SetDroppedHeight(m_hwndSizes, 8); SftBox_SetResizeHeader(m_hwndSizes, FALSE); SftBox_SetReorderColumns(m_hwndSizes, FALSE); SftBox_SetShowHeader(m_hwndSizes, SFTBOX_COLSTYLE_BUTTONTEXT); SftBox_EnableSortIndicators(m_hwndSizes, TRUE, FALSE); SftBox_SetDarkMode(m_hwndSizes, SFTBOX_DARKMODE_AUTO); InitColumn(&aCols[0], 0, 400, TEXT("Size")); SftBox_SetColumns(m_hwndSizes, 1, aCols); SetSortIndicator(m_hwndSizes, m_fSizesDescending); SftBox_LimitText(m_hwndSizes, 3); } static void EqualizeHeights(void) { // 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 = SftBox_GetItemHeight(m_hwndFonts, -1); height = max(height, SftBox_GetItemHeight(m_hwndStyle, -1)); height = max(height, SftBox_GetItemHeight(m_hwndSizes, -1)); SftBox_SetItemHeightMinMax(m_hwndFonts, -1, height, 0); SftBox_SetItemHeightMinMax(m_hwndStyle, -1, height, 0); SftBox_SetItemHeightMinMax(m_hwndSizes, -1, height, 0); } /**********************************************************************/ /* Dialog Proc */ /**********************************************************************/ INT_PTR CALLBACK MainDialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_INITDIALOG: SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_APP))); m_hwndFonts = GetDlgItem(hwndDlg, IDC_FONTS); m_hwndStyle = GetDlgItem(hwndDlg, IDC_FONTSTYLE); m_hwndSizes = GetDlgItem(hwndDlg, IDC_FONTSIZE); InitControls(); AddFonts(); // selects a font, which adds its styles and sizes EqualizeHeights(); SftDarkMode_ApplyToDialog(hwndDlg); return TRUE; case WM_DESTROY: ClearHDC(); break; case WM_COMMAND: { HWND hwndCtl = (HWND) lParam; int code = HIWORD(wParam); switch (LOWORD(wParam)) { case IDC_FONTS: // FONT NAMES switch (code) { case SFTBOXN_SELCHANGE: { TCHAR szFont[LF_FACESIZE]; LONG index = SftBox_GetCurSel(hwndCtl); if (index >= 0 && SftBox_GetTextLength(hwndCtl, index, 0) < LF_FACESIZE) { SftBox_GetText(hwndCtl, index, 0, szFont); AddSizes(szFont); AddStyles(szFont); } PaintSample(hwndDlg, GetDlgItem(hwndDlg, IDC_SAMPLE)); break; } case SFTBOXN_MAIN_NOTIFICATION(SFTBOXN_LBUTTONDOWN_COLUMN): // a column title was clicked if (SftBox_GetHeaderButton(hwndCtl) == 0) { m_fFontsDescending = !m_fFontsDescending; SortFonts(); SetSortIndicator(hwndCtl, m_fFontsDescending); } break; case SFTBOXN_SELENDOK: case SFTBOXN_SELENDCANCEL: SftBox_SetHorizontalOffset(hwndCtl, 0); break; } break; case IDC_FONTSTYLE: // FONT STYLE if (code == SFTBOXN_SELCHANGE) PaintSample(hwndDlg, GetDlgItem(hwndDlg, IDC_SAMPLE)); break; case IDC_FONTSIZE: // FONT SIZES switch (code) { case SFTBOXN_SELCHANGE: { LONG index = SftBox_GetCurSel(hwndCtl); if (index >= 0) { SFTBOX_CELLINFOPARM CellParm; CellParm.index = index; CellParm.iCol = 0; SftBox_GetCellInfo(hwndCtl, &CellParm); m_SizeSelected = (int) CellParm.Cell.cellData; } PaintSample(hwndDlg, GetDlgItem(hwndDlg, IDC_SAMPLE)); break; } case SFTBOXN_EDITCHANGE: { // a size was typed TCHAR szBuffer[80]; GetWindowText(hwndCtl, szBuffer, sizeof(szBuffer)/sizeof(szBuffer[0])); m_SizeSelected = _ttoi(szBuffer); if (m_SizeSelected <= 0) m_SizeSelected = 10; PaintSample(hwndDlg, GetDlgItem(hwndDlg, IDC_SAMPLE)); break; } case SFTBOXN_MAIN_NOTIFICATION(SFTBOXN_LBUTTONDOWN_COLUMN): // the column title was clicked m_fSizesDescending = !m_fSizesDescending; SortSizes(); SetSortIndicator(hwndCtl, m_fSizesDescending); break; } break; case IDC_PRINTER: { // show the fonts of a printer PRINTDLG pd; memset(&pd, 0, sizeof(PRINTDLG)); pd.lStructSize = sizeof(PRINTDLG); pd.hwndOwner = hwndDlg; pd.Flags = PD_RETURNDC; if (PrintDlg(&pd)) { SetHDC(pd.hDC, pd.hDevMode, pd.hDevNames); AddFonts(); } break; } case IDCANCEL: EndDialog(hwndDlg, 0); return TRUE; } break; } case WM_PAINT: PaintSample(hwndDlg, GetDlgItem(hwndDlg, IDC_SAMPLE)); 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; }