From 99983325d63dba3eb17d04f7497399da929a59a7 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Sat, 13 Mar 2021 23:13:18 +0100 Subject: [PATCH] Use proper data structure describing image of owner-drawn wxButton (wxMSW) When wxButton is switched to owner-drawn mode we need to store its image data in the dedicated wxODButtonImageData structure. We shouldn't use in this case wxXPButtonImageData structure because it is dedicated for native buttons and every time it's updated a BCM_SETIMAGELIST message is sent to the button what is unnecessary action. --- src/msw/anybutton.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/msw/anybutton.cpp b/src/msw/anybutton.cpp index b7a018041c..ae24df5993 100644 --- a/src/msw/anybutton.cpp +++ b/src/msw/anybutton.cpp @@ -671,12 +671,12 @@ void wxAnyButton::DoSetBitmap(const wxBitmap& bitmap, State which) "Must set normal bitmap with the new size first" ); #if wxUSE_UXTHEME - if ( ShowsLabel() && wxUxThemeIsActive() ) + // We can't change the size of the images stored in wxImageList + // in wxXPButtonImageData::m_iml so force recreating it below but + // keep the current data to copy its values into the new one. + oldData = dynamic_cast(m_imageData); + if ( oldData ) { - // We can't change the size of the images stored in wxImageList - // in wxXPButtonImageData::m_iml so force recreating it below but - // keep the current data to copy its values into the new one. - oldData = static_cast(m_imageData); m_imageData = NULL; } #endif // wxUSE_UXTHEME @@ -1162,6 +1162,24 @@ void wxAnyButton::MakeOwnerDrawn() { if ( !IsOwnerDrawn() ) { + // We need to use owner-drawn specific data structure so we have + // to create it and copy the data from native data structure, + // if necessary. + if ( m_imageData && dynamic_cast(m_imageData) == NULL ) + { + wxODButtonImageData* newData = new wxODButtonImageData(this, m_imageData->GetBitmap(State_Normal)); + for ( int n = 0; n < State_Max; n++ ) + { + State st = static_cast(n); + newData->SetBitmap(m_imageData->GetBitmap(st), st); + } + newData->SetBitmapPosition(m_imageData->GetBitmapPosition()); + wxSize margs = m_imageData->GetBitmapMargins(); + newData->SetBitmapMargins(margs.x, margs.y); + + delete m_imageData; + m_imageData = newData; + } // make it so wxMSWWinStyleUpdater(GetHwnd()).TurnOff(BS_TYPEMASK).TurnOn(BS_OWNERDRAW); }