February 28, 2021
Hot Topics:

Creating Drag Image for a CTreeCtrl without images

  • By Pål K Tønder
  • Send Email »
  • More Articles »

This article was contributed by Pål K Tønder.

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 bitmapCClientDCdc (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 colormemDC.SetTextColor(GetSysColor(COLOR_GRAYTEXT));memDC.TextOut(rect.left, rect.top, GetItemText(hItem));memDC.SelectObject( pOldFont );memDC.SelectObject( pOldMemDCBitmap );// Create imagelistCImageList* 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 colorreturn pImageList;}


Date Posted: August 4, 1999

This article was originally published on August 4, 1999

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

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