From 211b54b109f623310b6a7558a5f1b0ea44f5bdd8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 22 Mar 2003 23:47:06 +0000 Subject: [PATCH] added wxBitmapToHICON/CURSOR helper functions git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@19716 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/private.h | 11 ++++++ src/msw/bitmap.cpp | 75 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index a1308f5b47..9d639f1fb8 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -279,6 +279,17 @@ extern void PixelToHIMETRIC(LONG *x, LONG *y); // to invert the mask each time we pass one/get one to/from Windows extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w = 0, int h = 0); +// Creates an icon or cursor depending from a bitmap +// +// The bitmap must be valid and it should have a mask. If it doesn't, a default +// mask is created using light grey as the transparent colour. +extern HICON wxBitmapToHICON(const wxBitmap& bmp); + +// Same requirments as above apply and the bitmap must also have the correct +// size. +extern +HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY); + // get (x, y) from DWORD - notice that HI/LOWORD can *not* be used because they // will fail on system with multiple monitors where the coords may be negative // diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index 6a80f162c8..170c5b7897 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -1580,10 +1580,81 @@ void wxFreeDIB(LPBITMAPINFO lpDIBHeader) #endif // ---------------------------------------------------------------------------- -// other helper functions +// global helper functions implemented here // ---------------------------------------------------------------------------- -extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h) +// helper of wxBitmapToHICON/HCURSOR +static +HICON wxBitmapToIconOrCursor(const wxBitmap& bmp, + bool iconWanted, + int hotSpotX, + int hotSpotY) +{ + if ( !bmp.Ok() ) + { + // we can't create an icon/cursor form nothing + return 0; + } + + wxMask *mask = bmp.GetMask(); + if ( !mask ) + { + // we must have a mask for an icon, so even if it's probably incorrect, + // do create it (grey is the "standard" transparent colour) + mask = new wxMask(bmp, *wxLIGHT_GREY); + } + + ICONINFO iconInfo; + iconInfo.fIcon = iconWanted; // do we want an icon or a cursor? + if ( !iconWanted ) + { + iconInfo.xHotspot = hotSpotX; + iconInfo.yHotspot = hotSpotY; + } + + iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap()); + iconInfo.hbmColor = GetHbitmapOf(bmp); + + // black out the transparent area to preserve background colour, because + // Windows blits the original bitmap using SRCINVERT (XOR) after applying + // the mask to the dest rect. + { + MemoryHDC dcSrc, dcDst; + SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()), + selectBitmap(dcDst, iconInfo.hbmColor); + + if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(), + dcSrc, 0, 0, SRCAND) ) + { + wxLogLastError(_T("BitBlt")); + } + } + + HICON hicon = ::CreateIconIndirect(&iconInfo); + + if ( !bmp.GetMask() ) + { + // we created the mask, now delete it + delete mask; + } + + // delete the inverted mask bitmap we created as well + ::DeleteObject(iconInfo.hbmMask); + + return hicon; +} + +HICON wxBitmapToHICON(const wxBitmap& bmp) +{ + return wxBitmapToIconOrCursor(bmp, TRUE, 0, 0); +} + +HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY) +{ + return (HCURSOR)wxBitmapToIconOrCursor(bmp, FALSE, hotSpotX, hotSpotY); +} + +HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h) { #ifndef __WXMICROWIN__ wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") ); -- 2.45.2