AlphaBlend:Wince 5以后有带支持这个函数.仔细查看这个函数的时候我们发现这个函数有两种的用法,最简单的就是设定统一的透明色,一个就是利用位图的Alpha通道,来进行叠加。这里我们主要说的就是这个利用位图的Alpha通道来透明位图:
下面的这个是来自MSDN:
This function displays bitmaps that have transparent or semitransparent pixels.
BOOL AlphaBlend(
HDC hdcDest ,
int nXOriginDest ,
int nYOriginDest ,
int nWidthDest ,
int nHeightDest ,
HDC hdcSrc ,
int nXOriginSrc ,
int nYOriginSrc ,
int nWidthSrc ,
int nHeightSrc ,
BLENDFUNCTION blendFunction
);
Parameters
-
hdcDest
- [in] Handle to the destination device context. nXOriginDest
- [in] Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle. nYOriginDest
- [in] Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle. nWidthDest
- [in] Specifies the width, in logical units, of the destination rectangle. This value cannot be negative. nHeightDest
- [in] Specifies the height, in logical units, of the destination rectangle. This value cannot be negative. hdcSrc
- [in] Handle to the source device context. nXOriginSrc
- [in] Specifies the x-coordinate, in logical units, of the upper-left corner of the source rectangle. nYOriginSrc
- [in] Specifies the y-coordinate, in logical units, of the upper-left corner of the source rectangle. nWidthSrc
- [in] Specifies the width, in logical units, of the source rectangle. This value cannot be negative. nHeightSrc
- [in] Specifies the height, in logical units, of the source rectangle. This value cannot be negative. blendFunction
- [in] A BLENDFUNCTION structure that specifies the alpha-blending function for source and destination bitmaps, a global alpha value to be applied to the entire source bitmap, and format information for the source bitmap. The source and destination blend functions are currently limited to AC_SRC_OVER.
Return Values
If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE.
To get extended error information, call GetLastError. GetLastError can return the following value.
Value | Description |
---|---|
ERROR_INVALID_PARAMETER | One or more of the input parameters is invalid. |
Remarks
If the source rectangle and destination rectangle are different sizes, the source bitmap is stretched to match the destination rectangle. If the SetStretchBltMode function is used, the iStretchMode value is automatically converted to COLORONCOLOR for this function.
The destination coordinates are transformed by using the transformation currently specified for the destination device context. The source coordinates are transformed by using the transformation currently specified for the source device context.
If the source device context identifies an enhanced metafile device context, an error occurs (and the function returns FALSE).
If destination and source bitmaps do not have the same color format, AlphaBlend converts the source bitmap to match the destination bitmap.
AlphaBlend does not support mirroring. If either the width or height of the source or destination is negative, the call to AlphaBlend will fail.
When rendering to a printer, first call GetDeviceCaps with SHADEBLENDCAPS to determine if the printer supports blending with AlphaBlend . Note that, for a display DC, all blending operations are supported and these flags represent whether the operations are accelerated.
The source rectangle must lie completely within the source surface, otherwise an error occurs and the function returns FALSE.
The SourceConstantaAlpha member of the BLENDFUNCTION structure specifies an alpha transparency value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values. If SourceConstantAlpha is 0, it is assumed that the image is transparent. To use only per-pixel alpha values, set the SourceConstantAlpha value to 255 to indicate that the image is opaque.
Requirements
OS Versions: Microsoft® Windows CE® 5.0 and later.
Header: Windows.h.
Link Library: Coredll.lib.
This structure controls blending by specifying the blending functions for source and destination bitmaps.
typedef struct _BLENDFUNCTION {
BYTE BlendOp ;
BYTE BlendFlags ;
BYTE SourceConstantAlpha ;
BYTE AlphaFormat ;
}BLENDFUNCTION, *PBLENDFUNCTION, *LPBLENDFUNCTION;
Members
-
BlendOp
- Specifies the source blend operation. Currently, the only source and destination blend operation that has been defined is AC_SRC_OVER. For details, see the following Remarks section. BlendFlags
- Must be zero. SourceConstantAlpha
- Specifies an alpha transparency value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values in the source bitmap. If you set SourceConstantAlpha to 0, it is assumed that your image is transparent. When you only want to use per-pixel alpha values, set the SourceConstantAlpha value to 255 (opaque) . AlphaFormat
-
This member controls the way the source and destination bitmaps are interpreted. The following table shows the value for
AlphaFormat .
Value Description AC_SRC_ALPHA This flag is set when the bitmap has an Alpha channel (that is, per-pixel alpha). Because this API uses premultiplied alpha, the red, green and blue channel values in the bitmap must be premultiplied with the alpha channel value. For example, if the alpha channel value is x, the red, green and blue channels must be multiplied by x and divided by 0xff before the call.
Remarks
When the AlphaFormat parameter is AC_SRC_ALPHA, the source bitmap must be 32 bpp. If it is not, the AlphaBlend function will fail.
其实关键的一点就是如何使得我们得到的装载进入到得位图。
下面以MFC程序来示例:
HDC hm_DC0=::GetDC(this->m_hWnd);
//装载前置32B RGBA位图
HBITMAP hBmp_Front0=::SHLoadDIBitmap(fileName2);//文件路径
BITMAP bitmap;
GetObject(hBmp_Front0,sizeof(BITMAP),(LPVOID)&bitmap); //获取信息
byte *p=(byte*)bitmap.bmBits;
HDC DirectDC=CreateCompatibleDC(NULL);
HGDIOBJ PreviousObject=SelectObject(DirectDC, hBmp_Front0);
BLENDFUNCTION blend;
blend.AlphaFormat=AC_SRC_ALPHA;
blend.BlendFlags=0;
blend.BlendOp=AC_SRC_OVER;
blend.SourceConstantAlpha=255;
if(!AlphaBlend(hm_DC0,0,0,256,245,DirectDC,0,0,256,245,blend))
{
MessageBox(L"Faild",L"AlphaBlend",MB_OK);
}
//释放资源
DeleteDC(DirectDC);
DeleteObject(hBmp_Front0);
DeleteDC(hm_DC0);