具有文本覆盖的双缓冲AlphaBlend rect

时间:2015-10-22 01:49:54

标签: c++ c winapi

我对如何加倍缓冲这个有点困惑。我 不确定是否需要创建另一个CreateCompatibleBitmapCreateCompatibleDC以及如何将其全部链接。 这是按原样工作,但我不认为它是双重缓冲的。

void __OnPaint(HWND hWnd, HDC _hdc = nullptr)
{
    HDC hdc = _hdc;
    PAINTSTRUCT paint;
    RECT& rcClient = paint.rcPaint;

    if (!_hdc)
        hdc = BeginPaint(hWnd, &paint);
    else
        GetClientRect(hWnd, &rcClient);

    if (hdc)
    {
        int width = rcClient.right - rcClient.left;
        int height = rcClient.bottom - rcClient.top;

        HDC hDCMem = CreateCompatibleDC(_hdc);
        HBITMAP hBitmapMem = CreateCompatibleBitmap(hDCMem, width, height);

        SelectObject(hDCMem, hBitmapMem);

        Rectangle(hDCMem, 0, 0, width, height);

        BLENDFUNCTION bfn;

        bfn.BlendOp = AC_SRC_OVER;
        bfn.BlendFlags = 0;
        bfn.AlphaFormat = 0;
        bfn.SourceConstantAlpha = 0x50;

        AlphaBlend(hdc, 0, 0, width, height, hDCMem, 0, 0, width, height, bfn);

        SetTextColor(hdc, RGB(255, 0, 0));
        SetBkMode(hdc, TRANSPARENT);
        DrawText(hdc, "Your text here", -1, &rcClient, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

        //BitBlt(hdc, 0, 0, width, height, hDCMem, 0, 0, SRCCOPY);

        DeleteDC(hDCMem);
        DeleteObject(hBitmapMem);
    }

    if (!_hdc)
        EndPaint(hWnd, &paint);
}

此外,我发现我有另一个问题。 我在WM_TIMER中移动我的窗口,我调用我的__onpaint,问题就是它没有重绘它有一些与alphaBlend相关的东西,它保留了第一次抽奖时窗口下的东西,因为它在我之前工作了用那个

1 个答案:

答案 0 :(得分:0)

双缓冲是在临时位图上进行所有打印和绘图,应该存储在某处。该位图上的图形可以在WM_PAINT事件之外发生(例如:在添加项目或选择更改时)。

然后在WM_PAINT事件上,你唯一要做的就是通过BitBlt函数或类似函数将该位图投影到窗口。

你使用AlphaBlend的方式是错误的。 AlphaBlend用于绘制在现有图像上具有AlphaChanel的图像作为叠加。