AlphaBlend
函數(shù)功能:該函數(shù)用來顯示具有指定透明度的圖像。
函數(shù)原型:AlphaBlend(HDC hdcDest,int nXOriginDest,int nYOriginDest,int nWidthDest,int hHeightDest,HDC hdcSrc,int nXOriginSrc,int nYOriginSrc,int nWidthSrc,int nHeightSrc,BLENDFUNCTION blendFunction);
參數(shù):
hdcDest:指向目標設(shè)備環(huán)境的句柄。
nXoriginDest:指定目標矩形區(qū)域左上角的X軸坐標,按邏輯單位。
nYOriginDest:指定目標矩形區(qū)域左上角的Y軸坐標,按邏輯單位。
nWidthDest:指定目標矩形區(qū)域的寬度,按邏輯單位。
hHeghtdest:指向目標矩形區(qū)域高度的句柄,按邏輯單位。
hdcSrc:指向源設(shè)備環(huán)境的句柄。
nXOriginSrc:指定源矩形區(qū)域左上角的X軸坐標,按邏輯單位。
nYOriginSrc:指定源矩形區(qū)域左上角的Y軸坐標,按邏輯單位。
nWidthSrc:指定源矩形

區(qū)域的寬度,按邏輯單位。
nHeightSrc:指定源矩形區(qū)域的高度,按邏輯單位。
blendFunction:指定用于源位圖和目標位圖使用的alpha混合功能,用于整個源位圖的全局alpha值和格式信息。源和目標混合功能當前只限為AC_SRC_OVER。
最后一個參數(shù)blendFunction是一個BLENDFUNCTION結(jié)構(gòu)。BLENDFUNCTION結(jié)構(gòu)控制源和目標位圖的混合方式,它的BlendOp字段指明了源混合操作,但只支持AC_SRC_OVER,即根據(jù)源alpha值把源圖像疊加到目標圖像上。OpenGL的alpha混合還支持其他的方式,如常量顏色源。下一個字段BlendFalgs必須是0,也是為以后的應(yīng)用保留的。最后一個字段AlphaFormat有兩個選擇:0表示常量alpha值,AC_SRC_ALPHA表示每個像素有各自的alpha通道。
如果AlphaFormat字段為0,源位圖中的所有像素使用同樣的常量alpha值,即SourceConstantAlpha字段中的值,該值實際上是0和255,而不是0和1。這里0表示完全透明,255表示完全不透明。目標像素以255-SourceConstantAlpha值作為alpha值。
如果AlphaFormat字段的值是AC_SRC_ALPHA,源設(shè)備表面的每個像素必須有各自的alpha通道。即,必須是32-bpp的物理設(shè)備上下文,或是選中了32-bpp DDB和DIB段的內(nèi)存設(shè)備上下文。這些情況下,每個源像素有4個8位通道:紅、綠、藍和alpha。每個像素的alpha通道和SourceConstantAlpha字段一起用于把源和目標混合起來。
?
BLENDFUNCTION
?? 這個結(jié)構(gòu)體不太明白。
?
至于顏色的繪制,就需要一個重要的函數(shù)了,這就是AlphaBlend。在Windows2000之后,微軟的GDI+逐漸興起,其中最重要的就是支持了 AlphaBlend。這是一個可以讓鼠標透明有陰影,讓圖標支持消除鋸齒的增強型Icon格式,讓窗口半透明的函數(shù)。微軟在GDI+的計劃中添加了這一 API,硬件廠商也跟著支持,所有的廠家都加入到這個新的游戲中來。其實,Photoshop早在1.0版本的時候,就有這個功能了,畢竟軟件和系統(tǒng)的考慮和出發(fā)點不一樣,微軟支持的要晚些,因為他要考慮到整個系統(tǒng)的性能和內(nèi)存占用情況。
??? 使用AlphaBlend,就必須將RGB格式的Canvas升級為ARGB的透明格式。比如,我們將黑色看成是RGB都為0的顏色,計算機的表達式為 0x000000,每兩個0表示16進制的1個字節(jié),而ARGB格式則為0xFF000000,其中最高字節(jié)的0xFF就是16進制的255,也就是說這個黑色是全部顯示的。注意,計算機的術(shù)語和我們平時理解的百分比不一樣,一般都以256為100%的概念,這也和RGB的取值范圍一樣。比如,一個半透明的黑色,就是0x80000000,0x80正好是16進制的128。AlphaBlend除了要考慮畫布上原來的顏色,還要考慮到疊加顏色的RGB值和透明度,這一公式較復(fù)雜,而且也是技術(shù)核心,這里就不再羅列了,有興趣的可以參考很多關(guān)于Photoshop實現(xiàn)原理的軟件。
??? 使用AlphaBlend,我們可以將一個單位的黑色,分配到鄰近的4個點上,將0x25000000這樣的顏色,也就是半透明的黑色,疊加到原來白色的區(qū)域上。不過,這一區(qū)域也可能有其他的顏色,所以,一個區(qū)域如果不停的疊加黑色,也就會越來越黑。
郊果圖:
代碼:
void DrawAlphaBlend (HWND hWnd, HDC hdcwnd)
{
??? HDC hdc;?????????????? // handle of the DC we will create
??? BLENDFUNCTION bf;????? // structure for alpha blending
??? HBITMAP hbitmap;?????? // bitmap handle
??? BITMAPINFO bmi;??????? // bitmap header
??? VOID *pvBits;????????? // pointer to DIB section
??? ULONG?? ulWindowWidth, ulWindowHeight;????? // window width/height
??? ULONG?? ulBitmapWidth, ulBitmapHeight;????? // bitmap width/height
??? RECT??? rt;??????????? // used for getting window dimensions
??? UINT32?? x,y;????????? // stepping variables
??? UCHAR ubAlpha;???????? // used for doing transparent gradient
??? UCHAR ubRed;???????
??? UCHAR ubGreen;
??? UCHAR ubBlue;
??? float fAlphaFactor;??? // used to do premultiply
???????????
??? // get window dimensions
??? GetClientRect(hWnd, &rt);
???
??? // calculate window width/height
??? ulWindowWidth = rt.right - rt.left;?
??? ulWindowHeight = rt.bottom - rt.top;?
??? // make sure we have at least some window size
??? if ((!ulWindowWidth) || (!ulWindowHeight))
??????? return;
??? // divide the window into 3 horizontal areas
??? ulWindowHeight = ulWindowHeight / 3;
??? // create a DC for our bitmap -- the source DC for AlphaBlend
??? hdc = CreateCompatibleDC(hdcwnd);
???
??? // zero the memory for the bitmap info
??? ZeroMemory(&bmi, sizeof(BITMAPINFO));
??? // setup bitmap info
??? // set the bitmap width and height to 60% of the width and height of each of the three horizontal areas. Later on, the blending will occur in the center of each of the three areas.
??? bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
??? bmi.bmiHeader.biWidth = ulBitmapWidth = ulWindowWidth - (ulWindowWidth/5)*2;
??? bmi.bmiHeader.biHeight = ulBitmapHeight = ulWindowHeight - (ulWindowHeight/5)*2;
??? bmi.bmiHeader.biPlanes = 1;
??? bmi.bmiHeader.biBitCount = 32;???????? // four 8-bit components
??? bmi.bmiHeader.biCompression = BI_RGB;
??? bmi.bmiHeader.biSizeImage = ulBitmapWidth * ulBitmapHeight * 4;
??? // create our DIB section and select the bitmap into the dc
??? hbitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0x0);
??? SelectObject(hdc, hbitmap);
??? // in top window area, constant alpha = 50%, but no source alpha
??? // the color format for each pixel is 0xaarrggbb
??? // set all pixels to blue and set source alpha to zero
??? for (y = 0; y < ulBitmapHeight; y++)
??????? for (x = 0; x < ulBitmapWidth; x++)
??????????? ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0x000000ff;
??? bf.BlendOp = AC_SRC_OVER;
??? bf.BlendFlags = 0;
??? bf.SourceConstantAlpha = 0x7f;? // half of 0xff = 50% transparency
??? bf.AlphaFormat = 0;???????????? // ignore source alpha channel
??? if (!AlphaBlend(hdcwnd, ulWindowWidth/5, ulWindowHeight/5,
??????????????????? ulBitmapWidth, ulBitmapHeight,
??????????????????? hdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf))
??????? return;???????????????????? // alpha blend failed
???
??? // in middle window area, constant alpha = 100% (disabled), source
??? // alpha is 0 in middle of bitmap and opaque in rest of bitmap
??? for (y = 0; y < ulBitmapHeight; y++)
??? {???
??for (x = 0; x < ulBitmapWidth; x++)
??{
???if ((x > (int)(ulBitmapWidth/5)) && (x < (ulBitmapWidth-ulBitmapWidth/5)) &&
??????????????? (y > (int)(ulBitmapHeight/5)) && (y < (ulBitmapHeight-ulBitmapHeight/5)))
??????????????? //in middle of bitmap: source alpha = 0 (transparent).
??????????????? // This means multiply each color component by 0x00.
??????????????? // Thus, after AlphaBlend, we have a, 0x00 * r,
??????????????? // 0x00 * g,and 0x00 * b (which is 0x00000000)
??????????????? // for now, set all pixels to red
??????????????? ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0x00ff0000;
??????????? else
??????????????? // in the rest of bitmap, source alpha = 0xff (opaque)
??????????????? // and set all pixels to blue
??????????????? ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0xff0000ff;
??}
?}
???
??? bf.BlendOp = AC_SRC_OVER;
??? bf.BlendFlags = 0;
??? bf.AlphaFormat = AC_SRC_ALPHA;? // use source alpha
??? bf.SourceConstantAlpha = 0xff;? // opaque (disable constant alpha)
??
??? if (!AlphaBlend(hdcwnd, ulWindowWidth/5, ulWindowHeight/5+ulWindowHeight, ulBitmapWidth, ulBitmapHeight, hdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf))
??????? return;
??? // bottom window area, use constant alpha = 75% and a changing
??? // source alpha. Create a gradient effect using source alpha, and
??? // then fade it even more with constant alpha
??? ubRed = 0x00;
??? ubGreen = 0x00;
??? ubBlue = 0xff;
???
??? for (y = 0; y < ulBitmapHeight; y++)
??????? for (x = 0; x < ulBitmapWidth; x++)
??????? {
??????????? // for a simple gradient, base the alpha value on the x
??????????? // value of the pixel
??????????? ubAlpha = (UCHAR)((float)x / (float)ulBitmapWidth * 255);
??????????? //calculate the factor by which we multiply each component
??????????? fAlphaFactor = (float)ubAlpha / (float)0xff;
??????????? // multiply each pixel by fAlphaFactor, so each component
??????????? // is less than or equal to the alpha value.
??????????? ((UINT32 *)pvBits)[x + y * ulBitmapWidth]
??????????????? = (ubAlpha << 24) |?????????????????????? //0xaa000000
???????????????? ((UCHAR)(ubRed * fAlphaFactor) << 16) |? //0x00rr0000
???????????????? ((UCHAR)(ubGreen * fAlphaFactor) << 8) | //0x0000gg00
???????????????? ((UCHAR)(ubBlue?? * fAlphaFactor));????? //0x000000bb
??????? }
??? bf.BlendOp = AC_SRC_OVER;
??? bf.BlendFlags = 0;
??? bf.AlphaFormat = AC_SRC_ALPHA;?? // use source alpha
??? bf.SourceConstantAlpha = 0xbf;?? // use constant alpha, with
???????????????????????????????????? // 75% opaqueness
??? AlphaBlend(hdcwnd, ulWindowWidth/5,
?????????????? ulWindowHeight/5+2*ulWindowHeight, ulBitmapWidth,
?????????????? ulBitmapHeight, hdc, 0, 0, ulBitmapWidth,
?????????????? ulBitmapHeight, bf);
??? // do cleanup
??? DeleteObject(hbitmap);
??? DeleteDC(hdc);
?
}