+#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
+ // it may be either HICON or HCURSOR
+ HICON hicon = (HICON)icon.GetHandle();
+
+ ICONINFO iconInfo;
+ if ( !::GetIconInfo(hicon, &iconInfo) )
+ {
+ wxLogLastError(wxT("GetIconInfo"));
+
+ return false;
+ }
+
+ wxBitmapRefData *refData = new wxBitmapRefData;
+ m_refData = refData;
+
+ int w = icon.GetWidth(),
+ h = icon.GetHeight();
+
+ refData->m_width = w;
+ refData->m_height = h;
+ refData->m_depth = wxDisplayDepth();
+
+ refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
+
+ switch ( transp )
+ {
+ default:
+ wxFAIL_MSG( wxT("unknown wxBitmapTransparency value") );
+
+ case wxBitmapTransparency_None:
+ // nothing to do, refData->m_hasAlpha is false by default
+ break;
+
+ case wxBitmapTransparency_Auto:
+#if wxUSE_WXDIB
+ // If the icon is 32 bits per pixel then it may have alpha channel
+ // data, although there are some icons that are 32 bpp but have no
+ // alpha... So convert to a DIB and manually check the 4th byte for
+ // each pixel.
+ {
+ BITMAP bm;
+ if ( ::GetObject(iconInfo.hbmColor, sizeof(bm), &bm) &&
+ (bm.bmBitsPixel == 32) )
+ {
+ wxDIB dib(iconInfo.hbmColor);
+ if (dib.IsOk())
+ {
+ unsigned char* const pixels = dib.GetData();
+ int idx;
+ for ( idx = 0; idx < w*h*4; idx += 4 )
+ {
+ if (pixels[idx+3] != 0)
+ {
+ // If there is an alpha byte that is non-zero
+ // then set the alpha flag and stop checking
+ refData->m_hasAlpha = true;
+ break;
+ }
+ }
+
+ if ( refData->m_hasAlpha )
+ {
+ // If we do have alpha, ensure we use premultiplied
+ // data for our pixels as this is what the bitmaps
+ // created in other ways do and this is necessary
+ // for e.g. AlphaBlend() to work with this bitmap.
+ for ( idx = 0; idx < w*h*4; idx += 4 )
+ {
+ const unsigned char a = pixels[idx+3];
+
+ pixels[idx] = ((pixels[idx] *a) + 127)/255;
+ pixels[idx+1] = ((pixels[idx+1]*a) + 127)/255;
+ pixels[idx+2] = ((pixels[idx+2]*a) + 127)/255;
+ }
+
+ ::DeleteObject(refData->m_hBitmap);
+ refData->m_hBitmap = dib.Detach();
+ }
+ }
+ }
+ }
+ break;
+#endif // wxUSE_WXDIB
+
+ case wxBitmapTransparency_Always:
+ refData->m_hasAlpha = true;
+ break;
+ }
+
+ if ( !refData->m_hasAlpha )
+ {
+ // the mask returned by GetIconInfo() is inverted compared to the usual
+ // wxWin convention
+ refData->SetMask(wxInvertMask(iconInfo.hbmMask, w, h));
+ }