Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A replacement for MaskBlt

0.00/5 (No votes)
1 May 2002 1  
Now you can use MaskBlt on Windows 9X

MaskTest Image

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.

  1. creates a HDC for mask bitmap.

  2. create a HDC/bitmap to hold the masked background bitmap.

  3. 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.

  4. 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.

  5. Merge the two bitmaps created from steps 3 and 4 using SRCPAINT, and copy this bitmap to the ultimate destination HDC( hdcDest ).
  6. cleanup.

#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;
}

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here