我用 C++ 学习 Win API(我是新手)。我对字符/字符串数据类型有疑问。
我也在谷歌阅读了其他文档,但仍然不明白。
今天我遇到了这个问题:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
char MyChar = 0;
switch (message)
{
case WM_CHAR:
MyChar = LOWORD(wParam);
MessageBox(hWnd, (LPCWSTR)MyChar, (LPCWSTR)MyChar, MB_OK);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
目的:输入1个字符,messageBox显示。
我的问题是 MyChar 是一个字符(8 位),我想转换为 LPCWSTR。但是,...没有成功。
任何人都可以帮助我。提前致谢!
最佳答案
LPCWSTR 应为宽字符数组 (wchar_t
) 的地址,MessageBox()
期望该数组以空字符结尾。
然后你可以使用一个有两个元素的数组,在第二个元素中使用空字符,然后像这样修改第一个元素
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
wchar_t myString[2];
myString[1] = '\0'; // Ensure the second element is the null char
switch (message)
{
case WM_CHAR:
myString[0] = LOWORD(wParam); // Modify the first element only
MessageBox(hWnd, myString, myString, MB_OK);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
关于c++ - 从 char 转换为 LPCWSTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19743078/