我有一个函数可以获取显示分辨率。我想出了一个主意,但结果只是一些正方形。
LPCWSTR GetDispRes(HWND hWnd)
{
HMONITOR monitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &info);
int arr[2];
arr[0] = info.rcMonitor.right - info.rcMonitor.left;
arr[1] = info.rcMonitor.bottom - info.rcMonitor.top;
LPCWSTR a;
std::wstring s = std::to_wstring(arr[0]);
std::wstring d = std::to_wstring(arr[1]);
std::wstring ress = s + d;
a = (LPCWSTR)ress.c_str();
return a;
}
我从 MessageBox 调用这个函数
MessageBox(NULL, GetDispRes(hWnd) , TEXT("TEST"), NULL);
这是输出:
我的问题是,是什么导致了这个输出?还有什么其他方法可以做到这一点? (将 int 转换为 LPWCSTR)?谢谢。
最佳答案
您的问题很可能是您返回的指针 (LPCWSTR) 在函数外部无效,因为保存数据 (ress) 的对象已被破坏。 因此,您应该更改函数以返回 std::wstring 并在需要的地方调用 .c_str()(在创建消息框时):
std::wstring res = GetDispRes(hWnd);
MessageBox(NULL, res.c_str() , TEXT("TEST"), NULL);
关于c++ - 将 Int 转换为 LPCWSTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19322361/