转换 Delphi 7 代码以与 Delphi 2009 一起使用
我有一个字符串,我需要访问它的第一个字符,所以我使用了 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
String[20]
类型是长度为 20 的 ShortString,即包含 20 个字符的 ShortString。但 ShortString 的行为与 AnsiString 类似,即它们不是 Unicode - 一个字符就是一个字节。因此 KeyString[1] 是 AnsiChar,而 vkKeyScan 函数需要 WideChar (=Char) 作为参数。我真的不知道为什么你想使用类型String[20]
而不是String
(=UnicodeString
),但你可以转换AnsiCharKeyString[1]
转换为 WideChar: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 typeString[20]
instead ofString
(=UnicodeString
), but you could convert the AnsiCharKeyString[1]
to a WideChar:我突然想到:你真的需要一个字符串,它等于 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.