Delphi的剪贴板的AsText方法乱码问题的解决(转)
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;
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;
引用:SetClipboardText(trim((sender as TDBText).Caption));