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)
   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


Comments

  • Thanks a lot guys !!!

    Posted by Legacy on 06/12/2003 12:00am

    Originally posted by: Nazouille

    work fine ;)

    Reply
  • Increase the Height of the ListCtrl

    Posted by Legacy on 07/31/2002 12:00am

    Originally posted by: ShanPalani

    Hi,

    This is Shan Palani. How do I increase the height of the CListCtrl. Please help me on this. URGENT.

    Regards,
    ShanPalani

    Reply
  • Two minor issues

    Posted by Legacy on 04/24/2001 12:00am

    Originally posted by: Tim Kosse

    In CreateDragImageListEx the ImageList is only createsd with the default color depth (ILC_COLOR). To display high color icons, it should be ILC_COLOR32

    pCompleteImageList->Create(rectComplete.Width(), rectComplete.Height(), ILC_COLOR32 | ILC_MASK, 0, 1);

    I had a problem with the following line:
    pSingleImageList->Draw( &dcMem, 0, CPoint(rectSingle.left - rectComplete.left, rectSingle.top - rectComplete.top), ILD_MASK);

    I only saw the mask of the item icon while dragged.
    It should be ILD_NORMAL instead of ILD_MASK
    pSingleImageList->Draw( &dcMem, 0, CPoint(rectSingle.left - rectComplete.left, rectSingle.top - rectComplete.top), ILD_NORMAL);

    Reply
  • small improvement in BeginDrag

    Posted by Legacy on 04/04/2001 12:00am

    Originally posted by: 1st.app

    this:
    m_pDragImage->DragEnter(NULL, pNMListView->ptAction);

    will cause a one-time flicker at left-top corner of screen.
    because the second parameter is relative to left-top of screen.

    this will not happern if change to:
    ...
    CRect rect;
    ::GetWindowRect(pList->m_hWnd, &rect);
    CPOint pt = pNMListView->ptAction;
    pt.Offset(rect.left, rect.top);
    m_pDragImage->DragEnter(NULL, pt);


    thanks for your code.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds