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;
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;