XE7 将文本复制到剪贴板



http://www.swissdelphicenter.com/torry/showcode.php?id=640 有一个将列表项复制到剪贴板的示例。 该代码适用于 WIN-XP 和 Delphi 7。 它不适用于 XE7。 我猜 16 位字符或字符串类型会导致问题,因为列表中的数据来自处理 8 位字符的 USB 外围设备。但是代码看起来是正确的。

为了将列表框内容复制到剪贴板中,您可以使用以下代码:

uses
  Vcl.Clipbrd;
procedure TForm1.FormCreate(Sender: TObject);
begin
  Clipboard.AsText := ListBox1.Items.Text;
end;

复制到剪贴板代码应如下所示:

procedure ListBoxToClipboard(ListBox: TListBox; CopyAll: Boolean);
var
  i: Integer;
  s: string;
begin
  s := '';
  for i := 0 to ListBox.Items.Count - 1 do
  begin
    if CopyAll or ListBox.Selected[i] then
      s := s + ListBox.Items[i] + sLineBreak;
  end;
  ClipBoard.AsText := s;
end;

注意:我从原始代码更改了 CopyAll 逻辑,因为它对我来说没有多大意义。要么必须将所有项目复制到剪贴板,要么只能复制到选定的项目。无论列表框是否具有多选功能,都不应有任何区别。

procedure ListBoxToClipBoard(lb:TListBox; copyAll:Boolean);
var
  i: integer;
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create;
  try
    for i := 0 to lb.Items.Count -1  do
      if copyAll or lb.Selected[i] then
        sb.AppendLine(lb.Items[i]);
    Clipboard.AsText := sb.ToString;
  finally
    sb.Free;
  end;
end;

最新更新