如何将 wParam 转换为 CString?

发布于 2025-01-06 20:41:43 字数 240 浏览 2 评论 0原文

我有一个来自 WM_KEYDOWN 消息的 pMsg->wParam,我想将其转换为 CString。我怎样才能做到这一点?

我尝试过以下代码:

TCHAR ch[2];
ch[0] = pMsg->wParam;
ch[1] = _T('\0');
CString ss(ch);

但它不适用于高 ASCII 字符。

I have a pMsg->wParam from a WM_KEYDOWN message, and I want to convert it into a CString. How can I do that?

I have tried the following code:

TCHAR ch[2];
ch[0] = pMsg->wParam;
ch[1] = _T('\0');
CString ss(ch);

but it does not work for high ASCII characters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爺獨霸怡葒院 2025-01-13 20:41:43

根据文档、WM_CHARwParam 中发送字符代码。备注部分的第一段表示该代码确实是 Unicode UTF-16 代码点。无论您是为 8 位还是 16 位 TCHAR 编译代码,都是如此。

CodyGray 的评论在 CString 提供各种构造函数的部分是正确的。您正在寻找的是将 wchar_t 作为其第一个参数(第二个参数,重复计数,默认设置为 1)。因此,要从 WPARAM 构造 CString,请将值转换为 wchar_t。以下示例打印“0”,确认构造的字符串确实是预期的字符串。

#include <stdio.h>
#include <Windows.h>
#include <cstringt.h>
#include <atlstr.h>
int main ()
{
  WPARAM w = 0x222D;
  CString cs ((wchar_t)w);
  printf ("%d", cs.Compare (L"\x222D"));
}

它在 _UNICODE 和 ANSI 编译模式下的工作方式相同,并且可跨 32 和 64 位移植。

According to the documentation, WM_CHAR sends a character code in wParam. The first paragraph in the Remarks section says that the code is indeed a Unicode UTF-16 codepoint. This is true whether you are compiling your code for 8 or 16 bit TCHAR.

CodyGray's comment is correct in the part that CString supplies a variety of constructors. The one you are looking for is that which takes a wchar_t as its first argument (the second argument, the repetition count, is set to 1 by default). Therefore, to construct a CString out of a WPARAM, you cast the value to wchar_t. The following sample prints "0", confirming that the constructed string is indeed what it is expected to be.

#include <stdio.h>
#include <Windows.h>
#include <cstringt.h>
#include <atlstr.h>
int main ()
{
  WPARAM w = 0x222D;
  CString cs ((wchar_t)w);
  printf ("%d", cs.Compare (L"\x222D"));
}

It will work the same in both _UNICODE and ANSI compilation modes, and is portable across 32 and 64 bitness.

奶茶白久 2025-01-13 20:41:43

问题是 wParam 包含一个指向字符数组的指针。它不是单个字符,因此您无法像此处尝试那样通过将其分配给 ch[0] 来自行创建字符串。

事实证明,该解决方案比您预期的要容易得多。 CString 类有一个构造函数,它接受一个指向字符数组的指针,这正是 wParam 中的内容。
(实际上,它有一堆构造函数,一个几乎可以解决所有问题你永远需要...)

所以你所要做的就是:

CString ss(pMsg->wParam);

构造函数将处理其余的事情,将 wParam 指向的字符串复制到 ss类型。

The problem is that wParam contains a pointer to an array of characters. It is not a single character, so you can't create the string yourself by assigning it to ch[0] as you're trying to do here.

The solution turns out to be a lot easier than you probably expected. The CString class has a constructor that takes a pointer to a character array, which is precisely what you have in wParam.
(Actually, it has a bunch of constructors, one for pretty much everything you'll ever need...)

So all you have to do is:

CString ss(pMsg->wParam);

The constructor will take care of the rest, copying the string pointed to by wParam into the ss type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文