使用带有TRANSPARENT的SetBkMode时,如何修复背景不会更新

时间:2013-06-12 13:03:00

标签: visual-c++ gdi wtl

SetBkModeTRANSPARENT有问题。在绘制新文本之前,先前的文本不会从位图中清除。这是绘图的代码。

void PaintWindow(WTL::CDCHandle dc)
{
    CRect rcWindow;
    HFONT hPrevFont, hFont;
    DWORD dwStyle;
    UINT uTextFormat;

    ATLVERIFY(GetWindowRect(rcWindow));
    dwStyle = GetStyle();

    // Setup Font to use.
    hFont = GetFont();

    if (hFont != nullptr)
        hPrevFont = dc.SelectFont(hFont);
    else
        hPrevFont = nullptr;

    // Setup Text Format.
    uTextFormat = 0;

    if (dwStyle & SS_ENDELLIPSIS)
        uTextFormat |= DT_END_ELLIPSIS;

    if (dwStyle & SS_NOPREFIX)
        uTextFormat |= DT_NOPREFIX;

    if (dwStyle & SS_RIGHT)
        uTextFormat |= DT_RIGHT;

    // Draw Text.
    dc.SetTextColor(m_crTextColor);
    dc.SetBkMode(TRANSPARENT);
    dc.DrawText(m_strText, m_strText.GetLength(), CRect(0, 0, rcWindow.Width(), rcWindow.Height()), uTextFormat);

    // Clean up.
    if (hPrevFont != nullptr)
        dc.SelectFont(hPrevFont);
}

感谢您提前。

1 个答案:

答案 0 :(得分:1)

解决。我在绘制新文本之前添加了重绘父窗口的代码。这是完成的代码。

void PaintWindow(WTL::CDCHandle dc)
{
    CRect rcWindow;
    HFONT hPrevFont, hFont;
    DWORD dwStyle;
    UINT uTextFormat;
    ATL::CWindow wndParent;

    ATLVERIFY(GetWindowRect(rcWindow));
    dwStyle = GetStyle();

    // Redraw Background.
    wndParent = GetParent();

    if (wndParent != nullptr)
    {
        CRect rcInParent = rcWindow;

        ATLVERIFY(wndParent.ScreenToClient(rcInParent));
        ATLVERIFY(wndParent.InvalidateRect(rcInParent));
        ATLVERIFY(wndParent.UpdateWindow());
    }

    // Setup Font to use.
    hFont = GetFont();

    if (hFont != nullptr)
        hPrevFont = dc.SelectFont(hFont);
    else
        hPrevFont = nullptr;

    // Setup Text Format.
    uTextFormat = 0;

    if (dwStyle & SS_ENDELLIPSIS)
        uTextFormat |= DT_END_ELLIPSIS;

    if (dwStyle & SS_NOPREFIX)
        uTextFormat |= DT_NOPREFIX;

    if (dwStyle & SS_RIGHT)
        uTextFormat |= DT_RIGHT;

    // Draw Text.
    dc.SetTextColor(m_crTextColor);
    dc.SetBkMode(TRANSPARENT);
    dc.DrawText(m_strText, m_strText.GetLength(), CRect(0, 0, rcWindow.Width(), rcWindow.Height()), uTextFormat);

    // Clean up.
    if (hPrevFont != nullptr)
        dc.SelectFont(hPrevFont);
}