Creating Drag Image for a CTreeCtrl without images

The method CreateDragImage is used during drag'n drop to create a drag image. The problem is that it only works for a CTreeCtrl with images. The following method, CreateDragImageEx, checks whether the CTreeCtrl has a valid normal CImageList. If so, it just calls the standard CreateDragImage method and returns.

If, on the other hand, no valid CImageList is found, a bitmap is created based on the size of item rect, and the item text is drawn into it. Then a CImageList is created and the bitmap is added to it.


CImageList* CTreeCtrlEx::CreateDragImageEx(HTREEITEM hItem)
{
	if(GetImageList(TVSIL_NORMAL) != NULL)
		return CreateDragImage(hItem);

	CRect rect;
	GetItemRect(hItem, rect, TRUE);
	rect.top = rect.left = 0;

	// Create bitmap
	CClientDC	dc (this);
	CDC 		memDC;	

	if(!memDC.CreateCompatibleDC(&dc))
		return NULL;

	CBitmap bitmap;
	if(!bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()))
		return NULL;

	CBitmap* pOldMemDCBitmap = memDC.SelectObject( &bitmap );
	CFont* pOldFont = memDC.SelectObject(GetFont());

	memDC.FillSolidRect(&rect, RGB(0, 255, 0)); // Here green is used as mask color
	memDC.SetTextColor(GetSysColor(COLOR_GRAYTEXT));
	memDC.TextOut(rect.left, rect.top, GetItemText(hItem));

	memDC.SelectObject( pOldFont );
	memDC.SelectObject( pOldMemDCBitmap );

	// Create imagelist
	CImageList* pImageList = new CImageList;
	pImageList->Create(rect.Width(), rect.Height(), 
	 ILC_COLOR | ILC_MASK, 0, 1);
	pImageList->Add(&bitmap, RGB(0, 255, 0)); // Here green is used as mask color

	return pImageList;
}


Comments

  • Changing the colour?

    Posted by streetd on 07/17/2006 04:03am

    How do I go about chaning the colour of the text being dragged? All I seem to get is a grey despite SetTextColor() being used to set the colour I need? Thanks Dave

    Reply
  • Saved my butt!

    Posted by streetd on 07/12/2006 08:39am

    This code saved me from having to put images into my tree control, Pel K Txnder is a wizard! Thanks Dave

    Reply
  • Yep. This is excellent. THANKS!

    Posted by Legacy on 02/17/2004 12:00am

    Originally posted by: Mr Antelope

    Nice work!

    Reply
  • It really helped me

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

    Originally posted by: Tulasi Anand

    I was looking for drag drop without the image list and this piece of code really helped

    Reply
  • Wonderfull!!!

    Posted by Legacy on 06/19/2002 12:00am

    Originally posted by: Satya

    It works.

    Reply
  • Drag bitmap is too big

    Posted by Legacy on 11/10/1999 12:00am

    Originally posted by: Geert Delmeiren

    While I was playing with this code (that helped me a lot 
    
    while programming a multiple select treeview with drag & drop functionality)
    I remarked that the created drag bitmap is much too big.
    For one item this is maybe no problem, but in the case of
    multiple selects, this certainly reduces the redrawing
    performance while dragging.

    You can discover it by simply changing 255 in the line

    memDC.FillSolidRect(&rect2, RGB(0, 255, 0));

    into 250, so you see the bitmap range while dragging.

    The reason for this is the code line

    rect.top = rect.left = 0;

    This is my solution (you can check it in the same way):

    1. Remove the code line

    rect.top = rect.left = 0;

    2. Replace the code part

    memDC.FillSolidRect(&rect, RGB(0, 255, 0));
    memDC.SetTextColor(GetSysColor(COLOR_GRAYTEXT));
    memDC.TextOut(rect.left, rect.top, GetItemText(hItem));

    by

    CRect rect2(0,0, rect.Width(), rect.Height());
    memDC.FillSolidRect(&rect2, RGB(0, 255, 0));
    memDC.SetTextColor(GetSysColor(COLOR_GRAYTEXT));
    memDC.TextOut(0, 0, GetItemText(hItem));

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

Top White Papers and Webcasts

  • Mobile application management (MAM) refers to the workflow for security, governance, and distribution of mobile apps in the enterprise. Best-of-breed MAM provides app-level security for any app, deploys apps to every user in the extended enterprise because it is device management agnostic, manages the complete app lifecycle, and enables multiple app distribution methods, including an intuitive, custom-brandable enterprise app store. But, not all mobile application management (MAM) is created equal. Use this …

  • Enterprise cloud adoption has evolved rapidly from fringe curiosity to the mainstream. As enterprises increasingly move mission-critical workloads to the cloud, it's important to track best practices to ensure a seamless migration process. While CIOs are becoming increasingly mature and pragmatic in their approach to cloud, surprises and challenges still need to be addressed. Read this eBook to learn the key best practices for cloud deployment success, the importance of SLAs in choosing a cloud provider, and …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date