A replacement for MaskBlt


参考文章: 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.

  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. :)


二、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
);

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操作和光栅操作进行组合.

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

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.

DWORD MAKEROP4(
  DWORD fore, 
  DWORD back
);

fore

Specifies a foreground ternary raster operation code.

back

Specifies a background ternary raster operation code

The return value is a DWORD quaternary raster operation code for use with the MaskBlt function.

The MAKEROP4 macro is defined as follows.

#define MAKEROP4(fore,back) 
          (DWORD)((((back) << 8) & 0xFF000000) | (fore)) 


三、MaskBlt在实际场景中有应用

VS2008中的解决方案文件查看中就是使用了MaskBlt,下面是正常的图:


下面是没有调用MaskBlt时的效果:











  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值