转换 Delphi 7 代码以与 Delphi 2009 一起使用

发布于 2024-09-01 01:35:05 字数 308 浏览 7 评论 0原文

我有一个字符串,我需要访问它的第一个字符,所以我使用了 stringname[1]。有了 unicode 支持,这不再有效。我收到一个错误: [DCC Error] sndkey32.pas(420): E2010 不兼容的类型: 'Char' 和 'AnsiChar'

示例代码:

//vkKeyScan from the windows unit

var
KeyString : String[20];
MKey : Word;

mkey:=vkKeyScan(KeyString[1])

我如何在现代版本的 Delphi 中编写此代码

I have a String that I needed access to the first character of, so I used stringname[1]. With the unicode support this no longer works. I get an error: [DCC Error] sndkey32.pas(420): E2010 Incompatible types: 'Char' and 'AnsiChar'

Example code:

//vkKeyScan from the windows unit

var
KeyString : String[20];
MKey : Word;

mkey:=vkKeyScan(KeyString[1])

How would I write this in modern versions of Delphi

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

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

发布评论

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

评论(2

你没皮卡萌 2024-09-08 01:35:05

String[20] 类型是长度为 20 的 ShortString,即包含 20 个字符的 ShortString。但 ShortString 的行为与 AnsiString 类似,即它们不是 Unicode - 一个字符就是一个字节。因此 KeyString[1] 是 AnsiChar,而 vkKeyScan 函数需要 WideChar (=Char) 作为参数。我真的不知道为什么你想使用类型 String[20] 而不是 String (=UnicodeString),但你可以转换AnsiChar KeyString[1] 转换为 WideChar:

mkey := vkKeyScan(WideChar(KeyString[1]))

The type String[20] is a ShortString of length 20, i.e. a ShortString that contains 20 characters. But ShortStrings behave like AnsiStrings, i.e. they are not Unicode - one character is one byte. Thus KeyString[1] is an AnsiChar, whereas the vkKeyScan function expects a WideChar (=Char) as argument. I really have no idea whatsoever why you want to use the type String[20] instead of String (=UnicodeString), but you could convert the AnsiChar KeyString[1] to a WideChar:

mkey := vkKeyScan(WideChar(KeyString[1]))
凉月流沐 2024-09-08 01:35:05

我突然想到:你真的需要一个字符串,它等于 Delphi 2009 中的 Widestring 吗?

一种选择是定义
var KeyString: AnsiString;

那么当您采用 KeyString[1] 时,它将是 AnsiChar 而不是 Char。

Off the top of my head: do you really need a string, which is equal to widestring in Delphi 2009?

One option is to have the definition
var KeyString: AnsiString;

then when you take KeyString[1] that would be an AnsiChar rather than a Char.

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