Question

I have a problem with SetBkMode with TRANSPARENT. The previous text does not clear from a bitmap before drawing a new text. This is the code for drawing.

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

Thanks for advance.

Was it helpful?

Solution

Solved. I have added the code to redraw the parent window before drawing the new text. This is the completed code.

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top