從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