string - 如何通过 TMemoryStream 将 Unicode 字符串加载到 TTreeView 中?

标签 string delphi treeview memorystream

我需要对 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/

相关文章:

delphi - 禁用自动更改滚动条位置(禁用滚动到事件控件)

treeview - 从数据 API 动态创建递归 TreeView 的最佳方法

java 在第一个换行符处分隔字符串

c++ - 在 C++ 中将字符串乘以 int

python - 字符串格式化 - Python

string - 将大字符串拆分为子字符串

algorithm - 为什么 Jarvis 的 March ("Gift wrapping algorithm") 的这个实现不起作用?

Delphi + Binance Api + 限价单问题 无效签名

C#、treeView、如何更改标准的右键单击行为

c# - 如果检查了所有子节点,则检查父节点 C# asp.net