Objective Grid: Dragging items from a listbox to the grid

Article ID: 80
Last updated: 22 Jun, 2018
Article ID: 80
Last updated: 22 Jun, 2018
Revision: 3
Views: 1906
Posted: 11 Jan, 2001
by Meltreger B.
Updated: 22 Jun, 2018
by Meltreger B.

Problem


How can I drag item(s) from a listbox into the grid?


Cause



Action


Call AfxOleInit() in your Initinstance method and register the grid as drop target using CGXGridDropTarget::Register():

// Register the grid as drop target
VERIFY(m_objDndDropTarget.Register(this, GX_DNDEGDESCROLL | GX_DNDAUTOSCROLL | GX_DNDTEXT /*| GX_DNDSTYLES |GX_DNDNOAPPENDCOLS|GX_DNDNOAPPENDROWS*/));

In your listbox override WM_LBUTTONDOWN:

void CToDoList::OnLButtonDown(UINT nFlags, CPoint point)
{
CListBox::OnLButtonDown(nFlags, point);

// Get current selection
int nCurSel = GetCurSel();

if (nCurSel != LB_ERR)
{

// Get length of current selection
int nLength = GetTextLen(nCurSel);

if (nLength)
{

// Allocate memory and get address to memory
HGLOBAL hStr = GlobalAlloc(GMEM_FIXED | GMEM_SHARE, nLength+1);
LPSTR pStr = (LPSTR)GlobalLock(hStr);

if (pStr)
{

GetText(nCurSel, pStr);
GlobalUnlock(hStr);

// Have to pass rectangle for now,
// there is a bug in the
// COleDataSource::DoDragDrop class.
CRect rect(0,0,0,0);

// Cache the text data
m_dataSource.CacheGlobalData(CF_TEXT, hStr);

// Perform the drag and drop operation
DROPEFFECT dropEffect = m_dataSource.DoDragDrop(DROPEFFECT_COPY | DROPEFFECT_MOVE, rect);

// remove the selection if
// this was a move operation
if (dropEffect == DROPEFFECT_MOVE)

DeleteString(nCurSel);
// The OLE library eats the
// button up message, make the
// list box happy by simulating
// another button up message.
PostMessage(WM_LBUTTONUP, nFlags, MAKELONG(point.x, point.y));
}
}
}
}

This article was:   Helpful | Not helpful
Report an issue
Article ID: 80
Last updated: 22 Jun, 2018
Revision: 3
Views: 1906
Posted: 11 Jan, 2001 by Meltreger B.
Updated: 22 Jun, 2018 by Meltreger B.

Others in this category