實(shí)現(xiàn)AlphaBlend混合的代碼.
主要的算法是:
r = (BYTE)((((rForeground - rBackground)*delta) >> ALPHA) + rBackground);
g = (BYTE)((((gForeground - gBackground)*delta) >> ALPHA) + gBackground);
b = (BYTE)((((bForeground - bBackground)*delta) >> ALPHA) + bBackground);
下面是具體實(shí)現(xiàn)。(代碼可成功運(yùn)行)
// 一共2^8 + 1個(gè)等級(jí),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像素,寬度、高度可以動(dòng)態(tài)調(diào)整
#define DEFAULTOUTPUTWIDTH 100
VOID ShowTransparentText(
HWND hDstWnd, // 在那個(gè)窗口透明顯示
DWORD Alpha, // Alpha通道值(0 < Alpha < 256)
COLORREF crForeground, // 文本框底色
LPCTSTR lpszTxt, // 文本
DWORD dwDelayTime // 顯示多長(zhǎng)時(shí)間
)
{
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;
//創(chuàng)建文本框字體
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,取模
// 因?yàn)? , 256 對(duì)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函數(shù),所以自行實(shí)現(xiàn)該功能。
// 畫出外框
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);
// 延遲制定時(shí)間,最好用WaitForSingleObject, 這樣用戶既可以終止等待,SetEvent即可
// 或者超時(shí),即相當(dāng)于Sleep功能
Sleep(dwDelayTime);
// 恢復(fù)原來(lái)的背景
BitBlt(hDstDC, nDstPosX, nDstPosY, nWidth, nHeight, hSaveDC, 0, 0, SRCCOPY);
DeleteObject(hBitmap2);
DeleteDC(hSaveDC);
ReleaseDC(hDstWnd, hDstDC);
}
//wince半透明效果的實(shí)現(xiàn)
使用windows ce(5.0以上的版本)的一個(gè)api AlphaBlend,用法和BitBlt差不多:
BLENDFUNCTION bf;
bf.AlphaFormat=0;
bf.BlendFlags=0;
bf.BlendOp=AC_SRC_OVER;
bf.SourceConstantAlpha=100;//透明度0-255
AlphaBlend(hBackDC,0,70,73,20,hMaskDC,0,0,73,20,bf);
可以在WindowsCE里用
#include <wingdi.h>
//還要在Project -- setting -- link 里連接上msimg32.lib
////VC用AlphaBlend實(shí)現(xiàn)半透明位圖
Requirements:
Windows NT/2000/XP: Included in Windows 2000 and later.
Windows 95/98/Me: Included in Windows 98 and later.
Header: Declared in Wingdi.h; include Windows.h.
Library: Included as a resource in Msimg32.dll.
示例:
void CTestDlg::SaveBitmap(CDC* pDC,CRect rect,CString filename)
{
CDC* memDC=new CDC;
memDC->CreateCompatibleDC(pDC);
CBitmap* bmp=new CBitmap;
bmp->CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
CBitmap* oldbitmap=memDC->SelectObject(bmp);
//此時(shí)的bmp就相當(dāng)于一張桌布,在memDC中畫線etc都是畫在這張桌布上
if(!memDC->BitBlt(0,0,rect.Width(),rect.Height(),pDC,0,0,SRCCOPY))
{
AfxMessageBox("BitBlt Error!");
return;
}
memDC->Ellipse(0,0,100,100);
memDC->SelectObject(oldbitmap);
BITMAPINFO bi;
bi.bmiHeader.biSize=sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth=rect.Width();
bi.bmiHeader.biHeight=rect.Height();
bi.bmiHeader.biPlanes=1;
bi.bmiHeader.biBitCount=16;
bi.bmiHeader.biCompression=BI_RGB;
bi.bmiHeader.biSizeImage=0;
bi.bmiHeader.biXPelsPerMeter=0;
bi.bmiHeader.biYPelsPerMeter=0;
bi.bmiHeader.biClrUsed=0;
bi.bmiHeader.biClrImportant=0;
int bitsize=rect.Width()*rect.Height()*2;
BYTE* bits=new BYTE[bitsize];
::GetDIBits(memDC->m_hDC,*bmp,0,rect.Height(),bits,&bi,DIB_RGB_COLORS);
BITMAPFILEHEADER bf;
bf.bfType=(int)'M'*256+'B';
bf.bfSize=bitsize;//sizeof(bf);
bf.bfOffBits=sizeof(bi.bmiHeader)+sizeof(bf);
bf.bfReserved1=0;
bf.bfReserved2=0;
CFile f(filename,CFile::modeCreate|CFile::modeWrite);
f.Write(&bf,sizeof(bf));//注意是先寫bf,再寫bi
f.Write(&bi,sizeof(bi));
f.Write(bits,bitsize);
f.Close();
delete[] bits;
delete bmp;
delete memDC;
}
將memDC上的位圖半透明覆蓋到pDC上
BLENDFUNCTION bm;
bm.BlendOp=AC_SRC_OVER;
bm.BlendFlags=0;
bm.SourceConstantAlpha=100;
bm.AlphaFormat=0;
AlphaBlend(pDC->m_hDC,0,0,rect.Width(),rect.Height(),memDC->m_hDC,0,0,rect.Width(),rect.Height(),bm);
聯(lián)系客服