Microsoft & .NETVisual C#Creating Drag Image for a CTreeCtrl without images

Creating Drag Image for a CTreeCtrl without images

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;}

History

Date Posted: August 4, 1999

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories