您好我有一个用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);
}
}
}
感谢。
答案 0 :(得分:2)
您使其透明,因此您必须在
中使其父项无效 void Label::SetText(const String& text)
因为控件不再删除它的背景。您必须在create方法中保存父窗口句柄。并替换
InvalidateRect(m_managerWindow, NULL, TRUE);
类似
InvalidateRect(m_parentWindow, NULL, TRUE);
您可以通过调用标签上的GetClientRect来限制无效矩形,并将其传递给父级的InvalidRect。