2

我在 Win32 下有一个小文字绘图拼图。我正在尝试在窗口顶部为我的应用程序用户绘制一些说明。

请参考以下窗口(我已经更改了文本的背景颜色,以便您可以看到边界)

示范
(来源:billy-oneal.com

我目前正在使用 DrawTextEx 将文本绘制到我的窗口,但问题是它没有填满我给它的整个 RECTangle。不绘制该区域就可以了,直到窗口调整大小:

调整大小后的演示
(来源:billy-oneal.com

当文本由于窗口大小而被重新包装时,因为 DrawTextEx 没有清除它的背景,这些工件是剩余的。

我尝试使用 FillRect 填充文本绘制调用后面的区域,这确实消除了视觉伪影,但随后导致文本不断闪烁,因为它被完全擦除然后完全重绘到显示器上。

关于如何获得不包含要使用背景颜色绘制的文本的区域的任何想法?

编辑:如果可能的话,我想避免双重缓冲表格。

EDIT2:当我检测到在调整大小期间换行发生变化时,我只通过重绘文本来解决问题。

4

5 回答 5

4

使用双缓冲?

将所有内容绘制到位图并将位图绘制到窗口。闪烁通常是双缓冲问题。

于 2009-12-02T21:00:36.297 回答
2

有很多可能的解决方案,如果没有看到你的代码,很难判断哪种方法最好,所以我建议看看这篇关于无闪烁绘图的文章

于 2009-12-02T21:06:42.857 回答
2

SetBkMode + SetBkColor ?

于 2009-12-02T21:07:12.607 回答
2

好吧,因为似乎没有人知道该怎么做,所以我以这种方式实现了它:

std::vector<std::wstring> wrapString(HDC hDC, const std::wstring& text, const RECT& targetRect, HFONT font)
{
    std::vector<std::wstring> result;
    RECT targetRectangle;
    CopyRect(&targetRectangle, &targetRect);

    //Calculate the width of the bounding rectangle.
    int maxWidth = targetRectangle.right - targetRectangle.left;

    //Draw the lines one at a time
    std::wstring currentLine;
    for(std::wstring::const_iterator it = text.begin(); it != text.end(); currentLine.push_back(*it), it++)
    {
        if(*it == L'\r' || *it == L'\n')
        { //Hard return
            while(it != text.end() && (*it == L'\r' || *it == L'\n')) it++;
            result.push_back(currentLine);
            currentLine.clear();
        }
        else
        { //Check for soft return
            SIZE sizeStruct;
            GetTextExtentPoint32(hDC, currentLine.c_str(), static_cast<int>(currentLine.length()), &sizeStruct);
            if (sizeStruct.cx > maxWidth)
            {
                std::wstring::size_type lineLength = currentLine.find_last_of(L' ');
                if (lineLength == currentLine.npos)
                { //Word is longer than a line.
                    for(;it != text.end() && !iswspace(*it);it++) currentLine.push_back(*it);
                }
                else
                { //Clip word to line.
                    //Backtrack our scan of the source text.
                    it -= currentLine.length() - lineLength - 1;
                    //Remove the clipped word
                    currentLine.erase(lineLength);
                }
                result.push_back(currentLine);
                currentLine.clear();
            }
        }
    }
    //Last remaining text.
    result.push_back(currentLine);
    return result;
}

void DrawInstructionsWithFilledBackground(HDC hDC, const std::wstring& text, RECT& targetRectangle, HFONT font, COLORREF backgroundColor)
{
    //Set up our background color.
    int dcIdx = SaveDC(hDC);
    HBRUSH backgroundBrush = CreateSolidBrush(backgroundColor);
    SelectObject(hDC, backgroundBrush);
    SelectObject(hDC, font);
    SetBkColor(hDC, backgroundColor);

    std::vector<std::wstring> lines(wrapString(hDC, text, targetRectangle, font));
    for(std::vector<std::wstring>::const_iterator it = lines.begin(); it!=lines.end(); it++)
    {
        RECT backgroundRect = targetRectangle;
        DrawText(hDC, const_cast<LPWSTR>(it->c_str()), static_cast<int>(it->length()), &backgroundRect, DT_CALCRECT | DT_NOCLIP | DT_SINGLELINE);
        backgroundRect.left = backgroundRect.right;
        backgroundRect.right = targetRectangle.right;
        if (backgroundRect.right >= backgroundRect.left)
        FillRect(hDC, &backgroundRect, backgroundBrush);
        ExtTextOut(hDC, targetRectangle.left, targetRectangle.top, ETO_OPAQUE, NULL, it->c_str(), static_cast<UINT>(it->length()), NULL);
        targetRectangle.top += backgroundRect.bottom - backgroundRect.top;
    }
    instructionsWrap = lines;
    //Restore the DC to it's former glory.
    RestoreDC(hDC, dcIdx);
    DeleteObject(backgroundBrush);
}
于 2009-12-07T01:20:30.867 回答
-1

获取/计算 DrawText 调用使用的矩形,并在调用 FillRect 之前使用 ExcludeClipRect 之类的东西对其进行剪辑

于 2009-12-02T21:55:55.490 回答