我需要对 TTreeView 的树的 Unicode 字符串做一些事情,所以我想将此字符串加载到内存流中,然后将内存流加载到 TreeView 中。我怎样才能做到这一点?
最佳答案
您很想直接使用 TStringStream
TMemoryStream
的类.但是这个TStringStream
在 Unicode Delphi 版本中,类将在存储之前将 UnicodeString 编码为 AnsiString...
所以这里有一些函数来创建 TMemoryStream
具有纯 Unicode 内容的实例,然后检索此文本:
function StringToMemoryStream(const Text: string): TMemoryStream;
var Bytes: integer;
begin
if Text='' then
result := nil else
begin
result := TMemoryStream.Create;
Bytes := length(Text)*sizeof(Char);
result.Size := Bytes;
move(pointer(Text)^,result.Memory^,Bytes);
end;
end;
function MemoryStreamToString(MS: TMemoryStream): string;
begin
if MS=nil then
result := '' else
SetString(result,PChar(MS.Memory),MS.Size div sizeof(Char));
end;
请确保您
Free
TMemoryStream
当你不再需要它时。通过使用
sizeof(Char)
和 PChar
,此代码也适用于以前的非 Unicode 版本的 Delphi。
关于string - 如何通过 TMemoryStream 将 Unicode 字符串加载到 TTreeView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6315025/