#define FORE_ROP3(ROP4) (0x00FFFFFF&(ROP4))
#define BACK_ROP3(ROP4) (ROP3FromIndex(SwapROP3_SrcDst(BYTE((ROP4)>>24))))
#define DSTCOPY 0x00AA0029
#define DSTERASE 0x00220326 // dest = dest & (~src) : DSna
BOOL WINAPI MyMaskBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight,
HDC hdcSrc, int nXSrc, int nYSrc,
HBITMAP hbmMask, int xMask, int yMask,
DWORD dwRop
)
{
if ( hbmMask == NULL )
return BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc,
nXSrc, nYSrc, FORE_ROP3(dwRop));
// 1. make mask bitmap's dc
HDC hDCMask = ::CreateCompatibleDC(hdcDest);
HBITMAP hOldMaskBitmap = (HBITMAP)::SelectObject(hDCMask, hbmMask);
ASSERT ( hOldMaskBitmap );
// 2. make masked Background bitmap
// 2.1 make bitmap
HDC hDC1 = ::CreateCompatibleDC(hdcDest);
ASSERT ( hDC1 );
HBITMAP hBitmap2 = ::CreateCompatibleBitmap(hdcDest, nWidth, nHeight);
HBITMAP hOldBitmap2 = (HBITMAP)::SelectObject(hDC1, hBitmap2);
ASSERT ( hOldBitmap2 );
// 2.2 draw dest bitmap and mask
DWORD dwRop3 = BACK_ROP3(dwRop);
::BitBlt(hDC1, 0, 0, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, SRCCOPY);
::BitBlt(hDC1, 0, 0, nWidth, nHeight, hdcDest, nXDest, nYDest, dwRop3);
::BitBlt(hDC1, 0, 0, nWidth, nHeight, hDCMask, xMask, yMask, DSTERASE);
// 3. make masked Foreground bitmap
// 3.1 make bitmap
HDC hDC2 = ::CreateCompatibleDC(hdcDest);
ASSERT ( hDC2 );
HBITMAP hBitmap3 = ::CreateCompatibleBitmap(hdcDest, nWidth, nHeight);
HBITMAP hOldBitmap3 = (HBITMAP)::SelectObject(hDC2, hBitmap3);
ASSERT ( hOldBitmap3 );
// 3.2 draw src bitmap and mask
dwRop3 = FORE_ROP3(dwRop);
::BitBlt(hDC2, 0, 0, nWidth, nHeight, hdcDest, nXDest, nYDest, SRCCOPY);
::BitBlt(hDC2, 0, 0, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop3);
::BitBlt(hDC2, 0, 0, nWidth, nHeight, hDCMask, xMask, yMask, SRCAND);
// 4. combine two bitmap and copy it to hdcDest
::BitBlt(hDC1, 0, 0, nWidth, nHeight, hDC2, 0, 0, SRCPAINT);
::BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hDC1, 0, 0, SRCCOPY);
// 5. restore all object
::SelectObject(hDCMask, hOldMaskBitmap);
::SelectObject(hDC1, hOldBitmap2);
::SelectObject(hDC2, hOldBitmap3);
// 6. delete all temp object
DeleteObject(hBitmap2);
DeleteObject(hBitmap3);
DeleteDC(hDC1);
DeleteDC(hDC2);
DeleteDC(hDCMask);
return TRUE;
}