Introduction
When I developed a bitmap oriented CAD system, I was stuck because some APIs are not
supported by windows 9X, such as MaskBlt
. I can use other methods to
get around this, but I want to implement this API so that I use MaskBlt
on any Windows 9X platform.
So here is what I made.
It is almost same as drawing transparent bitmap, but I added other
raster operations so that this function works as same as MaskBlt
.
The sample shows that the first line's results come from MaskBlt in WIN32 API.
The next line's results come from MyMaskBlt.
The third one is mask bitmap for the test application.
The following steps describe how the code works.
- creates a HDC for mask bitmap.
- create a HDC/bitmap to hold the masked background bitmap.
- copy the bitmaps to the masked background bitmap using 3 BitBlt.
In this step, I use BACK_ROP3(dwRop)
for ROP3 from ROP4. And DSTERASE
to mask bitmap.
- create a HDC/bitmap to hold the masked foreground bitmap.
In this step, I use FORE_ROP3(dwRop)
for ROP3 from ROP4. This macro extracts the source ROP3 from ROP4. Then SRCAND
to the mask bitmap.
- Merge the two bitmaps created from steps 3 and 4 using
SRCPAINT
,
and copy this bitmap to the ultimate destination HDC( hdcDest ).
- cleanup.
#define FORE_ROP3(ROP4) (0x00FFFFFF&(ROP4))
#define BACK_ROP3(ROP4) (ROP3FromIndex(SwapROP3_SrcDst(BYTE((ROP4)>>24))))
#define DSTCOPY 0x00AA0029
#define DSTERASE 0x00220326
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));
HDC hDCMask = ::CreateCompatibleDC(hdcDest);
HBITMAP hOldMaskBitmap = (HBITMAP)::SelectObject(hDCMask, hbmMask);
ASSERT ( hOldMaskBitmap );
HDC hDC1 = ::CreateCompatibleDC(hdcDest);
ASSERT ( hDC1 );
HBITMAP hBitmap2 = ::CreateCompatibleBitmap(hdcDest, nWidth, nHeight);
HBITMAP hOldBitmap2 = (HBITMAP)::SelectObject(hDC1, hBitmap2);
ASSERT ( hOldBitmap2 );
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);
HDC hDC2 = ::CreateCompatibleDC(hdcDest);
ASSERT ( hDC2 );
HBITMAP hBitmap3 = ::CreateCompatibleBitmap(hdcDest, nWidth, nHeight);
HBITMAP hOldBitmap3 = (HBITMAP)::SelectObject(hDC2, hBitmap3);
ASSERT ( hOldBitmap3 );
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);
::BitBlt(hDC1, 0, 0, nWidth, nHeight, hDC2, 0, 0, SRCPAINT);
::BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hDC1, 0, 0, SRCCOPY);
::SelectObject(hDCMask, hOldMaskBitmap);
::SelectObject(hDC1, hOldBitmap2);
::SelectObject(hDC2, hOldBitmap3);
DeleteObject(hBitmap2);
DeleteObject(hBitmap3);
DeleteDC(hDC1);
DeleteDC(hDC2);
DeleteDC(hDCMask);
return TRUE;
}
After submitting the code, I didn't realized that there is a bug. Lonnie Cumberland let me know the bug and I fixed the bug.
Actually it was not compatible to MaskBlt before. :)
Some of the images used in the sample comes from wxwindows project.
Thanks to Lonnie.