0

我有一个SetBkMode问题TRANSPARENT。在绘制新文本之前,之前的文本不会从位图中清除。这是绘图的代码。

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

感谢提前。

4

1 回答 1

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);
}
于 2013-06-13T03:33:51.033 回答