BOOL AlphaBlendU( HDC dcDest, int x, int y, int cx, int cy,
HDC dcSrc, int sx, int sy, int scx, int scy, int alpha )
{
BITMAPINFOHEADER BMI;
BYTE * pSrcBits;
HBITMAP hbmSrc;
BYTE * pDestBits;
HBITMAP hbmDest;
HDC dc;
HBITMAP dcOld;
int j, i;
// Fill in the header info.
BMI.biSize = sizeof(BITMAPINFOHEADER);
BMI.biWidth = cx;
BMI.biHeight = cy;
BMI.biPlanes = 1;
BMI.biBitCount = 32;
BMI.biCompression = BI_RGB; // No compression
BMI.biSizeImage = 0;
BMI.biXPelsPerMeter = 0;
BMI.biYPelsPerMeter = 0;
BMI.biClrUsed = 0; // Always use the whole palette.
BMI.biClrImportant = 0;
// Create DIB section in shared memory
hbmSrc = CreateDIBSection (dcSrc, (BITMAPINFO *)&BMI,
DIB_RGB_COLORS, (void **)&pSrcBits, 0, 0l);
// Create DIB section in shared memory
hbmDest = CreateDIBSection (dcDest, (BITMAPINFO *)&BMI,
DIB_RGB_COLORS, (void **)&pDestBits, 0, 0l);
// Copy our source and destination bitmaps onto our DIBSections,
// so we can get access to their bits using the BYTE *'s we
// passed into CreateDIBSection
dc = CreateCompatibleDC(NULL);
dcOld = (HBITMAP) SelectObject(dc, hbmSrc);
if (!StretchBlt(dc, 0, 0, cx, cy, dcSrc, sx, sy,
scx, scy, SRCCOPY))
return FALSE;
SelectObject( dc, hbmDest );
if( !StretchBlt( dc, 0, 0, cx, cy, dcDest, x, y, cx, cy, SRCCOPY ) )
return FALSE;
SelectObject( dc, dcOld );
DeleteDC( dc );
for( j = 0; j < cy; ++j )
{
LPBYTE pbDestRGB = (LPBYTE)&((DWORD*)pDestBits)[j * cx];
LPBYTE pbSrcRGB = (LPBYTE)&((DWORD*)pSrcBits)[j * cx];
unsigned char a1, a2;
for( i = 0; i < cx; ++i )
{
a1 = pbSrcRGB[ 3 ];
if( a1 != 0 )
{
a1 = alpha * ( ( a1 * 100 ) / 255 ) / 100;
a2 = 255 - a1;
pbSrcRGB[ 0 ] = ( ( pbDestRGB[ 0 ] * a2 ) + ( pbSrcRGB[ 0 ] * a1 ) ) >> 8;
pbSrcRGB[ 1 ] = ( ( pbDestRGB[ 1 ] * a2 ) + ( pbSrcRGB[ 1 ] * a1 ) ) >> 8;
pbSrcRGB[ 2 ] = ( ( pbDestRGB[ 2 ] * a2 ) + ( pbSrcRGB[ 2 ] * a1 ) ) >> 8;
}
else
{
pbSrcRGB[ 0 ] = pbDestRGB[ 0 ];
pbSrcRGB[ 1 ] = pbDestRGB[ 1 ];
pbSrcRGB[ 2 ] = pbDestRGB[ 2 ];
}
pbSrcRGB += 4;
pbDestRGB += 4;
}
}
dc = CreateCompatibleDC( NULL );
dcOld = (HBITMAP) SelectObject(dc, hbmSrc);
if (!BitBlt(dcDest, x, y, cx, cy, dc, 0, 0, SRCCOPY))
return FALSE;
DeleteDC(dc);
DeleteObject(hbmSrc);
DeleteObject(hbmDest);
return TRUE;
}
HB_FUNC( ABPAINT )
{
HDC hDC = ( HDC ) hb_parnl( 1 );
HDC hDCComp = CreateCompatibleDC( hDC );
BITMAP bm;
HGDIOBJ hOldBmp = SelectObject( hDCComp, ( HBITMAP ) hb_parnl( 4 ) );
GetObject( ( HBITMAP ) hb_parnl( 4 ), sizeof( BITMAP ), ( LPSTR ) &bm );
AlphaBlendU( hDC, hb_parnl( 2 ), hb_parnl( 3 ), bm.bmWidth, bm.bmHeight, hDCComp,
0, 0, bm.bmWidth, bm.bmHeight, hb_parnl( 5 ) );
SelectObject( hDCComp, hOldBmp );
DeleteDC( hDCComp );
}