0

嗨,我有一个用 WinAPI 创建标签的类。创建了标签,但是在更改新文本的文本绘制在前一个文本之上之后,我想问是否有人能注意到导致这种行为的错误。

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

谢谢。

4

1 回答 1

2

你让它透明,所以你必须使它的父级无效

    void Label::SetText(const String& text)

因为控件不再擦除它的背景。您必须在 create 方法中保存父窗口句柄。并更换

InvalidateRect(m_managerWindow, NULL, TRUE);

有类似的东西

InvalidateRect(m_parentWindow, NULL, TRUE);

您可以通过在标签上调用 GetClientRect 来限制无效矩形,并将其传递给父级上的 InvalidRect。

于 2013-09-05T09:20:21.090 回答