Alpha blending براى نمایش یک alpha Bitmap که Bitmapی است که پیکسل های transparent (شفاف) یا semi-transparent (نیمه شفاف) دارد استفاده می شود.
به اضافه کانالهای رنگ قرمز، سبز و آبی، هر پیکسلی در یک alpha Bitmap یک موله شفافیت دارد که از آن به عنوان کانال آلفا یاد می شود. کانال آلفا حاوی همان تعداد بیت هایی است که در کانال رنگ وجود دارد. به عنوان مثال یک کانال آلفای ۸ بیت می تواند ۲۵۶ مرحله یا Level از شفافیت را به نمایش بگذارید. از ۰ (تمام تصویر شفاف است) تا ۲۵۶ (تمام تصویر مات است). مکانیسم AlphaBlending (یا ترکیب کردن آلفا) بوسیله صدا کردن تابع AlphaBlend احضار می شود. تابع AlphaBlend بیتمتهایی را که پیکسل های شفاف یا نیمه شفاف دارند نمایش می دهد. این تابع در ویندوز ۹۵ و حتی در ویندوزهای NT قبل از ویندوز ۲۰۰۰ پشتیبانی نمی شد.
کد زیر یک پنجره را به سه قسمت افقی تقسیم می کند. آنگاه یک Bitmap به صورت alpha-blend شده (ترکیب آلفا شده) در هر یک از قسمتهای پنجره نمایش می دهد.
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;
دیدگاه خود را بیان کنید.
باید وارد سایت شده باشید برای دیدگاه دادن