Set HBITMAP and its parameters in one call

To avoid separate calls to SetWidth/Height/Size/Depth functions after calling SetHBITMAP() use newly implemented InitFromHBITMAP() function which allows to set HBITMAP together with its parameters in one call.
This commit is contained in:
Artur Wieczorek
2018-09-11 21:44:56 +02:00
parent 12c44f1c79
commit 97f73acddb
5 changed files with 58 additions and 25 deletions

View File

@@ -201,6 +201,8 @@ public:
public: public:
void SetHBITMAP(WXHBITMAP bmp) { SetHandle((WXHANDLE)bmp); } void SetHBITMAP(WXHBITMAP bmp) { SetHandle((WXHANDLE)bmp); }
WXHBITMAP GetHBITMAP() const { return (WXHBITMAP)GetHandle(); } 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); void SetSelectedInto(wxDC *dc);
wxDC *GetSelectedInto() const; wxDC *GetSelectedInto() const;

View File

@@ -1290,6 +1290,34 @@ void wxBitmap::SetMask(wxMask *mask)
GetBitmapData()->SetMask(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 // raw bitmap access support
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -1620,9 +1648,7 @@ wxBitmap wxMask::GetBitmap() const
// Create and return a new wxBitmap. // Create and return a new wxBitmap.
wxBitmap bmp; wxBitmap bmp;
bmp.SetHBITMAP((WXHBITMAP)hNewBitmap); bmp.InitFromHBITMAP((WXHBITMAP)hNewBitmap, bm.bmWidth, bm.bmHeight, bm.bmBitsPixel);
bmp.SetSize(bm.bmWidth, bm.bmHeight);
bmp.SetDepth(bm.bmBitsPixel);
return bmp; return bmp;
} }

View File

@@ -348,26 +348,31 @@ bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap,
int WXUNUSED(desiredHeight)) int WXUNUSED(desiredHeight))
{ {
// TODO: load colourmap. // TODO: load colourmap.
bitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), name.t_str())); HBITMAP hbmp = ::LoadBitmap(wxGetInstance(), name.t_str());
if ( hbmp == NULL )
if ( !bitmap->IsOk() )
{ {
// it's probably not found // it's probably not found
wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."), wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
name.c_str()); name.c_str());
return false; return false;
} }
int w, h, d;
BITMAP bm; 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)")); wxLogLastError(wxT("GetObject(HBITMAP)"));
w = h = d = 0;
} }
bitmap->SetWidth(bm.bmWidth); bitmap->InitFromHBITMAP((WXHBITMAP)hbmp, w, h, d);
bitmap->SetHeight(bm.bmHeight);
bitmap->SetDepth(bm.bmBitsPixel);
// use 0xc0c0c0 as transparent colour by default // use 0xc0c0c0 as transparent colour by default
bitmap->SetMask(new wxMask(*bitmap, *wxLIGHT_GREY)); bitmap->SetMask(new wxMask(*bitmap, *wxLIGHT_GREY));

View File

@@ -1076,8 +1076,8 @@ bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf)
wxCHECK_MSG( hbmp, FALSE, wxT("pasting/dropping invalid bitmap") ); wxCHECK_MSG( hbmp, FALSE, wxT("pasting/dropping invalid bitmap") );
const BITMAPINFOHEADER * const pbmih = &pbmi->bmiHeader; const BITMAPINFOHEADER * const pbmih = &pbmi->bmiHeader;
wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount); wxBitmap bitmap;
bitmap.SetHBITMAP((WXHBITMAP)hbmp); bitmap.InitFromHBITMAP((WXHBITMAP)hbmp, pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
// TODO: create wxPalette if the bitmap has any // 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)")); wxLogLastError(wxT("GetObject(HBITMAP)"));
} }
wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmBitsPixel); wxBitmap bitmap;
bitmap.SetHBITMAP((WXHBITMAP)hbmp); if ( !bitmap.InitFromHBITMAP((WXHBITMAP)hbmp, bmp.bmWidth, bmp.bmHeight, bmp.bmBitsPixel) )
{
if ( !bitmap.IsOk() ) {
wxFAIL_MSG(wxT("pasting/dropping invalid bitmap")); wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
return false; return false;
@@ -1214,6 +1213,7 @@ bool wxBitmapDataObject::SetData(const wxDataFormat& format,
size_t size, const void *pBuf) size_t size, const void *pBuf)
{ {
HBITMAP hbmp; HBITMAP hbmp;
int w, h, d;
if ( format.GetFormatId() == CF_DIB ) if ( format.GetFormatId() == CF_DIB )
{ {
// here we get BITMAPINFO struct followed by the actual bitmap bits and // 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")); wxLogLastError(wxT("CreateDIBitmap"));
} }
m_bitmap.SetWidth(pbmih->biWidth); w = pbmih->biWidth;
m_bitmap.SetHeight(pbmih->biHeight); h = pbmih->biHeight;
m_bitmap.SetDepth(pbmih->biBitCount); d = pbmih->biBitCount;
} }
else // CF_BITMAP else // CF_BITMAP
{ {
@@ -1244,12 +1244,12 @@ bool wxBitmapDataObject::SetData(const wxDataFormat& format,
wxLogLastError(wxT("GetObject(HBITMAP)")); wxLogLastError(wxT("GetObject(HBITMAP)"));
} }
m_bitmap.SetWidth(bmp.bmWidth); w = bmp.bmWidth;
m_bitmap.SetHeight(bmp.bmHeight); h = bmp.bmHeight;
m_bitmap.SetDepth(bmp.bmBitsPixel); 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") ); wxASSERT_MSG( m_bitmap.IsOk(), wxT("pasting invalid bitmap") );

View File

@@ -953,7 +953,7 @@ bool wxToolBar::Realize()
{ {
hBitmap = GetHbitmapOf(bitmap); hBitmap = GetHbitmapOf(bitmap);
// don't delete this HBITMAP! // don't delete this HBITMAP!
bitmap.SetHBITMAP(0); bitmap.ResetHBITMAP();
} }
if ( remapValue == Remap_Buttons ) if ( remapValue == Remap_Buttons )