Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / MFC
Article

A replacement for MaskBlt

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
1 May 2002 131K   3.5K   49   13
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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCreating a Mask SUCCESSFULLY! Pin
eatmyfeat20-Jan-04 22:44
eatmyfeat20-Jan-04 22:44 
GeneralRe: Creating a Mask SUCCESSFULLY! Pin
ted_yu6-Apr-04 15:14
ted_yu6-Apr-04 15:14 
GeneralRe: Creating a Mask SUCCESSFULLY! Pin
eatmyfeat6-Apr-04 18:29
eatmyfeat6-Apr-04 18:29 
GeneralRe: Creating a Mask SUCCESSFULLY! Pin
WiseAlex19-May-04 1:56
WiseAlex19-May-04 1:56 
QuestionRe: Creating a Mask SUCCESSFULLY! Pin
chobanu_p_m4-Aug-09 20:22
chobanu_p_m4-Aug-09 20:22 
GeneralDoesn't quite work. Pin
eatmyfeat19-Jan-04 1:14
eatmyfeat19-Jan-04 1:14 
Generalsome missing. Pin
23-Apr-01 17:01
suss23-Apr-01 17:01 
GeneralA more efficient solution Pin
24-Feb-01 22:14
suss24-Feb-01 22:14 
GeneralHow about this? Pin
Jinhyuck Jung28-Jul-02 17:04
Jinhyuck Jung28-Jul-02 17:04 
GeneralRe: How about this? (Excellent) Pin
AORD6-Dec-03 23:21
AORD6-Dec-03 23:21 
Smashing my head all day ( Cry | :(( Cry | :(( Cry | :(( Cry | :(( ) till I found this hidden code.

This is the best, from hundreds of line down to 7, your a GOD. Big Grin | :-D Big Grin | :-D Big Grin | :-D Big Grin | :-D
GeneralRe: How about this? Pin
eatmyfeat6-Apr-04 18:32
eatmyfeat6-Apr-04 18:32 
GeneralOH PLEASE Pin
Anonymous6-Jan-03 6:03
Anonymous6-Jan-03 6:03 
GeneralRe: OH PLEASE Pin
Teashirt25-Dec-05 5:50
Teashirt25-Dec-05 5:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.