Alpha blending is used to display an alpha bitmap, which is a bitmap that has
transparent or semi-transparent pixels.
In addition to a red, green, and blue color channel, each pixel in an alpha bitmap
has a transparency component known as its alpha channel. The alpha channel
typically contains as many bits as a color channel. For example, an 8-bit alpha
channel can represent 256 levels of transparency, from 0 (the entire bitmap is
transparent) to 255 (the entire bitmap is opaque). Alpha blending mechanisms are
invoked by calling AlphaBlend function. The AlphaBlend function displays bitmaps
that have transparent or semitransparent pixels. It is not supported on Microsoft
Windows 95 or on versions of Microsoft Windows NT prior to Microsoft Windows 2000.
The following code sample divides a window into three horizontal areas. Then it
draws an alpha-blended bitmap in each of the window areas.
1 const
2 AC_SRC_ALPHA = $1;
3
4 procedure DrawAlphaBlend (hWnd : HWND; hdcwnd : HDC);
5 var
6 Ahdc : HDC; // handle of the DC we will create
7 bf : BLENDFUNCTION; // structure for alpha blending
8 Ahbitmap : HBITMAP; // bitmap handle
9 bmi : BITMAPINFO; // bitmap header
10 pvBits : pointer; // pointer to DIB section
11 ulWindowWidth,
12 ulWindowHeight : ULONG; // window width/height
13 ulBitmapWidth,
14 ulBitmapHeight : ULONG; // bitmap width/height
15 rt : TRect; // used for getting window dimensions
16 begin
17 // get window dimensions
18 GetClientRect(hWnd, rt);
19
20 // calculate window width/height
21 ulWindowWidth := rt.right - rt.left;
22 ulWindowHeight := rt.bottom - rt.top;
23
24 // make sure we have at least some window size
25 if ((ulWindowWidth = 0 ) and (ulWindowHeight=0)) then
26 exit;
27
28 // divide the window into 3 horizontal areas
29 ulWindowHeight := trunc(ulWindowHeight / 3);
30
31 // create a DC for our bitmap -- the source DC for AlphaBlend
32 Ahdc := CreateCompatibleDC(hdcwnd);
33
34 // zero the memory for the bitmap info
35 ZeroMemory(@bmi, sizeof(BITMAPINFO));
36
37 // setup bitmap info
38 bmi.bmiHeader.biSize := sizeof(BITMAPINFOHEADER);
39 bmi.bmiHeader.biWidth := trunc(ulWindowWidth - (ulWindowWidth/5)*2);
40 ulBitmapWidth := trunc(ulWindowWidth - (ulWindowWidth/5)*2);
41 bmi.bmiHeader.biHeight := trunc(ulWindowHeight - (ulWindowHeight/5)*2);
42 ulBitmapHeight := trunc(ulWindowHeight - (ulWindowHeight/5)*2);
43 bmi.bmiHeader.biPlanes := 1;
44 bmi.bmiHeader.biBitCount := 32; // four 8-bit components
45 bmi.bmiHeader.biCompression := BI_RGB;
46 bmi.bmiHeader.biSizeImage := ulBitmapWidth * ulBitmapHeight * 4;
47
48 // create our DIB section and select the bitmap into the dc
49 Ahbitmap := CreateDIBSection(Ahdc, bmi, DIB_RGB_COLORS, pvBits, 0, 0);
50 SelectObject(Ahdc, Ahbitmap);
51
52 bf.BlendOp := AC_SRC_OVER;
53 bf.BlendFlags := 0;
54 bf.SourceConstantAlpha := $7f; // half of 0xff = 50% transparency
55 bf.AlphaFormat := 0; // ignore source alpha channel
56
57 AlphaBlend(hdcwnd, trunc(ulWindowWidth/5), trunc(ulWindowHeight/5),
58 ulBitmapWidth, ulBitmapHeight,
59 Ahdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf);
60
61
62 bf.BlendOp := AC_SRC_OVER;
63 bf.BlendFlags := 0;
64 bf.AlphaFormat := AC_SRC_ALPHA; // use source alpha
65 bf.SourceConstantAlpha := $ff; // opaque (disable constant alpha)
66
67 AlphaBlend(hdcwnd, trunc(ulWindowWidth/5),
68 trunc(ulWindowHeight/5+ulWindowHeight), ulBitmapWidth, ulBitmapHeight,
69 Ahdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf);
70
71 bf.BlendOp := AC_SRC_OVER;
72 bf.BlendFlags := 0;
73 bf.AlphaFormat := 0;
74 bf.SourceConstantAlpha := $3A;
75
76 AlphaBlend(hdcwnd, trunc(ulWindowWidth/5),
77 trunc(ulWindowHeight/5+2*ulWindowHeight), ulBitmapWidth,
78 ulBitmapHeight, Ahdc, 0, 0, ulBitmapWidth,
79 ulBitmapHeight, bf);
80
81 // do cleanup
82 DeleteObject(Ahbitmap);
83 DeleteDC(Ahdc);
84
85 end;
|