从Windows API调用(GetUserPreferredUILanguages()
)中,我得到一个字符串列表一个零空格分隔PWideChar
。我需要将其转换为Delphi字符串列表。我开始编写代码来手动循环遍历列表,寻找#0
字符。将空分隔的PWideChar转换为字符串列表
有没有更聪明的方法来做到这一点?通过GetUserPreferredUILanguages
返回PWideChar
的
例子:
('e','n','-','U','S',#0,'f','r','-','F','R',#0,#0,...)
(根据我的文档中读取的,因为当我把我的电脑上的功能,它只返回一个语言,即“EN-US “#0#0)
这里是我到目前为止的代码:
procedure GetWinLanguages(aList: TStringList);
var lCount, lSize: ULong;
lChars: array of WideChar;
lIndex, lLastIndex: integer;
begin
lSize := 1000;
SetLength(lChars, lSize);
GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, @lCount, @lChars[0], @lSize);
// untested quick solution to convert from lChars to aList
lIndex := 0;
lLastIndex := -1;
while (lIndex<=lSize) do
begin
while (lIndex<lSize) and (lChars[lIndex]<>#0) do
inc(lIndex);
if (lIndex-lLastIndex)>1 then
begin
// here: copy range lLastIndex to lIndex, convert to string and add to aList
lLastIndex := lIndex;
inc(lIndex);
end else
Break;
end;
end;
PS。我在Windows 10上使用Delphi Berlin进行FMX项目。
对于downvoter:请让我知道这个问题有什么问题。我很乐意改进它。 – Hans