+// ----------------------------------------------------------------------------
+// sub bitmap extraction
+// ----------------------------------------------------------------------------
+wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect ) const
+{
+ MemoryHDC dcSrc;
+ SelectInHDC selectSrc(dcSrc, GetHbitmap());
+ return GetSubBitmapOfHDC( rect, (WXHDC)dcSrc );
+}
+
+wxBitmap wxBitmap::GetSubBitmapOfHDC( const wxRect& rect, WXHDC hdc ) const
+{
+ wxCHECK_MSG( IsOk() &&
+ (rect.x >= 0) && (rect.y >= 0) &&
+ (rect.x+rect.width <= GetWidth()) &&
+ (rect.y+rect.height <= GetHeight()),
+ wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
+
+ wxBitmap ret( rect.width, rect.height, GetDepth() );
+ wxASSERT_MSG( ret.IsOk(), wxT("GetSubBitmap error") );
+
+#ifndef __WXMICROWIN__
+ // handle alpha channel, if any
+ if (HasAlpha())
+ ret.UseAlpha();
+
+ // copy bitmap data
+ MemoryHDC dcSrc,
+ dcDst;
+
+ {
+ SelectInHDC selectDst(dcDst, GetHbitmapOf(ret));
+
+ if ( !selectDst )
+ {
+ wxLogLastError(wxT("SelectObject(destBitmap)"));
+ }
+
+ if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
+ (HDC)hdc, rect.x, rect.y, SRCCOPY) )
+ {
+ wxLogLastError(wxT("BitBlt"));
+ }
+ }
+
+ // copy mask if there is one
+ if ( GetMask() )
+ {
+ HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
+
+ SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()),
+ selectDst(dcDst, hbmpMask);
+
+ if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
+ dcSrc, rect.x, rect.y, SRCCOPY) )
+ {
+ wxLogLastError(wxT("BitBlt"));
+ }
+
+ wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
+ ret.SetMask(mask);