www.pudn.com > MultiDragImg_src.zip > CListCtrlEx.cpp
CImageList *CListCtrlEx::CreateDragImageEx( LPPOINT lpPoint )
{
CRect cSingleRect;
CRect cCompleteRect( 0,0,0,0 );
int nIdx;
BOOL bFirst = TRUE;
//
// Determine the size of the drag image
//
POSITION pos = GetFirstSelectedItemPosition();
while (pos)
{
nIdx = GetNextSelectedItem( pos );
GetItemRect( nIdx, cSingleRect, LVIR_BOUNDS );
if (bFirst)
{
// Initialize the CompleteRect
GetItemRect( nIdx, cCompleteRect, LVIR_BOUNDS );
bFirst = FALSE;
}
cCompleteRect.UnionRect( cCompleteRect, cSingleRect );
}
//
// Create bitmap in memory DC
//
CClientDC cDc(this);
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
//
CImageList *pSingleImageList;
CPoint cPt;
pos = GetFirstSelectedItemPosition();
while (pos)
{
nIdx = GetNextSelectedItem( pos );
GetItemRect( nIdx, cSingleRect, LVIR_BOUNDS );
pSingleImageList = CreateDragImage( nIdx, &cPt);
if (pSingleImageList)
{
pSingleImageList->DrawIndirect( &cMemDC,
0,
CPoint( cSingleRect.left-cCompleteRect.left,
cSingleRect.top-cCompleteRect.top ),
cSingleRect.Size(),
CPoint(0,0));
delete pSingleImageList;
}
}
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( &cCursorPos );
lpPoint->x = cCursorPos.x - cCompleteRect.left;
lpPoint->y = cCursorPos.y - cCompleteRect.top;
}
return( pCompleteImageList );
}