Selain saluran, warna merah hijau, dan biru, setiap pixel dalam bitmap alpha memiliki komponen transparansi dikenal sebagai alpha channel nya. Alpha channel biasanya mengandung banyak bit sebagai channel warna.
Misalnya, alpha channel 8-bit dapat mewakili 256 tingkat transparansi, dari 0 (bitmap seluruh transparan) sampai 255 (bitmap seluruh buram). Mekanisme alpha blending dipanggil dengan memanggil fungsi AlphaBlend.
Fungsi AlphaBlend menampilkan bitmap yang memiliki piksel transparan atau semi transparan. Hal ini tidak didukung pada Microsoft Windows 95 atau versi Microsoft Windows NT sebelum Microsoft Windows 2000.
Contoh kode berikut membagi jendela menjadi tiga bidang horisontal. Lalu ia menarik sebuah bitmap alpha-blended di setiap daerah jendela.
const
AC_SRC_ALPHA = $1;
procedure DrawAlphaBlend (hWnd : HWND; hdcwnd : HDC);
var
Ahdc : HDC; // handle of the DC we will create
bf : BLENDFUNCTION; // structure for alpha blending
Ahbitmap : HBITMAP; // bitmap handle
bmi : BITMAPINFO; // bitmap header
pvBits : pointer; // pointer to DIB section
ulWindowWidth,
ulWindowHeight : ULONG; // window width/height
ulBitmapWidth,
ulBitmapHeight : ULONG; // bitmap width/height
rt : TRect; // used for getting window dimensions
begin
// 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 = 0 ) and (ulWindowHeight=0)) then
exit;
// divide the window into 3 horizontal areas
ulWindowHeight := trunc(ulWindowHeight / 3);
// create a DC for our bitmap -- the source DC for AlphaBlend
Ahdc := CreateCompatibleDC(hdcwnd);
// zero the memory for the bitmap info
ZeroMemory(@bmi, sizeof(BITMAPINFO));
// setup bitmap info
bmi.bmiHeader.biSize := sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth := trunc(ulWindowWidth - (ulWindowWidth/5)*2);
ulBitmapWidth := trunc(ulWindowWidth - (ulWindowWidth/5)*2);
bmi.bmiHeader.biHeight := trunc(ulWindowHeight - (ulWindowHeight/5)*2);
ulBitmapHeight := trunc(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
Ahbitmap := CreateDIBSection(Ahdc, bmi, DIB_RGB_COLORS, pvBits, 0, 0);
SelectObject(Ahdc, Ahbitmap);
bf.BlendOp := AC_SRC_OVER;
bf.BlendFlags := 0;
bf.SourceConstantAlpha := $7f; // half of 0xff = 50% transparency
bf.AlphaFormat := 0; // ignore source alpha channel
AlphaBlend(hdcwnd, trunc(ulWindowWidth/5), trunc(ulWindowHeight/5),
ulBitmapWidth, ulBitmapHeight,
Ahdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf);
bf.BlendOp := AC_SRC_OVER;
bf.BlendFlags := 0;
bf.AlphaFormat := AC_SRC_ALPHA; // use source alpha
bf.SourceConstantAlpha := $ff; // opaque (disable constant alpha)
AlphaBlend(hdcwnd, trunc(ulWindowWidth/5),
trunc(ulWindowHeight/5+ulWindowHeight), ulBitmapWidth, ulBitmapHeight,
Ahdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf);
bf.BlendOp := AC_SRC_OVER;
bf.BlendFlags := 0;
bf.AlphaFormat := 0;
bf.SourceConstantAlpha := $3A;
AlphaBlend(hdcwnd, trunc(ulWindowWidth/5),
trunc(ulWindowHeight/5+2*ulWindowHeight), ulBitmapWidth,
ulBitmapHeight, Ahdc, 0, 0, ulBitmapWidth,
ulBitmapHeight, bf);
// do cleanup
DeleteObject(Ahbitmap);
DeleteDC(Ahdc);
end
0 comments:
Post a Comment