void FixedLayeredDrawText(HDC hDC, // handle to DC
LPCTSTR lpString, // text to draw
int nCount, // text length
LPRECT lpRect, // formatting dimensions
UINT uFormat // text-drawing options
)
{
HDC hMemDC = ::CreateCompatibleDC(hDC);
if(hMemDC == NULL)
return ;
SetBkMode(hMemDC, TRANSPARENT);
int nWidth = lpRect->right-lpRect->left;
int nHeight = lpRect->bottom-lpRect->top;
HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC, nWidth, nHeight);
int nSavedDC = SaveDC(hMemDC);
::SelectObject(hMemDC, hBitmap);
// RGB(0, 0, 0)透明
TRIVERTEX vert[2] ;
GRADIENT_RECT gRect;
vert [0] .x = 0;
vert [0] .y = 0;
vert [0] .Red = 0x0000;
vert [0] .Green = 0x0000;
vert [0] .Blue = 0x0000;
vert [0] .Alpha = 0x0000;
vert [1] .x = nWidth;
vert [1] .y = nHeight;
vert [1] .Red = 0x0000;
vert [1] .Green = 0x0000;
vert [1] .Blue = 0x0000;
vert [1] .Alpha = 0x0000;
gRect.UpperLeft = 0;
gRect.LowerRight = 1;
GradientFill(hMemDC, vert, 2, &gRect, 1, GRADIENT_FILL_RECT_H);
RECT rcText = {0, 0, nWidth, nHeight};
DrawText(hMemDC, lpString, nCount, &rcText, uFormat);
DWORD dwSize = GetBitmapBits(hBitmap, 0, NULL);
char* pMem = new char[dwSize];
GetBitmapBits(hBitmap, dwSize, pMem);
for(DWORD i = 0; (i+3) < dwSize; i += 4)
{
if((pMem[i] == 0x0)
&& (pMem[i+1] == 0x0)
&& (pMem[i+2]) == 0x0)
continue;
pMem[i+3] = 0xff;
}
SetBitmapBits(hBitmap, dwSize, pMem);
delete[] pMem;
pMem = NULL;
BLENDFUNCTION blend = {AC_SRC_OVER,0,255,AC_SRC_ALPHA};
AlphaBlend(hDC, lpRect->left, lpRect->top, nWidth, nHeight, hMemDC, 0, 0, nWidth, nHeight, blend);
RestoreDC(hMemDC, nSavedDC);
DeleteObject(hBitmap);
DeleteDC(hMemDC);
}