Introduction
This article describes how to create a multi select drag image in CTreeCtrl
. I have created a custom tree control CDragDropTreeCtrl
(derived from CTreeCtrl
) for adding drag and drop support. CreateDragImageEx()
is the core function, it creates the drag image list.
CDragDropTreeCtrl
This class implements multiple selection with mouse, with the Ctrl or Shift key down. The DragDetect()
API is used to detect the start of drag. Once the dragging is detected, the OnDrag()
function is invoked. The OnDrag()
function invokes CreateImageEx()
to create the drag image list. The rest is the same as single selection drag. BeginDrag()
and DragEnter()
functions are used to prepare the image list for dragging. In the mouse move event, the drag image is moved.
CreateDragImageEx
This is the core function, which creates the drag image list. Steps in creating drag image list are:
- Get the image list associated with the tree control
- Get the icon size with the help of the API
ImageList_GetIconSize()
- Calculate the bounding rect of the drag image
- Calculate the maximum width
- Calculate the height of single item and multiply it with list size to get the total height
- Create a memory device context
- Prepare the memory DC
- Create and prepare bitmap object
- Select Bitmap into the memory DC
- For each element in the list, extract the icon and associated text and draw them to the memory DC
- Create a new image list
- Add the bitmap to the image list
CImageList *pImageList = GetImageList(TVSIL_NORMAL);
int cx,cy;
ImageList_GetIconSize(*pImageList, &cx, &cy);
for ( nIdx = 0; nIdx < nNumSelected; nIdx++)
{
hItem = m_vSelItem[nIdx];
strItemText = GetItemText(hItem);
rectFirstItem.SetRectEmpty();
pDragImageCalcDC->DrawText(strItemText, rectFirstItem, DT_CALCRECT);
if(nMaxWidth < ( rectFirstItem.Width()+cx))
nMaxWidth = rectFirstItem.Width()+cx;
}
hItem = m_vSelItem[0];
strItemText = GetItemText(hItem);
rectFirstItem.SetRectEmpty();
pDragImageCalcDC->DrawText(strItemText, rectFirstItem, DT_CALCRECT);
rectTextArea.SetRect(1, 1, nMaxWidth, rectFirstItem.Height());
rectBounding.SetRect(0,0, nMaxWidth+2,
(rectFirstItem.Height()+2)*nNumSelected);
CDC MemoryDC;
if(!MemoryDC.CreateCompatibleDC(&DraggedNodeDC))
return NULL;
CBitmap DraggedNodeBmp;
if(!DraggedNodeBmp.CreateCompatibleBitmap(&DraggedNodeDC,
rectBounding.Width(), rectBounding.Height()))
return NULL;
pBitmapOldMemDCBitmap = MemoryDC.SelectObject( &DraggedNodeBmp );
for( nIdx = 0; nIdx < nNumSelected; nIdx++)
{
hItem = m_vSelItem[nIdx];
int nImg = 0,nSelImg=0;
GetItemImage(hItem,nImg,nSelImg);
HICON hIcon = pImageList->ExtractIcon(nImg);
MemoryDC.MoveTo(rectTextArea.left,rectTextArea.top);
if( nIdx != nNumSelected-1 )
{
MemoryDC.LineTo(rectTextArea.left,rectTextArea.top+18);
}
else
{
MemoryDC.LineTo(rectTextArea.left,rectTextArea.top+8);
}
MemoryDC.MoveTo(rectTextArea.left,rectTextArea.top+8);
MemoryDC.LineTo(rectTextArea.left+5,rectTextArea.top+8);
int nLeft = rectTextArea.left;
rectTextArea.left += 3;
::DrawIconEx(MemoryDC.m_hDC,rectTextArea.left,rectTextArea.top,hIcon,
16,16,0,NULL,DI_NORMAL);
rectTextArea.left += cx;
MemoryDC.Rectangle(rectTextArea);
MemoryDC.DrawText(GetItemText(hItem), rectTextArea,
DT_LEFT| DT_SINGLELINE|DT_NOPREFIX);
rectTextArea.left = nLeft;
rectTextArea.OffsetRect(0, rectFirstItem.Height()+2);
DestroyIcon(hIcon);
}
pImageListDraggedNode = new CImageList;
pImageListDraggedNode->Create(rectBounding.Width(), rectBounding.Height(),
ILC_COLOR | ILC_MASK, 0, 1);
pImageListDraggedNode->Add(&DraggedNodeBmp, RGB(255, 255,255));
Conclusion
This CDragDropTreeCtrl
implements multiple selection with mouse only. The keyboard support will be updated later. With this method for creating drag image list, we can create drag image for list controls also.