DrawText在UpdateLayeredWindow中透明的解决办法

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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值