CImageList* CSelectionDlg::CreateDragImageEx(CListCtrl *pList, LPPOINT lpPoint)
{
if (pList->GetSelectedCount() <= 0)
return NULL; // no row selected
CRect rectSingle;
CRect rectComplete(0,0,0,0);
// Determine List Control Client width size
pList->GetClientRect(rectSingle);
int nWidth = rectSingle.Width();
// Start and Stop index in view area
int nIndex = pList->GetTopIndex() - 1;
int nBottomIndex = pList->GetTopIndex() + pList->GetCountPerPage() - 1;
if (nBottomIndex > (pList->GetItemCount() - 1))
nBottomIndex = pList->GetItemCount() - 1;
// Determine the size of the drag image (limite for rows visibled and Client width)
while ((nIndex = pList->GetNextItem(nIndex, LVNI_SELECTED)) != -1)
{
if (nIndex > nBottomIndex)
break;
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 = pList->GetTopIndex() - 1;
while ((nIndex = pList->GetNextItem(nIndex, LVNI_SELECTED)) != -1)
{
if (nIndex > nBottomIndex)
break;
dcMem.SelectObject(pOldMemDCBitmap);
CImageList* pCompleteImageList = new CImageList;
pCompleteImageList->Create(rectComplete.Width(), rectComplete.Height(), ILC_COLOR | ILC_MASK, 0, 1);
pCompleteImageList->Add(&Bitmap, RGB(0, 255, 0)); // Green is used as mask color
Bitmap.DeleteObject();
if (lpPoint)
{
lpPoint->x = rectComplete.left;
lpPoint->y = rectComplete.top;
}