How to drag item(s) from a listbox into the grid
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); // copy selection from list box
GlobalUnlock(hStr); // done modifying memory
// 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));
}
}
}
}