ST3
ST3

Reputation: 8946

What is wrong with this transparent WinAPI label code?

Hi I have a class to create label with WinAPI. Label is created, however after text changing new text is draw above previous-one, I want to ask if someone can notice mistake which leads into such behavior.

namespace Gui
{
    const String Label::LABEL_CLASS = L"MY LABEL";
    WNDCLASSEX Label::m_wndClass = Label::CreateWndClass();

    Label::Label() : m_text(L""), m_font(NULL), m_managerWindow(NULL), m_labelWindow(NULL), m_x(0), m_y(0)
    {}

    Label::Label(const String& text, HFONT font) : m_text(text), m_font(font), m_managerWindow(NULL), m_labelWindow(NULL), m_x(0), m_y(0)
    {}

    Label::~Label()
    {
        if (m_labelWindow != NULL)
            DestroyWindow(m_labelWindow);

        if (m_managerWindow != NULL)
            DestroyWindow(m_managerWindow);
    }

    bool Label::Create(HWND parent, int x, int y, int height, int width)
    {
        m_managerWindow = CreateWindowEx(0, Label::LABEL_CLASS.c_str(), NULL, WS_CHILD | WS_VISIBLE, x, y, width, height, parent, NULL, NULL, NULL);
        if (m_managerWindow == NULL)
            return false;

        m_labelWindow = CreateWindowEx(0, L"STATIC", m_text.c_str(), WS_VISIBLE | WS_CHILD | SS_LEFT, x, y, width, height, m_managerWindow, NULL, NULL, NULL);
        if (m_labelWindow == NULL)
        {
            DestroyWindow(m_managerWindow);
            return false;
        }

        return true;
    }

    void Label::SetText(const String& text)
    {
        m_text = text;
        if (m_labelWindow != NULL)
        {
            SetWindowText(m_labelWindow, m_text.c_str());
            InvalidateRect(m_managerWindow, NULL, TRUE);
        }
    }

    void Label::SetFont(HFONT font)
    {
        m_font = font;
        if (m_labelWindow != NULL)
            SendMessage(m_labelWindow, WM_SETFONT, (WPARAM)m_font, TRUE);
    }

    WNDCLASSEX Label::CreateWndClass()
    {
        WNDCLASSEX m_wndClass = {0};

        if (m_wndClass.cbSize == 0)
        {
            m_wndClass.cbSize = sizeof(WNDCLASSEX);
            m_wndClass.style = CS_NOCLOSE;
            m_wndClass.lpfnWndProc = WndProc;
            m_wndClass.cbClsExtra = 0;
            m_wndClass.cbWndExtra = 0;
            m_wndClass.hInstance = GetModuleHandle(NULL);
            m_wndClass.hIcon = NULL;
            m_wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
            m_wndClass.hbrBackground = NULL;
            m_wndClass.lpszMenuName = NULL;
            m_wndClass.lpszClassName = Label::LABEL_CLASS.c_str(); 
            m_wndClass.hIconSm = NULL;
        }

        RegisterClassEx(&m_wndClass);
        return m_wndClass;
    }

    LRESULT __stdcall Label::WndProc (HWND window, UINT msg, WPARAM wp, LPARAM lp)
    {
        switch(msg)
        {
            case WM_CTLCOLORSTATIC:
            {
                HDC hdc = (HDC) wp; 
                SetBkMode (hdc, TRANSPARENT);

                return (LRESULT)GetStockObject(NULL_BRUSH);
            }
            default:
                return DefWindowProc(window, msg, wp, lp);
        }
    }
}

Thanks.

Upvotes: 0

Views: 234

Answers (1)

Martin Schlott
Martin Schlott

Reputation: 4557

You made it transparent, so you have to invalid its parent in

    void Label::SetText(const String& text)

because the control did not erase it's background any more. You have to save the parent window handle in your create method. And replace the

InvalidateRect(m_managerWindow, NULL, TRUE);

with something like

InvalidateRect(m_parentWindow, NULL, TRUE);

you can restrict the invalid rectangle by calling GetClientRect on your label and pass that to InvalidRect on the parent.

Upvotes: 2

Related Questions