CreateDragImage for (Unlimited) Multiply Selected Items

Environment: NT4 VC5 (also tested on WIN98 VC6)

This is based on the article

“CreateDragImage for multiple selected items in CListCtrl” by Frank Kobs
Thanks Frank for the idea!

I have some suggestions to Frank’s CreateDragImageEx() with the following advantages:

  • You can use the code in VC5 (of course, it can also be used in VC6).

  • You can select as many items to Drag and Drop.

    (i.e. when I selected 500 items to Drag&Drop, Frank’s CreateDragImageEx() didn’t work.

    In fact, for the items selected out of the ListCtrl’s ClientRect,

    called CreateDragImage() it returned a “blank” rectangle.

    Thus, it is not necessary to create a big bitmap with blank rows ).

  • You can make better use of the optional service.

    (i.e. at the end of Frank’s CreateDragImagEx(), if you press the mouse button then quickly move the hot point,

    the cursor will have a certain distance with the hot point of DragImage.

    That’s because the GetMousePos() will have a different position then the GetMessagePos().)

Here is my version of CreateDragImageEx():

CImageList* CMyList::CreateDragImageEx(LPPOINT lpPoint)
if (GetSelectedCount() <= 0) return NULL; // no row selected CRect rectSingle; CRect rectComplete(0,0,0,0); // Determine List Control Client width size GetClientRect(rectSingle); int nWidth = rectSingle.Width(); // Start and Stop index in view area int nIndex = GetTopIndex() - 1; int nBottomIndex = GetTopIndex() + GetCountPerPage() - 1; if (nBottomIndex > (GetItemCount() – 1))
nBottomIndex = GetItemCount() – 1;

// Determine the size of the drag image (limite for
// rows visibled and Client width)
while ((nIndex = GetNextItem(nIndex, LVNI_SELECTED)) != -1)
if (nIndex > nBottomIndex)

GetItemRect(nIndex, rectSingle, LVIR_BOUNDS);

if (rectSingle.left < 0) rectSingle.left = 0; if (rectSingle.right > nWidth)
rectSingle.right = nWidth;

rectComplete.UnionRect(rectComplete, rectSingle);

CClientDC dcClient(this);
CDC dcMem;
CBitmap Bitmap;

if (!dcMem.CreateCompatibleDC(&dcClient))
return NULL;

if (!Bitmap.CreateCompatibleBitmap(&dcClient,
return NULL;

CBitmap *pOldMemDCBitmap = dcMem.SelectObject(&Bitmap);
// Use green as mask color
dcMem.FillSolidRect(0, 0,

// Paint each DragImage in the DC
nIndex = GetTopIndex() – 1;
while ((nIndex = GetNextItem(nIndex, LVNI_SELECTED)) != -1)
if (nIndex > nBottomIndex)

CPoint pt;
CImageList* pSingleImageList = CreateDragImage(nIndex, &pt);

if (pSingleImageList)
GetItemRect(nIndex, rectSingle, LVIR_BOUNDS);

CPoint(rectSingle.left – rectComplete.left, –,

delete pSingleImageList;

CImageList* pCompleteImageList = new CImageList;
0, 1);

// Green is used as mask color
pCompleteImageList->Add(&Bitmap, RGB(0, 255, 0));

if (lpPoint)
lpPoint->x = rectComplete.left;
lpPoint->y =;

return pCompleteImageList;

I use my CreateDragImageEx to create the commonly dual ListCtrl selection manager.
(The CreateDragImageEx in the demo. is situated on Dialog level in order to
demonstrate another way to use CreateDragImageEx)

You can double click, use the move button, or Drag&Drop single or multiple items

to move from one list to another.
All items move to other list is automatically selected and placed at the top of
the list allowing the user to move back and forth to Undo.


Download demo project – 16 Kb

Download exe – 8 Kb

More by Author

Must Read