Fix using MSW wxStaticBitmap with custom wxBitmapBundles

wxBitmapBundle-related changes introduced a bug when wxStaticBitmap
could be using an invalid handle (which manifested itself in not showing
any image on screen) if it was using wxBitmapBundle whose GetBitmap()
returned a wxBitmap that wasn't cached inside wxBitmapBundle itself.

In this case, this wxBitmap wasn't referenced anywhere after being
associated with wxStaticBitmap and so was destroyed, invalidating the
HBITMAP used by the native static control.

Fix this by keeping a copy of the bitmap in wxStaticBitmap itself. This
is not the most efficient, but is the simplest, solution.
This commit is contained in:
Vadim Zeitlin
2021-12-12 23:39:48 +00:00
parent e211a451fe
commit b9a1931394
2 changed files with 8 additions and 2 deletions

View File

@@ -122,8 +122,10 @@ private:
// we can have either an icon or a bitmap: if m_icon is valid, it is used,
// otherwise m_bitmapBundle defined in the base class is used if it is valid
// otherwise we use m_bitmap which is itself obtained from m_bitmapBundle
// defined in the base class
wxIcon m_icon;
wxBitmap m_bitmap;
// handle used in last call to STM_SETIMAGE
WXHANDLE m_currentHandle;

View File

@@ -160,6 +160,8 @@ void wxStaticBitmap::Init()
void wxStaticBitmap::Free()
{
m_bitmap.UnRef();
MSWReplaceImageHandle(0);
if ( m_ownsCurrentHandle )
@@ -266,7 +268,9 @@ void wxStaticBitmap::DoUpdateImage(const wxSize& sizeOld, bool wasIcon)
else
#endif // wxUSE_WXDIB
{
// Just use the HBITMAP as is.
// Just use the HBITMAP as is, but also make a copy of the bitmap
// to ensure that HBITMAP remains valid for as long as we need it
m_bitmap = bitmap;
m_currentHandle = bitmap.GetHandle();
}
}