// Create a drag image from a bitmap and optional cursor bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor) { if ( m_hImageList ) ImageList_Destroy(GetHimageList()); m_hImageList = 0; #ifdef __WXWINCE__ UINT flags = ILC_COLOR; #else UINT flags wxDUMMY_INITIALIZE(0) ; if (image.GetDepth() <= 4) flags = ILC_COLOR4; else if (image.GetDepth() <= 8) flags = ILC_COLOR8; else if (image.GetDepth() <= 16) flags = ILC_COLOR16; else if (image.GetDepth() <= 24) flags = ILC_COLOR24; else flags = ILC_COLOR32; #endif bool mask = (image.GetMask() != 0); // Curiously, even if the image doesn't have a mask, // we still have to use ILC_MASK or the image won't show // up when dragged. // if ( mask ) flags |= ILC_MASK; m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1); int index; if (!mask) { HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP(); index = ImageList_Add(GetHimageList(), hBitmap1, 0); } else { HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP(); HBITMAP hBitmap2 = (HBITMAP) image.GetMask()->GetMaskBitmap(); HBITMAP hbmpMask = wxInvertMask(hBitmap2); index = ImageList_Add(GetHimageList(), hBitmap1, hbmpMask); ::DeleteObject(hbmpMask); } if ( index == -1 ) { wxLogError(_("Couldn't add an image to the image list.")); } m_cursor = cursor; // Can only combine with drag image after calling BeginDrag. return (index != -1) ; }
static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask) { #if wxUSE_IMAGE wxBitmap bitmapWithMask; #endif // wxUSE_IMAGE HBITMAP hbmpMask; wxMask *pMask; bool deleteMask = false; if ( mask.IsOk() ) { hbmpMask = GetHbitmapOf(mask); pMask = NULL; } else { pMask = bitmap.GetMask(); #if wxUSE_IMAGE // check if we don't have alpha in this bitmap -- we can create a mask // from it (and we need to do it for the older systems which don't // support 32bpp bitmaps natively) if ( !pMask ) { wxImage img(bitmap.ConvertToImage()); if ( img.HasAlpha() ) { img.ConvertAlphaToMask(); bitmapWithMask = wxBitmap(img); pMask = bitmapWithMask.GetMask(); } } #endif // wxUSE_IMAGE if ( !pMask ) { // use the light grey count as transparent: the trouble here is // that the light grey might have been changed by Windows behind // our back, so use the standard colour map to get its real value wxCOLORMAP *cmap = wxGetStdColourMap(); wxColour col; wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from); pMask = new wxMask(bitmap, col); deleteMask = true; } hbmpMask = (HBITMAP)pMask->GetMaskBitmap(); } // windows mask convention is opposite to the wxWidgets one HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask); if ( deleteMask ) { delete pMask; } return hbmpMaskInv; }