Robert N.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@36926 
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
     virtual WXDWORD MSWGetStyle(long style, WXDWORD *exstyle) const;
 
     // ctor/dtor helpers
     virtual WXDWORD MSWGetStyle(long style, WXDWORD *exstyle) const;
 
     // ctor/dtor helpers
-    void Init() { m_isIcon = true; m_image = NULL; }
+    void Init() { m_isIcon = true; m_image = NULL; m_currentHandle = 0; }
     void Free();
 
     // true if icon/bitmap is valid
     void Free();
 
     // true if icon/bitmap is valid
     // we can have either an icon or a bitmap
     bool m_isIcon;
     wxGDIImage *m_image;
     // we can have either an icon or a bitmap
     bool m_isIcon;
     wxGDIImage *m_image;
+    // handle used in last call to STM_SETIMAGE
+    HGDIOBJ m_currentHandle;
 
 private:
     DECLARE_DYNAMIC_CLASS(wxStaticBitmap)
 
 private:
     DECLARE_DYNAMIC_CLASS(wxStaticBitmap)
 
     LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ;
     ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) |
                      ( m_isIcon ? SS_ICON : SS_BITMAP ) );
     LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ;
     ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) |
                      ( m_isIcon ? SS_ICON : SS_BITMAP ) );
-    ::SendMessage(GetHwnd(), STM_SETIMAGE,
+    HGDIOBJ oldHandle = (HGDIOBJ)::SendMessage(GetHwnd(), STM_SETIMAGE,
                   m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
                   m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
+    // detect if this is still the handle we passed before or
+    // if the static-control made a copy of the bitmap!
+    if (m_currentHandle != 0 && oldHandle != m_currentHandle)
+    {
+        // the static control made a copy and we are responsible for deleting it
+        DeleteObject(oldHandle);      
+    }
+    m_currentHandle = (HGDIOBJ)handle;                         
 #endif // Win32
 
     if ( ImageIsOk() )
 #endif // Win32
 
     if ( ImageIsOk() )