参考文章: http://www.codeproject.com/Articles/931/A-replacement-for-MaskBlt
代码下载:http://download.csdn.net/detail/chenyujing1234/5218726
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
for ROP3 from ROP4. AndBACK_ROP3(dwRop)
to mask bitmap.DSTERASE
-
create a HDC/bitmap to hold the masked foreground bitmap.
In this step, I use
for ROP3 from ROP4. This macro extracts the source ROP3 from ROP4. ThenFORE_ROP3(dwRop)
to the mask bitmap.SRCAND
- Merge the two bitmaps created from steps 3 and 4 using
, and copy this bitmap to the ultimate destination HDC( hdcDest ).SRCPAINT
- 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. :)
二、BitBlt介绍
This function transfers pixels from a specified source rectangle to a specified destination rectangle, altering the pixels according to the selected raster operation (ROP) code.
BOOL BitBlt(
HDC hdcDest,
int nXDest,
int nYDest,
int nWidth,
int nHeight,
HDC hdcSrc,
int nXSrc,
int nYSrc,
DWORD dwRop
);
Parameters
- hdcDest
- [in] Handle to the destination device context.
- nXDest
- [in] Specifies the logical x-coordinate of the upper-left corner of the destination rectangle.
- nYDest
- [in] Specifies the logical y-coordinate of the upper-left corner of the destination rectangle.
- nWidth
- [in] Specifies the logical width of the source and destination rectangles.
- nHeight
- [in] Specifies the logical height of the source and the destination rectangles.
- hdcSrc
- [in] Handle to the source device context.
- nXSrc
- [in] Specifies the logical x-coordinate of the upper-left corner of the source rectangle.
- nYSrc
- [in] Specifies the logical y-coordinate of the upper-left corner of the source rectangle.
- dwRop
-
[in] Specifies a raster-operation code.
These codes define how the color data for the source rectangle is to be combined with the color data for the destination rectangle to achieve the final color.
The following list shows some common raster operation codes.
Value Description BLACKNESS Fills the destination rectangle using the color associated with index 0 in the physical palette.
This color is black for the default physical palette.
DSTINVERT Inverts the destination rectangle. MERGECOPY Merges the colors of the source rectangle with the specified pattern by using the Boolean AND operator. MERGEPAINT Merges the colors of the inverted source rectangle with the colors of the destination rectangle by using the Boolean OR operator. NOTSRCCOPY Copies the inverted source rectangle to the destination. NOTSRCERASE Combines the colors of the source and destination rectangles by using the Boolean OR operator and then inverts the resultant color. PATCOPY Copies the specified pattern into the destination bitmap. PATINVERT 目的矩形的颜色中与指定样式的颜色进行XOR操作 PATPAINT Combines the colors of the pattern with the colors of the inverted source rectangle by using the Boolean OR operator.
The result of this operation is combined with the colors of the destination rectangle by using the Boolean OR operator.
SRCAND Combines the colors of the source and destination rectangles by using the Boolean AND operator. SRCCOPY 把源矩形直接复制到目的矩形上 SRCERASE 源矩形的颜色和目的矩形颜色反转后的颜色进行AND操作 SRCINVERT 源矩形的颜色和目的矩形的颜色进行XOR操作 SRCPAINT 源矩形的颜色和目的的颜色采用XOR方式组合 WHITENESS 用物理光栅中的index 1中的数据填充目的矩形。
This color is white for the default physical palette.
2、MaskBlt
将源的颜色数据与目的位图采用指定的mask操作和光栅操作进行组合.
ParametersBOOL MaskBlt( HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc, HBITMAP hbmMask, int xMask, int yMask, DWORD dwRop );
- hdcDest
- [in] Handle to the destination device context.
- nXDest
- [in] Specifies the logical x-coordinate of the upper left corner of the destination rectangle.
- nYDest
- [in] Specifies the logical y-coordinate of the upper left corner of the destination rectangle.
- nWidth
- [in] Specifies the width, in logical units, of the destination rectangle and source bitmap.
- nHeight
- [in] Specifies the height, in logical units, of the destination rectangle and source bitmap.
- hdcSrc
- [in] Handle to the device context from which the bitmap is to be copied. It must be zero if the dwRop parameter specifies a raster operation that does not include a source.
- nXSrc
- [in] Specifies the logical x-coordinate of the upper left corner of the source bitmap.
- nYSrc
- [in] Specifies the logical y-coordinate of the upper left corner of the source bitmap.
- hbmMask
- [in] Handle to the monochrome mask bitmap combined with the color bitmap in the source device context.
- xMask
- [in] Specifies the horizontal pixel offset for the mask bitmap specified by the hbmMask parameter.
- yMask
- [in] Specifies the vertical pixel offset for the mask bitmap specified by the hbmMask parameter.
- dwRop
-
[in] Specifies both foreground and background ternary raster operation codes that the function uses to control the combination of source and destination data.
背景的光栅操作码是存储在dwRop中的确高字节中. 前景操作码是存储在dwRop中的高字中的低字节中; the low-order word of this value is ignored, and should be zero.
The macro MAKEROP4 creates such combinations of foreground and background raster operation codes.
For a discussion of foreground and background in the context of this function, see the following Remarks section.
For a list of common raster operation codes, see the BitBlt function.
Windows CE 1.0 and 1.01 support only the SRCCOPY and SRCINVERT raster operations.
Remarks
A value of 1 in the mask specified by hbmMask indicates that the foreground raster operation code specified by dwRop should be applied at that location.
A value of 0 in the mask indicates that the background raster operation code specified by dwRop should be applied at that location.
If the raster operations require a source, the mask rectangle must cover the source rectangle. If it does not, the function will fail.
If the raster operations do not require a source, the mask rectangle must cover the destination rectangle. If it does not, the function will fail.
If a rotation or shear transformation is in effect for the source device context when this function is called, an error occurs. However, other types of transformation are allowed.
If the color formats of the source, pattern, and destination bitmaps differ, this function converts the pattern or source format, or both, to match the destination format.
If the mask bitmap is not a monochrome bitmap, an error occurs.
When an enhanced metafile is being recorded, an error occurs (and the function returns FALSE) if the source device context identifies an enhanced-metafile device context.
Not all devices support the MaskBlt function. An application should call the GetDeviceCaps function to determine whether a device supports this function.
If no mask bitmap is supplied, this function behaves exactly like BitBlt, using the foreground raster operation code.
3、MAKEROP4
This macro creates a quaternary raster operation (ROP) code for use by the MaskBlt function.
The macro takes two ternary ROP codes as input, one for the foreground and one for the background, and packs their Boolean operation indexes into the high-order word of a 32-bit value. MAKEROP4 ignores the low-order word. For more information, see Ternary Raster Operations.
ParametersDWORD MAKEROP4( DWORD fore, DWORD back );
- fore
- Specifies a foreground ternary raster operation code.
- back
- Specifies a background ternary raster operation code
Return Value
The return value is a DWORD quaternary raster operation code for use with the MaskBlt function.
Remarks
The MAKEROP4 macro is defined as follows.
#define MAKEROP4(fore,back) (DWORD)((((back) << 8) & 0xFF000000) | (fore))
三、MaskBlt在实际场景中有应用
VS2008中的解决方案文件查看中就是使用了MaskBlt,下面是正常的图:
下面是没有调用MaskBlt时的效果: