CreateDragImage for (Unlimited) Multiply Selected Items | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
May 1, 2000
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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)
   break;
  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,
     rectComplete.Width(),
     rectComplete.Height()))
  return NULL;
 CBitmap *pOldMemDCBitmap = dcMem.SelectObject(&Bitmap);
 // Use green as mask color
 dcMem.FillSolidRect(0, 0,
                     rectComplete.Width(),
                     rectComplete.Height(),
                     RGB(0,255,0));
 // Paint each DragImage in the DC
 nIndex = GetTopIndex() – 1;
 while ((nIndex = GetNextItem(nIndex, LVNI_SELECTED)) != -1)
 {
  if (nIndex > nBottomIndex)
   break;
  CPoint pt;
  CImageList* pSingleImageList = CreateDragImage(nIndex, &pt);
  if (pSingleImageList)
  {
   GetItemRect(nIndex, rectSingle, LVIR_BOUNDS);
   pSingleImageList->Draw(&dcMem,
    0,
    CPoint(rectSingle.left – rectComplete.left,
    rectSingle.top – rectComplete.top),
    ILD_MASK);
   pSingleImageList->DeleteImageList();
   delete pSingleImageList;
  }
 }
 dcMem.SelectObject(pOldMemDCBitmap);
 CImageList* pCompleteImageList = new CImageList;
 pCompleteImageList->Create(rectComplete.Width(),
                            rectComplete.Height(),
                            ILC_COLOR | ILC_MASK,
                            0, 1);
 // Green is used as mask color
 pCompleteImageList->Add(&Bitmap, RGB(0, 255, 0));
 Bitmap.DeleteObject();
 if (lpPoint)
 {
  lpPoint->x = rectComplete.left;
  lpPoint->y = rectComplete.top;
 }
 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.

Downloads

Download demo project – 16 Kb

Download exe – 8 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.