HeaderPane
Main
Hide
Share Link
Print

Drag & Drop

A combo box control can be the source of a drag & drop operation. The user can drag an item of the dropdown portion or the selected text of the edit control portion and drop it on the same combo box, on another combo box or on any other window. SftBox/DLL detects the drag & drop operation, sends notifications to the parent window as the operation progresses and can highlight a drop target in a combo box. Processing the drop, such as moving or copying an item, is up to the application.

Enabling Drag & Drop

Drag & drop is enabled using SetDragType:

SFTBOX_DRAG_NONEDrag & drop is not available. This is the default.
SFTBOX_DRAG_EDITThe selected text of the edit control portion can be dragged (simple combo box and dropdown combo box). The operation starts when the user presses the left mouse button on the selected text.
SFTBOX_DRAG_DROPDOWNThe items of the dropdown portion can be dragged. The operation starts when the user presses a mouse button on an item and moves the mouse cursor by a small distance (as defined by Windows) without releasing the mouse button.
SFTBOX_DRAG_EDITDROPDOWNBoth the selected text of the edit control portion and the items of the dropdown portion can be dragged.

Notifications

During a drag & drop operation, the parent window of the combo box where the operation started receives the following notifications as WM_COMMAND messages:

NotificationSent when
SFTBOXN_BEGINDRAGThe user starts dragging. The application can abort the operation using the fAbort member described below or by sending a WM_CANCELMODE message to the combo box.
SFTBOXN_DRAGGINGThe mouse cursor has moved. The application updates the mouse cursor and the drop target.
SFTBOXN_ENDDRAGThe user has released the mouse button. The application processes the drop.
SFTBOXN_CANCELDRAGThe operation has ended without a drop, for example because the combo box received a WM_CANCELMODE message. The application removes any drop target indication.

While handling these notifications, GetDragInfo returns a pointer to the SFTBOX_DRAGINFO structure of the combo box, which describes the operation in progress:

MemberDescription
hwndThe window the mouse cursor is located on.
ptDragThe position of the mouse cursor in screen coordinates.
areaThe portion where the operation started, SFTBOX_AREA_EDIT (edit control portion) or SFTBOX_AREA_DROPDOWN (dropdown portion).
buttonThe mouse button used (SFTBOX_LBUTTON, SFTBOX_MBUTTON or SFTBOX_RBUTTON).
fControl, fShiftNonzero if the Control key or the Shift key is pressed.
fAbortSet by the application to TRUE while handling SFTBOXN_BEGINDRAG to abort the operation. An aborted operation sends no further notifications, not even SFTBOXN_CANCELDRAG. An operation started in the edit control portion can also be ended by setting fAbort while handling SFTBOXN_DRAGGING, followed by SFTBOXN_CANCELDRAG.
hCursorSet by the application to define the mouse cursor shown, for example to tell the user whether a drop is possible at the current location.
UserDataAn application defined value, which is preserved until the operation ends.

Showing the Drop Target

If the target of the operation is a SftBox/DLL combo box, it can show where the dragged data will be dropped. CalcIndexFromPoint translates a position into an item index, SetDropHighlight marks an item as the drop target and GetDropHighlight returns the current drop target. SetDropHighlight with an index of -1 removes the drop target indication. SetDropHighlightStyle defines its appearance:

SFTBOX_DROPHIGHLIGHT_ONTOPA rectangle is drawn around the target item, using the colorDropHighlight color of the SFTBOX_COLORS structure. If the selection is rendered as an outline (SFTBOX_SELECTION_OUTLINE, see SetSelectionStyle), the target item is drawn with an outline instead, using the droptarget... colors of the SFTBOX_COLORS structure. This is the default.
SFTBOX_DROPHIGHLIGHT_BETWEENA line is drawn before the target item, using the colorDropHighlight color. The index is used as insertion point and can be set to the number of items to show the insertion point after the last item.

If the fScroll parameter of SetDropHighlight is TRUE and the drop target is the first or last item displayed, the items scroll automatically, so the user can reach a target that is not currently visible.

Step by Step

The following steps are taken from the DragDrop sample, which uses two simple combo boxes. An item dragged from the list of one combo box is moved to the other combo box. Selected text dragged from the edit control portion is added to the other combo box as a new item.

1. Initialization - Both combo boxes allow dragging and show the insertion point as a line between items.

SftBox_SetDragType(hwndBox, SFTBOX_DRAG_EDITDROPDOWN);
SftBox_SetDropHighlightStyle(hwndBox, SFTBOX_DROPHIGHLIGHT_BETWEEN);

2. Notifications - The dialog procedure dispatches the notifications. The combo box where the operation started is the source (lParam).

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 */
            OnCancelDrag((HWND) lParam);
            break;
        }
        break;
    }
    break;

3. The operation starts - The application remembers the item being dragged in the UserData member. An operation started in the dropdown portion ends with SFTBOXN_CANCELDRAG if no item is selected.

static void OnBeginDrag(HWND hwndSource)
{
    LPSFTBOX_DRAGINFO lpInfo = SftBox_GetDragInfo(hwndSource);

    lpInfo->UserData = (SFTBOX_DWORD_PTR) SftBox_GetCurSel(hwndSource);
}

4. The mouse cursor moves - The application tests whether the mouse cursor is located on the target combo box. A combo box consists of several windows, so the window under the mouse cursor (hwnd member) can be the combo box or one of its child windows. If the target is valid, the position is translated to an item index and the drop target is shown. CalcIndexFromPoint expects a position relative to the combo box window and reports whether the position is on the static portion or edit control portion (fMain), in which case the item is added at the end. The mouse cursor is updated to reflect whether a drop is possible.

static BOOL IsOver(HWND hwndBox, HWND hwndPoint)
{
    return hwndPoint && (hwndPoint == hwndBox || IsChild(hwndBox, hwndPoint));
}

static void OnDragging(HWND hwndSource)
{
    LPSFTBOX_DRAGINFO lpInfo = SftBox_GetDragInfo(hwndSource);
    HWND hwndTarget = GetTarget(hwndSource);    /* the other combo box */

    if (IsOver(hwndTarget, lpInfo->hwnd)) {
        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);
    }
}

5. The drop - The application retrieves the insertion point from the target combo box, removes the drop target indication and makes sure that the drop occurred on the target. Then the data is transferred: the selected text of the edit control portion is copied, an item of the list is deleted from the source. Finally, the new item is inserted into the target combo box and selected.

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

6. The operation is canceled - The drop target indication is removed.

static void OnCancelDrag(HWND hwndSource)
{
    SftBox_SetDropHighlight(GetTarget(hwndSource), -1, FALSE);
}

The complete source code can be found in the DragDrop sample.


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