在D2009 +中使用TMemoryStream和TClipboard复制/粘贴

时间:2015-07-23 08:00:21

标签: delphi migration delphi-xe2 clipboard

我有一个方法可以读取TStringGrid行的单元格中的数据,并将其复制到剪贴板。我有一个相应的方法将剪贴板中的数据粘贴到TStringGrid

中的空行

这些方法是为D7编写的,但在迁移到XE2后会中断。

procedure TfrmBaseRamEditor.CopyLine(Sender: TObject; StrGridTemp: TStringGrid;
                                     Row, Column: Integer);
var
  Stream: TMemoryStream;
  MemHandle: THandle;
  MemBlock: Pointer;
  i, Len: Integer;
  RowStr: String;
begin
  Stream := nil;
  try
    Stream := TMemoryStream.Create;

    // The intermediate format to write to the stream.
    // Separate each item by horizontal tab character.
    RowStr := '';
    for i := 0 to (StrGridTemp.ColCount - 1) do
      RowStr := RowStr + StrGridTemp.Cells[i, Row] + #9;
    // Write all elements in a string.
    Len := Length(RowStr);
    Stream.Write(Len, SizeOf(Len));
    Stream.Write(PChar(RowStr)^, Length(RowStr));
    // Request Memory for the clipboard.
    MemHandle := GlobalAlloc(GMEM_DDESHARE, Stream.SIZE);
    MemBlock := GlobalLock(MemHandle);
    try
      // Copy the contents of the stream into memory.
      Stream.Seek(0, soFromBeginning);
      Stream.Read(MemBlock^, Stream.SIZE);
    finally
      GlobalUnlock(MemHandle);
    end;
    // Pass the memory to the clipboard in the correct format.
    Clipboard.Open;
    Clipboard.SetAsHandle(TClipboardFormat, MemHandle);
    Clipboard.Close;
  finally
    Stream.Free;
  end;
end;

procedure TfrmBaseRamEditor.PasteLine(Sender: TObject; StrGridTemp: TStringGrid;
                                      Row, Column: Integer);
var
  Stream: TMemoryStream;
  MemHandle: THandle;
  MemBlock: Pointer;
  ASize, Len, i: Integer;
  TempStr: String;
begin
  Clipboard.Open;
  try
    // If something is in the clipboard in the correct format.
    if Clipboard.HasFormat(TClipboardFormat) then
    begin
      MemHandle := Clipboard.GetAsHandle(TClipboardFormat);
      if MemHandle <> 0 then
      begin
        // Detect size (number of bytes).
        ASize := GlobalSize(MemHandle);
        Stream := nil;
        try
          Stream := TMemoryStream.Create;
          // Lock the contents of the clipboard.
          MemBlock := GlobalLock(MemHandle);
          try
            // Copy the data into the stream.
            Stream.Write(MemBlock^, ASize);
          finally
            GlobalUnlock(MemHandle);
          end;
          Stream.Seek(0, soFromBeginning);
          Stream.Read(Len, SizeOf(Len));
          SetLength(TempStr, Len);
          Stream.Read(PChar(TempStr)^, Stream.SIZE);
          for i := 0 to StrGridTemp.RowCount do
            StrGridTemp.Cells[i, Row] := NextStr(TempStr, #9);
        finally
          Stream.Free;
        end;
      end;
    end;
  finally
    Clipboard.Close;
  end;
end;

当我使用某些值复制行,然后将其粘贴到空行时,会出现问题。第一个单元格正确粘贴,但第二个单元格包含垃圾字符(并且第三列中没有任何内容粘贴)。我知道为什么在第3列以后没有粘贴任何内容:因为分隔列的“水平制表符”字符与单元格内容一起被破坏。

我看过Marco Cantu的“Delphi and Unicode”,但未能弄清楚哪里出错了。

1 个答案:

答案 0 :(得分:3)

CharWideChar的别名。所以在CopyLine

Stream.Write(PChar(RowStr)^, Length(RowStr));

只写一半字符串。它应该是

Stream.Write(PChar(RowStr)^, Length(RowStr)*SizeOf(Char));

PasteLine中,我觉得这句话很奇怪:

Stream.Read(PChar(TempStr)^, Stream.SIZE);

因为你已经消耗了一些你试图读完的字符串。我会这样写:

Stream.Read(PChar(TempStr)^, Len*SizeOf(Char));

请注意,如果您使用与ANSI程序相同的自定义剪贴板格式标识符,那么如果您从一个复制并粘贴到另一个,则编码不匹配。您可能明智地以不同的剪贴板格式注册新的Unicode格式。

其他一些评论:

Stream := nil;
try
  Stream := TMemoryStream.Create;
  ...
finally
  Stream.Free;
end;

应写成:

Stream := TMemoryStream.Create;
try
  ...
finally
  Stream.Free;
end;

如果构造函数引发异常,则不会输入try块。

你真的不需要写出字符串长度。您可以在阅读时依赖流大小来了解字符串的长度。

CopyLine中,剪贴板OpenClose调用应受try / finally块保护。