LYSoft

powered by Liu Yang

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

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;
阅读更多
上一篇LYSoft之QQ去广告+本地会员外挂原理
下一篇Fix EEPROM access error for WinAVR
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭