如何使用c ++ / WinAPI绘制透明背景的文本? [英] How to draw text with transparent background using c++/WinAPI?

查看:733
本文介绍了如何使用c ++ / WinAPI绘制透明背景的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用WinAPI绘制透明颜色的文本?
在通常的方式,我使用SetBkMode(hDC,TRANSPARENT),但现在我需要使用双缓冲区。
以这种方式图像绘制正确,但文本绘制不正确(黑色背景)。

How to draw text with transparent color using WinAPI? In usual way I used SetBkMode(hDC, TRANSPARENT), but now I need to use double buffer. In this way images draws correct, but text draws not correct (with black background).

case WM_PAINT:
{
    hDC = BeginPaint(hWnd, &paintStruct);
    SetBkMode(hDC, TRANSPARENT);

    HDC cDC = CreateCompatibleDC(hDC);
    HBITMAP hBmp = CreateCompatibleBitmap(hDC, width, height);
    HANDLE hOld = SelectObject(cDC, hBmp);

    HFONT hFont = (HFONT)SelectObject(hDC, font);
    SetTextColor(cDC, color);
    SetBkMode(cDC, TRANSPARENT);

    TextOut(cDC, 0, 0, text, wcslen(text));

    SelectObject(cDC, hFont);

    BitBlt(hDC, 0, 0, width, height, cDC, 0, 0, SRCCOPY);

    SelectObject(cDC, hOld);
    DeleteObject(hBmp);
    DeleteDC(cDC);

    EndPaint(hWnd, &paintStruct);
    return 0;
}


推荐答案

颜色未指定。文档没有说明它是如何初始化的,但实心的黑色(全零)似乎可能。由于您在位图上绘制文本,位图的背景保持黑色。

When you create a bitmap, the color isn't specified. The documentation doesn't state how it's initialized, but solid black (all zeros) seems likely. Since you're drawing the text on the bitmap, the background of the bitmap remains black. You then copy the entire bitmap to the DC and all the pixels come along, the background along with the text.

要解决这个问题,您必须将所需的背景复制到位图中在您绘制文本之前。

To fix this you must copy the desired background into the bitmap before you draw the text.

这篇关于如何使用c ++ / WinAPI绘制透明背景的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆