Click here to Skip to main content
Click here to Skip to main content
Go to top

A replacement for MaskBlt

, 1 May 2002
Rate this:
Please Sign up or sign in to vote.
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. Smile | :)

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

Share

About the Author

James Lee

United States United States
No Biography provided

Comments and Discussions

 
GeneralCreating a Mask SUCCESSFULLY! Pinmembereatmyfeat20-Jan-04 22:44 
GeneralRe: Creating a Mask SUCCESSFULLY! Pinmemberted_yu6-Apr-04 15:14 
GeneralRe: Creating a Mask SUCCESSFULLY! Pinmembereatmyfeat6-Apr-04 18:29 
GeneralRe: Creating a Mask SUCCESSFULLY! PinmemberWiseAlex19-May-04 1:56 
QuestionRe: Creating a Mask SUCCESSFULLY! Pinmemberchobanu_p_m4-Aug-09 20:22 
GeneralDoesn't quite work. Pinmembereatmyfeat19-Jan-04 1:14 
Generalsome missing. PinmemberLee JaeKil23-Apr-01 17:01 
GeneralA more efficient solution PinmemberFeng Yuan (www.fengyuan.com)24-Feb-01 22:14 
GeneralHow about this? PinmemberJinhyuck Jung28-Jul-02 17:04 
GeneralRe: How about this? (Excellent) PinmemberAORD6-Dec-03 23:21 
GeneralRe: How about this? Pinmembereatmyfeat6-Apr-04 18:32 
GeneralOH PLEASE PinsussAnonymous6-Jan-03 6:03 
GeneralRe: OH PLEASE PinmemberTeashirt25-Dec-05 5:50 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 2 May 2002
Article Copyright 2001 by James Lee
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid