Joh*_*han 3 winapi stringstream lpcwstr
我是Winapi的初学者,我正在尝试将wstringstream转换为LPCWSTR(在WM_PAINT内):
wstringstream ws;
ws << "my text" << endl;
LPCWSTR myWindowOutput = ws.str().c_str();
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 150, 305, myWindowOutput, 10);
Run Code Online (Sandbox Code Playgroud)
它只生产垃圾,有人可以帮忙吗?谢谢.
LPCWSTR myWindowOutput = ws.str().c_str()产生一个临时的(str()调用的返回值),一旦完整的语句结束就会消失.由于您需要临时,您需要将其移至调用,最终消耗它:
TextOutW(hdc, 150, 305, ws.str().c_str(), static_cast<int>(ws.str().length()));
Run Code Online (Sandbox Code Playgroud)
同样,临时生活直到完整的陈述结束.这一次,这足以让API调用使用它.
作为替代方案,您可以将返回值绑定str()到const引用1),然后使用它.这可能更合适,因为您需要使用两次返回值(以获取指向缓冲区的指针,并确定其大小):
wstringstream ws;
ws << "my text" << endl;
hdc = BeginPaint(hWnd, &ps);
const wstring& s = ws.str();
TextOutW(hdc, 150, 305, s.c_str(), static_cast<int>(s.length()));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
329 次 |
| 最近记录: |