HeaderPane
Main
Hide
Share Link
Print

DragDrop Sample (C++/MFC)

This sample illustrates drag & drop between two simple combo boxes, hosted in an MFC dialog through CSftBox. 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 through ON_CONTROL 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\MFC\DragDrop.

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

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

/////////////////////////////////////////////////////////////////////////////
// CMainDlg dialog

BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
    ON_CONTROL(SFTBOXN_BEGINDRAG, IDC_AVAILABLE, OnAvailableBeginDrag)
    ON_CONTROL(SFTBOXN_DRAGGING, IDC_AVAILABLE, OnAvailableDragging)
    ON_CONTROL(SFTBOXN_ENDDRAG, IDC_AVAILABLE, OnAvailableEndDrag)
    ON_CONTROL(SFTBOXN_CANCELDRAG, IDC_AVAILABLE, OnAvailableCancelDrag)
    ON_CONTROL(SFTBOXN_BEGINDRAG, IDC_SELECTED, OnSelectedBeginDrag)
    ON_CONTROL(SFTBOXN_DRAGGING, IDC_SELECTED, OnSelectedDragging)
    ON_CONTROL(SFTBOXN_ENDDRAG, IDC_SELECTED, OnSelectedEndDrag)
    ON_CONTROL(SFTBOXN_CANCELDRAG, IDC_SELECTED, OnSelectedCancelDrag)
END_MESSAGE_MAP()

CMainDlg::CMainDlg(CWnd* pParent) : CDialog(CMainDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_AVAILABLE, m_Available);
    DDX_Control(pDX, IDC_SELECTED, m_Selected);
}

void CMainDlg::InitList(CSftBox& box, LPCTSTR* alpszItems, int count)
{
    // Items can be dragged from the list, selected text from the edit portion
    box.SetDragType(SFTBOX_DRAG_EDITDROPDOWN);
    // A line between items shows where a dragged item will be inserted
    box.SetDropHighlightStyle(SFTBOX_DROPHIGHLIGHT_BETWEEN);
    box.SetDarkMode(SFTBOX_DARKMODE_AUTO);

    for (int i = 0 ; i < count ; ++i)
        box.AddString(alpszItems[i]);
    if (count > 0)
        box.SetCurSel(0);
}

BOOL CMainDlg::OnInitDialog()
{
    static LPCTSTR alpszAvailable[] = {
        _T("Apples"), _T("Bananas"), _T("Cherries"), _T("Grapes"), _T("Lemons"),
        _T("Mangos"), _T("Oranges"), _T("Peaches"), _T("Pears"), _T("Plums"),
    };
    static LPCTSTR alpszSelected[] = {
        _T("Strawberries"), _T("Watermelons"),
    };

    CDialog::OnInitDialog();

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

    InitList(m_Available, alpszAvailable, sizeof(alpszAvailable) / sizeof(alpszAvailable[0]));
    InitList(m_Selected, alpszSelected, sizeof(alpszSelected) / sizeof(alpszSelected[0]));

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

/////////////////////////////////////////////////////////////////////////////
// Drag and drop

static BOOL IsOver(const CSftBox& box, HWND hwndPoint)
{
    // A combo box consists of several windows
    return hwndPoint && (hwndPoint == box.m_hWnd || ::IsChild(box.m_hWnd, hwndPoint));
}

void CMainDlg::BeginDrag(CSftBox& source)
{
    LPSFTBOX_DRAGINFO lpInfo = source.GetDragInfo();

    // Remember the item being dragged. The user data is preserved
    // until the drag and drop operation ends.
    lpInfo->UserData = (SFTBOX_DWORD_PTR) source.GetCurSel();
    if (lpInfo->area == SFTBOX_AREA_DROPDOWN && (LONG) lpInfo->UserData < 0)
        lpInfo->fAbort = TRUE;          // nothing to drag
}

void CMainDlg::Dragging(CSftBox& source, CSftBox& target)
{
    LPSFTBOX_DRAGINFO lpInfo = source.GetDragInfo();

    if (IsOver(target, lpInfo->hwnd)) {
        // Over the other combo box, show where the item will be inserted
        CPoint pt = lpInfo->ptDrag;
        BOOL fMain;
        target.ScreenToClient(&pt);
        LONG index = target.CalcIndexFromPoint(&pt, &fMain);
        target.SetDropHighlight(fMain ? -1 : index, TRUE);
        lpInfo->hCursor = AfxGetApp()->LoadCursor(IDC_DRAG);
    } else {
        target.SetDropHighlight(-1, FALSE);
        lpInfo->hCursor = AfxGetApp()->LoadStandardCursor(IDC_NO);
    }
}

void CMainDlg::EndDrag(CSftBox& source, CSftBox& target)
{
    LPSFTBOX_DRAGINFO lpInfo = source.GetDragInfo();
    LONG insertAt = target.GetDropHighlight();
    TCHAR szText[256];

    target.SetDropHighlight(-1, FALSE);
    if (!IsOver(target, 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 (source.GetSelTextLength() < sizeof(szText) / sizeof(szText[0]))
            source.GetSelText(szText);
    } else {
        // An item of the list is moved
        LONG from = (LONG) lpInfo->UserData;
        if (source.GetTextLength(from, 0) < sizeof(szText) / sizeof(szText[0])) {
            source.GetText(from, 0, szText);
            source.DeleteString(from);
            if (source.GetCount() > 0)
                source.SetCurSel(min(from, source.GetCount() - 1));
        }
    }
    if (!szText[0])
        return;

    LONG index = target.InsertString(insertAt, szText);// -1 adds the item at the end
    target.SetCurSel(index);
}

void CMainDlg::OnAvailableBeginDrag()   // the user starts dragging
{
    BeginDrag(m_Available);
    Dragging(m_Available, m_Selected);
}

void CMainDlg::OnAvailableDragging()    // the mouse moved
{
    Dragging(m_Available, m_Selected);
}

void CMainDlg::OnAvailableEndDrag()     // the mouse button was released
{
    EndDrag(m_Available, m_Selected);
}

void CMainDlg::OnAvailableCancelDrag()  // ended without a drop (ESCAPE key)
{
    m_Selected.SetDropHighlight(-1, FALSE);
}

void CMainDlg::OnSelectedBeginDrag()
{
    BeginDrag(m_Selected);
    Dragging(m_Selected, m_Available);
}

void CMainDlg::OnSelectedDragging()
{
    Dragging(m_Selected, m_Available);
}

void CMainDlg::OnSelectedEndDrag()
{
    EndDrag(m_Selected, m_Available);
}

void CMainDlg::OnSelectedCancelDrag()
{
    m_Available.SetDropHighlight(-1, FALSE);
}

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

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