gpt4 book ai didi

c++ - 从 char 转换为 LPCWSTR

转载 作者:太空狗 更新时间:2023-10-29 21:44:34 24 4
gpt4 key购买 nike

我用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com