我正在VS 2019中制作一个桌面应用程序,并尝试用TextOut打印变量x。我知道问题不在于我修改x变量的方式,因为它用OutputDebugString正确地输出了x变量。我对TextOut做错了什么?
以下是我的代码的相关部分:
case WM_PAINT:
{
float x = 1;
while (x < 100) {
x = x + 0.01;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
std::string s = std::to_string(x);
std::wstring stemp = s2ws(s);
LPCWSTR sw = stemp.c_str();
OutputDebugString(sw);
TextOut(hdc, x * 100, 150, sw, 3);
EndPaint(hWnd, &ps);
}
}我预计数字会慢慢增加(1.01、1.02、1.03等)它在100处停止,但我在窗口中得到一个停滞的1.0。任何帮助都将不胜感激。
发布于 2019-04-16 04:37:48
每条WM_PAINT消息只需要调用(Begin|End)Paint()一次。这是因为BeginPaint()将图形区域剪裁为仅包含已失效的区域,然后验证窗口。因此,在您的示例中,循环的第二次迭代和后续迭代将没有任何地方可供绘制,因为裁剪区域将为空。
您需要将调用移到您的循环之外的(Begin|End)Paint()。
也不需要手动将std::string数据转换为std::wstring,只需使用OutputDebugString()和TextOut()的ANSI版,并让它们在内部为您转换为Unicode即可。
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
float x = 1;
while (x < 100) {
x = x + 0.01;
std::string s = std::to_string(x);
OutputDebugStringA(s.c_str());
TextOutA(hdc, x * 100, 150, s.c_str(), 3);
}
EndPaint(hWnd, &ps);
break;
}如果您真的想使用std::wstring,那么只需使用std::to_wstring()而不是std::to_string()
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
float x = 1;
while (x < 100) {
x = x + 0.01;
std::wstring s = std::to_wstring(x);
OutputDebugStringW(s.c_str());
TextOutW(hdc, x * 100, 150, s.c_str(), 3);
}
EndPaint(hWnd, &ps);
break;
}https://stackoverflow.com/questions/55696765
复制相似问题