diff --git a/include/wx/msw/bitmap.h b/include/wx/msw/bitmap.h index 9b76b55036..555d7f801c 100644 --- a/include/wx/msw/bitmap.h +++ b/include/wx/msw/bitmap.h @@ -201,6 +201,8 @@ public: public: void SetHBITMAP(WXHBITMAP bmp) { SetHandle((WXHANDLE)bmp); } WXHBITMAP GetHBITMAP() const { return (WXHBITMAP)GetHandle(); } + bool InitFromHBITMAP(WXHBITMAP bmp, int width, int height, int depth); + void ResetHBITMAP() { InitFromHBITMAP(NULL, 0, 0, 0); } void SetSelectedInto(wxDC *dc); wxDC *GetSelectedInto() const; diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index e68e85bfac..02611aade2 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -1290,6 +1290,34 @@ void wxBitmap::SetMask(wxMask *mask) GetBitmapData()->SetMask(mask); } +bool wxBitmap::InitFromHBITMAP(WXHBITMAP bmp, int width, int height, int depth) +{ +#if wxDEBUG_LEVEL >= 2 + if ( bmp != NULL ) + { + BITMAP bm; + if ( ::GetObject(bmp, sizeof(bm), &bm) == sizeof(bm) ) + { + wxASSERT_MSG(bm.bmWidth == width && bm.bmHeight == height && bm.bmBitsPixel == depth, + wxS("Inconsistent bitmap parameters")); + } + else + { + wxFAIL_MSG(wxS("Cannot retrieve parameters of the bitmap")); + } + } +#endif // wxDEBUG_LEVEL >= 2 + + AllocExclusive(); + + GetBitmapData()->m_handle = (WXHANDLE)bmp; + GetBitmapData()->m_width = width; + GetBitmapData()->m_height = height; + GetBitmapData()->m_depth = depth; + + return IsOk(); +} + // ---------------------------------------------------------------------------- // raw bitmap access support // ---------------------------------------------------------------------------- @@ -1620,9 +1648,7 @@ wxBitmap wxMask::GetBitmap() const // Create and return a new wxBitmap. wxBitmap bmp; - bmp.SetHBITMAP((WXHBITMAP)hNewBitmap); - bmp.SetSize(bm.bmWidth, bm.bmHeight); - bmp.SetDepth(bm.bmBitsPixel); + bmp.InitFromHBITMAP((WXHBITMAP)hNewBitmap, bm.bmWidth, bm.bmHeight, bm.bmBitsPixel); return bmp; } diff --git a/src/msw/gdiimage.cpp b/src/msw/gdiimage.cpp index a48cb4775d..c4ec760a6b 100644 --- a/src/msw/gdiimage.cpp +++ b/src/msw/gdiimage.cpp @@ -348,26 +348,31 @@ bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap, int WXUNUSED(desiredHeight)) { // TODO: load colourmap. - bitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), name.t_str())); - - if ( !bitmap->IsOk() ) + HBITMAP hbmp = ::LoadBitmap(wxGetInstance(), name.t_str()); + if ( hbmp == NULL ) { // it's probably not found wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."), - name.c_str()); + name.c_str()); return false; } + int w, h, d; BITMAP bm; - if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(BITMAP), (LPSTR) &bm) ) + if (::GetObject(hbmp, sizeof(BITMAP), &bm)) + { + w = bm.bmWidth; + h = bm.bmHeight; + d = bm.bmBitsPixel; + } + else { wxLogLastError(wxT("GetObject(HBITMAP)")); + w = h = d = 0; } - bitmap->SetWidth(bm.bmWidth); - bitmap->SetHeight(bm.bmHeight); - bitmap->SetDepth(bm.bmBitsPixel); + bitmap->InitFromHBITMAP((WXHBITMAP)hbmp, w, h, d); // use 0xc0c0c0 as transparent colour by default bitmap->SetMask(new wxMask(*bitmap, *wxLIGHT_GREY)); diff --git a/src/msw/ole/dataobj.cpp b/src/msw/ole/dataobj.cpp index 870fd71f4f..e052521521 100644 --- a/src/msw/ole/dataobj.cpp +++ b/src/msw/ole/dataobj.cpp @@ -1076,8 +1076,8 @@ bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf) wxCHECK_MSG( hbmp, FALSE, wxT("pasting/dropping invalid bitmap") ); const BITMAPINFOHEADER * const pbmih = &pbmi->bmiHeader; - wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount); - bitmap.SetHBITMAP((WXHBITMAP)hbmp); + wxBitmap bitmap; + bitmap.InitFromHBITMAP((WXHBITMAP)hbmp, pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount); // TODO: create wxPalette if the bitmap has any @@ -1122,10 +1122,9 @@ bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf) wxLogLastError(wxT("GetObject(HBITMAP)")); } - wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmBitsPixel); - bitmap.SetHBITMAP((WXHBITMAP)hbmp); - - if ( !bitmap.IsOk() ) { + wxBitmap bitmap; + if ( !bitmap.InitFromHBITMAP((WXHBITMAP)hbmp, bmp.bmWidth, bmp.bmHeight, bmp.bmBitsPixel) ) + { wxFAIL_MSG(wxT("pasting/dropping invalid bitmap")); return false; @@ -1214,6 +1213,7 @@ bool wxBitmapDataObject::SetData(const wxDataFormat& format, size_t size, const void *pBuf) { HBITMAP hbmp; + int w, h, d; if ( format.GetFormatId() == CF_DIB ) { // here we get BITMAPINFO struct followed by the actual bitmap bits and @@ -1229,9 +1229,9 @@ bool wxBitmapDataObject::SetData(const wxDataFormat& format, wxLogLastError(wxT("CreateDIBitmap")); } - m_bitmap.SetWidth(pbmih->biWidth); - m_bitmap.SetHeight(pbmih->biHeight); - m_bitmap.SetDepth(pbmih->biBitCount); + w = pbmih->biWidth; + h = pbmih->biHeight; + d = pbmih->biBitCount; } else // CF_BITMAP { @@ -1244,12 +1244,12 @@ bool wxBitmapDataObject::SetData(const wxDataFormat& format, wxLogLastError(wxT("GetObject(HBITMAP)")); } - m_bitmap.SetWidth(bmp.bmWidth); - m_bitmap.SetHeight(bmp.bmHeight); - m_bitmap.SetDepth(bmp.bmBitsPixel); + w = bmp.bmWidth; + h = bmp.bmHeight; + d = bmp.bmBitsPixel; } - m_bitmap.SetHBITMAP((WXHBITMAP)hbmp); + m_bitmap.InitFromHBITMAP((WXHBITMAP)hbmp, w, h, d); wxASSERT_MSG( m_bitmap.IsOk(), wxT("pasting invalid bitmap") ); diff --git a/src/msw/toolbar.cpp b/src/msw/toolbar.cpp index e43d28d1e8..2c4ae26eb1 100644 --- a/src/msw/toolbar.cpp +++ b/src/msw/toolbar.cpp @@ -953,7 +953,7 @@ bool wxToolBar::Realize() { hBitmap = GetHbitmapOf(bitmap); // don't delete this HBITMAP! - bitmap.SetHBITMAP(0); + bitmap.ResetHBITMAP(); } if ( remapValue == Remap_Buttons )