实现AlphaBlend混合实现透明的代码

实现AlphaBlend混合实现透明的代码 收藏
 http://hi.baidu.com/%B8%DF%D5%F1%B8%D5%D1%EE%C8%F0%BB%AA/blog/item/4faf62d35f782931960a167b.html

实现AlphaBlend混合的代码.
主要的算法是:
r = (BYTE)((((rForeground - rBackground)*delta) >> ALPHA) + rBackground);
g = (BYTE)((((gForeground - gBackground)*delta) >> ALPHA) + gBackground);
b = (BYTE)((((bForeground - bBackground)*delta) >> ALPHA) + bBackground);


下面是具体实现。(代码可成功运行)

// 一共2^8 + 1个等级,0为透明,256为不透明,中间的值为半透明
#define ALPHA 8
#define FRAMEPENWIDTH 2 // 文本框的宽度
#define FRAMECOLOR RGB(192,192,192) // 文本框的颜色
#define SHADOWWIDTH 1 // 阴影的宽度(为了有立体感)
#define SHADOWCOLOR RGB(0,0,0) // 阴影的颜色
#define TEXTCOLOR RGB(0,0,192) // 文本的颜色
// 文本框的宽度缺省100像素,宽度、高度可以动态调整
#define DEFAULTOUTPUTWIDTH 100

VOID ShowTransparentText(
HWND hDstWnd, // 在那个窗口透明显示
DWORD Alpha, // Alpha通道值(0 < Alpha < 256)
COLORREF crForeground, // 文本框底色
LPCTSTR lpszTxt, // 文本
DWORD dwDelayTime // 显示多长时间
)
{
COLORREF crBackground;
BYTE r, g, b;
BYTE rBackground, gBackground, bBackground;
BYTE rForeground, gForeground, bForeground;
INT x, y;
INT nDstPosX, nDstPosY;
INT nWidth, nHeight;
HDC hWorkDC, hSaveDC, hDstDC;
HANDLE hBitmap, hBitmap2;
HFONT hf, hfSave;
LOGFONT lf;
RECT rect;
DWORD delta;

//创建文本框字体
lf.lfHeight = 14;
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = FW_NORMAL; //FW_BOLD
lf.lfItalic = FALSE;
lf.lfUnderline = FALSE;
lf.lfStrikeOut = 0;
lf.lfCharSet = ANSI_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
_tcscpy(lf.lfFaceName, TEXT("Tahoma"));

VERIFY(hf = CreateFontIndirect(&lf));

hDstDC = GetDC(hDstWnd);

hWorkDC = CreateCompatibleDC(hDstDC);

hfSave = (HFONT)SelectObject(hWorkDC, hf);
nWidth = DEFAULTOUTPUTWIDTH;
nHeight = DEFAULTOUTPUTWIDTH;
SetRect(&rect, 0,0,nWidth,nHeight);
DrawText(hWorkDC, lpszTxt, lstrlen(lpszTxt), &rect, DT_CALCRECT|DT_LEFT|DT_WORDBREAK);

// 自画立体边框
nWidth = rect.right - rect.left + (FRAMEPENWIDTH + SHADOWWIDTH) * 2;
nHeight = rect.bottom - rect.top + (FRAMEPENWIDTH + SHADOWWIDTH) * 2;

hBitmap = CreateCompatibleBitmap(hDstDC, nWidth, nHeight);
SelectObject(hWorkDC, hBitmap);

hSaveDC = CreateCompatibleDC(hDstDC);
hBitmap2 = CreateCompatibleBitmap(hDstDC, nWidth, nHeight);
SelectObject(hSaveDC, hBitmap2);


GetClientRect(hDstWnd, &rect);
nDstPosX = rect.left + (rect.right - rect.left - nWidth)/2;
nDstPosY = rect.top + (rect.bottom - rect.top - nHeight)/2;
BitBlt(hWorkDC, 0, 0, nWidth, nHeight, hDstDC, nDstPosX, nDstPosY, SRCCOPY);
BitBlt(hSaveDC, 0, 0, nWidth, nHeight, hDstDC, nDstPosX, nDstPosY, SRCCOPY);

delta = Alpha%(1<<ALPHA); // 假若Alpha的值操作256,取模

// 因为0 , 256 对256取模都为0, 但是0为透明,256为不透明
if((0 == delta) && (Alpha == (1<<ALPHA)))
{
delta = Alpha;
}

rForeground = GetRValue(crForeground);
gForeground = GetGValue(crForeground);
bForeground = GetBValue(crForeground);

for(y = SHADOWWIDTH + SHADOWWIDTH; y< (nHeight - (SHADOWWIDTH + SHADOWWIDTH)); y++)
{
for(x = SHADOWWIDTH + SHADOWWIDTH; x < (nWidth - (SHADOWWIDTH + SHADOWWIDTH)); x++)
{
crBackground = GetPixel(hWorkDC, x, y);

rBackground = GetRValue(crBackground);
gBackground = GetGValue(crBackground);
bBackground = GetBValue(crBackground);

r = (BYTE)((((rForeground - rBackground)*delta) >> ALPHA) + rBackground);
g = (BYTE)((((gForeground - gBackground)*delta) >> ALPHA) + gBackground);
b = (BYTE)((((bForeground - bBackground)*delta) >> ALPHA) + bBackground);

SetPixel(hWorkDC, x, y, RGB(r,g,b));
}
}

// 由于Smartphone不提供FrameRect函数,所以自行实现该功能。
// 画出外框
for(y = 0; y< FRAMEPENWIDTH; y++)
{
for(x = 0; x < nWidth; x++)
{
SetPixel(hWorkDC, x, y, FRAMECOLOR);
SetPixel(hWorkDC, x, nHeight - y - 1, FRAMECOLOR);
}
}

for(x = 0; x< FRAMEPENWIDTH; x++)
{
for(y = 0; y < nHeight; y++)
{
SetPixel(hWorkDC, x, y, FRAMECOLOR);
SetPixel(hWorkDC, nWidth - x -1, y, FRAMECOLOR);
}
}

// 画出阴影框
for(y = FRAMEPENWIDTH; y< (FRAMEPENWIDTH+SHADOWWIDTH); y++)
{
for(x = FRAMEPENWIDTH; x < (nWidth - FRAMEPENWIDTH); x++)
{
SetPixel(hWorkDC, x, y, SHADOWCOLOR);
SetPixel(hWorkDC, x, nHeight - y - 1, SHADOWCOLOR);
}
}

for(x = FRAMEPENWIDTH; x< (FRAMEPENWIDTH+SHADOWWIDTH); x++)
{
for(y = FRAMEPENWIDTH; y < (nHeight - FRAMEPENWIDTH); y++)
{
SetPixel(hWorkDC, x, y, SHADOWCOLOR);
SetPixel(hWorkDC, nWidth - x -1, y, SHADOWCOLOR);
}
}

// 输出透明字
SetRect(&rect, (FRAMEPENWIDTH+SHADOWWIDTH), (FRAMEPENWIDTH+SHADOWWIDTH), nWidth - (FRAMEPENWIDTH+SHADOWWIDTH), nHeight - (FRAMEPENWIDTH+SHADOWWIDTH));
SetBkMode(hWorkDC, TRANSPARENT);
SetTextColor(hWorkDC, TEXTCOLOR);
DrawText(hWorkDC, lpszTxt, lstrlen(lpszTxt), &rect, DT_LEFT|DT_WORDBREAK);
BitBlt(hDstDC, nDstPosX, nDstPosY, nWidth, nHeight, hWorkDC, 0, 0, SRCCOPY);
DeleteObject(SelectObject(hWorkDC, hfSave));
DeleteObject(hBitmap);
DeleteDC(hWorkDC);

// 延迟制定时间,最好用WaitForSingleObject, 这样用户既可以终止等待,SetEvent即可
// 或者超时,即相当于Sleep功能
Sleep(dwDelayTime);

// 恢复原来的背景
BitBlt(hDstDC, nDstPosX, nDstPosY, nWidth, nHeight, hSaveDC, 0, 0, SRCCOPY);
DeleteObject(hBitmap2);
DeleteDC(hSaveDC);

ReleaseDC(hDstWnd, hDstDC);

你可能感兴趣的:(实现AlphaBlend混合实现透明的代码)