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:
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;

View File

@@ -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;
}

View File

@@ -348,9 +348,8 @@ 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."),
@@ -359,15 +358,21 @@ bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap,
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));

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") );
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") );

View File

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