September 18, 2018
Hot Topics:

Creating Drag Image for a CTreeCtrl without images

  • August 4, 1999
  • 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

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

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

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


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