如何获取unicode字符串的LPCWSTR?

         今天在学习window编程方面的内容时,我想要修改一个窗口的标题,这个标题的内容是窗口的高度,这就遇到一个问题,设置标题的方法是SetWindowText,其第二个形参是LPCWSTR类型,怎么把内容显示到窗口标题栏上呢?

        经过一番查找折腾,发现下面的代码是可行的:

//获取窗口的Rect
RECT rc; 
GetWindowRect(hwnd, &rc);
//定义字符串
std::string strHeight = "height = " + std::to_string(rc.bottom - rc.top);
//处理字符串
size_t size = strHeight.length();
wchar_t* buffer = new wchar_t[size + 1];
MultiByteToWideChar(CP_ACP, 0, strHeight.c_str(), size, buffer, size * sizeof(wchar_t));
buffer[size] = 0;  //确保以 "" 结尾 
//设置窗口标题
SetWindowText(hwnd, buffer);

猜你喜欢

转载自blog.csdn.net/ttod/article/details/135422917