我正在创建Win32控件:
m_progress = CreateWindowExW(0, PROGRESS_CLASSW, L"ProgressBar", WS_VISIBLE | WS_CHILD | WS_TABSTOP, 153, 339, 135, 33, m_window, (HMENU)0, m_instance, 0);
SendMessageW(m_progress, WM_SETFONT, (WPARAM)m_fontBold, TRUE);
SendMessageW(m_progress, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
它正在运行,但我也希望以百分比绘制文字 所以我将这样的进度控制子类化:
m_progressPrevProc = (WNDPROC)SetWindowLongPtrW(m_progress, GWLP_WNDPROC, (LONG_PTR)ProgressMsgProcessor);
...
static LRESULT CALLBACK ProgressMsgProcessor(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if (msg == WM_PAINT)
{
PAINTSTRUCT ps;
RECT rc = { 5, 5, 135, 33 };
//HDC hdc = BeginPaint(hwnd, &ps);
//SelectObject(hdc, g_App.m_fontBold);
//DrawTextA(hdc, "100 %", -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
//EndPaint(hwnd, &ps);
}
return CallWindowProcW((WNDPROC)PrevWndProcProzess, hwnd, msg, wparam, lparam);
}
但如果取消评论至少“HDC hdc = BeginPaint(hwnd,& ps);”然后出现文本,但默认控件绝对消失(就像它没有绘制) 如何修复它以显示带有文本的默认窗口控件,因为我不需要绘制自定义控件,只添加叠加文本? 谢谢
答案 0 :(得分:1)
此处的问题是您使用BeginPaint
和EndPaint
来电清除了更新区域,因此进度条认为不必绘制任何内容。这是WM_PAINT
工作方式的一个弱点,你不能以这种方式在现有控件上绘制。相反,你必须做这样的事情:
static LRESULT CALLBACK ProgressMsgProcessor(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if (msg == WM_PAINT)
{
// Paint the control first
CallWindowProcW ((WNDPROC)PrevWndProcProzess, hwnd, msg, wparam, lparam);
// Then draw over it
HDC hDC = GetDC (hwnd);
HFONT hOldFont = (HFONT) SelectObject(hDC, g_App.m_fontBold);
// Draw your own stuff into hDC
SelectObject (hDC, hOldFont);
ReleaseDC (hwnd, hDC);
return 0;
}
return CallWindowProcW ((WNDPROC)PrevWndProcProgress, hwnd, msg, wparam, lparam);
}
其他说明: