This sample illustrates drag & drop between two simple combo boxes. An item dragged from one list to the other is moved, text selected in an edit control portion and dragged to the other list is copied. It covers enabling drag & drop using SetDragType, handling the SFTBOXN_BEGINDRAG, SFTBOXN_DRAGGING, SFTBOXN_ENDDRAG and SFTBOXN_CANCELDRAG notifications using GetDragInfo and the SFTBOX_DRAGINFO structure, and showing the insertion point with SetDropHighlight and the SFTBOX_DROPHIGHLIGHT_BETWEEN drop highlight style.
The source code is located at C:\Program Files (x86)\Softelvdm\SftBox DLL 6.0\Samples\C\DragDrop\DragDrop.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 "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 HWND m_hwndLeft; // Available items - SftBox combo box control HWND m_hwndRight; // Selected items - SftBox combo box control /**********************************************************************/ /* Combo Box Setup */ /**********************************************************************/ static void InitList(HWND hwndBox, LPCTSTR * alpszItems, int count) { int i; // Items can be dragged from the list, selected text from the edit portion SftBox_SetDragType(hwndBox, SFTBOX_DRAG_EDITDROPDOWN); // A line between items shows where a dragged item will be inserted SftBox_SetDropHighlightStyle(hwndBox, SFTBOX_DROPHIGHLIGHT_BETWEEN); SftBox_SetDarkMode(hwndBox, SFTBOX_DARKMODE_AUTO); for (i = 0 ; i < count ; ++i) SftBox_AddString(hwndBox, alpszItems[i]); if (count > 0) SftBox_SetCurSel(hwndBox, 0); } /**********************************************************************/ /* Drag and Drop */ /**********************************************************************/ static HWND GetTarget(HWND hwndSource) { return (hwndSource == m_hwndLeft) ? m_hwndRight : m_hwndLeft; } static BOOL IsOver(HWND hwndBox, HWND hwndPoint) { // A combo box consists of several windows return hwndPoint && (hwndPoint == hwndBox || IsChild(hwndBox, hwndPoint)); } static void OnBeginDrag(HWND hwndSource) { LPSFTBOX_DRAGINFO lpInfo = SftBox_GetDragInfo(hwndSource); // Remember the item being dragged. The user data is preserved // until the drag and drop operation ends. lpInfo->UserData = (SFTBOX_DWORD_PTR) SftBox_GetCurSel(hwndSource); if (lpInfo->area == SFTBOX_AREA_DROPDOWN && (LONG) lpInfo->UserData < 0) lpInfo->fAbort = TRUE; // nothing to drag } static void OnDragging(HWND hwndSource) { LPSFTBOX_DRAGINFO lpInfo = SftBox_GetDragInfo(hwndSource); HWND hwndTarget = GetTarget(hwndSource); if (IsOver(hwndTarget, lpInfo->hwnd)) { // Over the other combo box, show where the item will be inserted POINT pt = lpInfo->ptDrag; BOOL fMain; LONG index; ScreenToClient(hwndTarget, &pt); index = SftBox_CalcIndexFromPoint(hwndTarget, &pt, &fMain); SftBox_SetDropHighlight(hwndTarget, fMain ? -1 : index, TRUE); lpInfo->hCursor = LoadCursor(g_hInst, MAKEINTRESOURCE(IDC_DRAG)); } else { SftBox_SetDropHighlight(hwndTarget, -1, FALSE); lpInfo->hCursor = LoadCursor(NULL, IDC_NO); } } static void OnEndDrag(HWND hwndSource) { LPSFTBOX_DRAGINFO lpInfo = SftBox_GetDragInfo(hwndSource); HWND hwndTarget = GetTarget(hwndSource); LONG insertAt = SftBox_GetDropHighlight(hwndTarget); TCHAR szText[256]; LONG index; SftBox_SetDropHighlight(hwndTarget, -1, FALSE); if (!IsOver(hwndTarget, lpInfo->hwnd)) return; // dropped somewhere else szText[0] = 0; if (lpInfo->area == SFTBOX_AREA_EDIT) { // The selected text of the edit portion is copied if (SftBox_GetSelTextLength(hwndSource) < sizeof(szText) / sizeof(szText[0])) SftBox_GetSelText(hwndSource, szText); } else { // An item of the list is moved LONG from = (LONG) lpInfo->UserData; if (SftBox_GetTextLength(hwndSource, from, 0) < sizeof(szText) / sizeof(szText[0])) { SftBox_GetText(hwndSource, from, 0, szText); SftBox_DeleteString(hwndSource, from); if (SftBox_GetCount(hwndSource) > 0) SftBox_SetCurSel(hwndSource, min(from, SftBox_GetCount(hwndSource) - 1)); } } if (!szText[0]) return; index = SftBox_InsertString(hwndTarget, insertAt, szText);// -1 adds the item at the end SftBox_SetCurSel(hwndTarget, index); } static void OnCancelDrag(HWND hwndSource) { SftBox_SetDropHighlight(GetTarget(hwndSource), -1, FALSE); } /**********************************************************************/ /* Dialog Proc */ /**********************************************************************/ INT_PTR CALLBACK MainDialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_INITDIALOG: { static LPCTSTR alpszAvailable[] = { TEXT("Apples"), TEXT("Bananas"), TEXT("Cherries"), TEXT("Grapes"), TEXT("Lemons"), TEXT("Mangos"), TEXT("Oranges"), TEXT("Peaches"), TEXT("Pears"), TEXT("Plums"), }; static LPCTSTR alpszSelected[] = { TEXT("Strawberries"), TEXT("Watermelons"), }; SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_APP))); m_hwndLeft = GetDlgItem(hwndDlg, IDC_AVAILABLE); m_hwndRight = GetDlgItem(hwndDlg, IDC_SELECTED); InitList(m_hwndLeft, alpszAvailable, sizeof(alpszAvailable) / sizeof(alpszAvailable[0])); InitList(m_hwndRight, alpszSelected, sizeof(alpszSelected) / sizeof(alpszSelected[0])); SftDarkMode_ApplyToDialog(hwndDlg); return TRUE; } case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_AVAILABLE: case IDC_SELECTED: switch (HIWORD(wParam)) { case SFTBOXN_BEGINDRAG: // the user starts dragging OnBeginDrag((HWND) lParam); OnDragging((HWND) lParam); break; case SFTBOXN_DRAGGING: // the mouse moved OnDragging((HWND) lParam); break; case SFTBOXN_ENDDRAG: // the mouse button was released OnEndDrag((HWND) lParam); break; case SFTBOXN_CANCELDRAG: // ended without a drop (ESCAPE key) OnCancelDrag((HWND) lParam); 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; }