hmm....that code really should use the Resource Acquisition Is Initialization (RAII) idiom.
i.e. this code:
---
pbmpOldDst = dcDst.SelectObject( &m_bmpBitmap );
pbmpOldSrc = dcSrc.SelectObject( sprFrom.GetBitmap() );
bSuccess = dcDst.BitBlt( 0, 0, nWidth, nHeight, &dcSrc, nX, nY, SRCCOPY );
dcDst.SelectObject( pbmpOldDst );
dcSrc.SelectObject( pbmpOldSrc );
---
is brittle and error prone. What if the bits in the middle threw an exception? Then the bitmaps wouldn't get reselected properly? Likewise, what if the code was modified and someone forgot to reselect the bitmaps properly? It's much safer and cleaner to use the RAII idiom. All you have to do is define a simple helper class template:
template<typename T>
class object_selecter
{
CDC& cdc_;
T* obj_;
public:
object_selecter(CDC& cdc, T* obj) : cdc_(cdc), obj_(obj) {}
~object_selecter() { cdc_.SelectObject(obj_); }
};
and then you can rewrite the above code to:
const object_selecter<CBitmap> dst_releaser( dcDst,dcDst.SelectObject( &m_bmpBitmap ) );
const object_selecter<CBitmap> src_releaser( dcSrc, dcSrc.SelectObject( sprFrom.GetBitmap() ) );
bSuccess = dcDst.BitBlt( 0, 0, nWidth, nHeight, &dcSrc, nX, nY, SRCCOPY );
//the bitmaps will automatically be reselected here
This way of doing it is much cleaner, more elegant, and safer. You can make it even more generic than that.
C++ is a great language, but to get the full benefit out of it, you really do have to learn to use it properly. People whine about how it requires explicit memory management, but then neglect to use powerful techniques such as RAII to help manage their memory.
-Sirp.