关闭

解决Delphi的剪贴板的AsText方法乱码的问题

1853人阅读 评论(0) 收藏 举报
Wrong character in ClipBoard.AsText??

In Delphi, use ClipBoard unit, a common method "ClipBoard.AsText" is doing a wrong way when using non-English character. How to fix it? Look into the source code, you will find VCL use CF_TEXT to do the job :( change it to CF_UNICODETEXT!
Now we can use following code to due with the problem!

---Chinese---
使用Delphi的剪贴板的AsText方法会导致非英语出现乱码,怎么解决?改用Unicode就OK啦

procedure SetClipboardText(AStr: string);
var    // SetBuffer(CF_TEXT, PChar(Value)^, Length(Value) + 1);
  Data: THandle;
  DataPtr: Pointer;
  Size: Integer;
  WStr: PWideChar;
begin
  Size := Length(AStr) * 4;
  WStr := AllocMem(Size);
  try
    // convert to Unicode
    StringToWideChar(AStr, WStr, Size);
    OpenClipboard(0);
    EmptyClipboard;
    Data := GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, Size);
    try
      DataPtr := GlobalLock(Data);
      try
        Move(WStr^, DataPtr^, Size);
        SetClipboardData(CF_UNICODETEXT, Data);
      finally
        GlobalUnlock(Data);
      end;
    except
      GlobalFree(Data);
      raise;
    end;
  finally
    CloseClipboard;
    FreeMem(WStr);
  end;
end;
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44254次
    • 积分:664
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:0篇
    • 译文:0篇
    • 评论:23条
    文章分类
    最新评论