CBitmap* pOldMemDCBitmap = cMemDC.SelectObject(&cBitmap); // Here green is used as mask color cMemDC.FillSolidRect(0, 0, cCompleteRect.Width(), cCompleteRect.Height(), RGB(0, 255, 0));
cMemDC.SelectObject(pOldMemDCBitmap); // Create the imagelist with the merged drag images CImageList* pCompleteImageList = new CImageList;
pCompleteImageList->Create( cCompleteRect.Width(), cCompleteRect.Height(), ILC_COLOR | ILC_MASK, 0, 1); // Here green is used as mask color pCompleteImageList->Add(&cBitmap, RGB(0, 255, 0));
cBitmap.DeleteObject(); // as an optional service: // Find the offset of the current mouse cursor to the imagelist // this we can use in BeginDrag() if(lpPoint) { CPoint cCursorPos; GetCursorPos(&cCursorPos); ::ScreenToClient(hWnd, &cCursorPos); lpPoint->x = cCursorPos.x - cCompleteRect.left; lpPoint->y = cCursorPos.y - cCompleteRect.top; }
// 创建列表视图选中的拖拉效果图片
CImageList* ListView_CreateDragImageEx(HWND hWnd,
LPPOINT lpPoint)
{
CRect cSingleRect;
CRect cCompleteRect( 0,0,0,0 );
int nIdx = -1;
BOOL bFirst = TRUE;
// Determine the size of the drag image
for(;;)
{
nIdx = ListView_GetNextItem(hWnd, nIdx, LVIS_SELECTED);
if(nIdx == -1)
break;
ListView_GetItemRect(hWnd, nIdx, &cSingleRect, LVIR_BOUNDS);
if(bFirst)
{
// Initialize the CompleteRect
ListView_GetItemRect(hWnd, nIdx, &cCompleteRect, LVIR_BOUNDS);
bFirst = FALSE;
}
cCompleteRect.UnionRect(cCompleteRect, cSingleRect);
}
// Create bitmap in memory DC
CClientDC cDc(CWnd::FromHandle(hWnd));
CDC cMemDC;
CBitmap cBitmap;
if(!cMemDC.CreateCompatibleDC(&cDc))
return NULL;
if(!cBitmap.CreateCompatibleBitmap(&cDc, cCompleteRect.Width(), cCompleteRect.Height()))
return NULL;
CBitmap* pOldMemDCBitmap = cMemDC.SelectObject(&cBitmap);
// Here green is used as mask color
cMemDC.FillSolidRect(0, 0, cCompleteRect.Width(), cCompleteRect.Height(),
RGB(0, 255, 0));
// Paint each DragImage in the DC
CPoint cPt;
nIdx = -1;
for(;;)
{
nIdx = ListView_GetNextItem(hWnd, nIdx, LVIS_SELECTED);
if(nIdx == -1)
break;
ListView_GetItemRect(hWnd, nIdx, &cSingleRect, LVIR_BOUNDS);
HIMAGELIST hImageSingle = ListView_CreateDragImage(hWnd, nIdx, &cPt);
if(hImageSingle)
{
CImageList *pSingleImageList = CImageList::FromHandle(hImageSingle);
pSingleImageList->DrawIndirect( &cMemDC,
0,
CPoint(cSingleRect.left - cCompleteRect.left,
cSingleRect.top - cCompleteRect.top),
cSingleRect.Size(),
CPoint(0,0));
}
}
cMemDC.SelectObject(pOldMemDCBitmap);
// Create the imagelist with the merged drag images
CImageList* pCompleteImageList = new CImageList;
pCompleteImageList->Create( cCompleteRect.Width(),
cCompleteRect.Height(),
ILC_COLOR | ILC_MASK, 0, 1);
// Here green is used as mask color
pCompleteImageList->Add(&cBitmap, RGB(0, 255, 0));
cBitmap.DeleteObject(); // as an optional service:
// Find the offset of the current mouse cursor to the imagelist
// this we can use in BeginDrag() if(lpPoint)
{
CPoint cCursorPos;
GetCursorPos(&cCursorPos);
::ScreenToClient(hWnd, &cCursorPos);
lpPoint->x = cCursorPos.x - cCompleteRect.left;
lpPoint->y = cCursorPos.y - cCompleteRect.top;
}
return pCompleteImageList;
}