复制中文文字到剪貼板
在Delphi中把文字复制到剪貼板是很容易的,只要Uses clipbrd;後設定Clipboard.AsText即可,但是如果文字中使用了非ASCII字元,在不同語言的系統中將出現亂碼,解決的方法是复制時說明文字的編碼:
procedure CopyToClipboard(const s: string; LocaleID: LCID = $0404); var MemHandle: HGLOBAL; P: ^LCID; begin if s='' then exit; with Clipboard do begin MemHandle:=GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, sizeof(LCID)); if MemHandle <> 0 then begin p:=GlobalLock(MemHandle); p^:=LocaleID; GlobalUnlock(MemHandle); Open; try AsText := s; SetAsHandle(CF_Locale, MemHandle); finally Close; end; end else astext:=s; end; end;
procedure CopyToClipBoardw(const ws: widestring); var MemHandle: HGLOBAL; ptr: ^LCID; begin MemHandle := GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, 2*length(ws)+2); if MemHandle <> 0 then begin ptr:=GlobalLock(MemHandle); Move(PWideChar(ws)^, ptr^, 2*length(ws)+2); GlobalUnlock(MemHandle); Clipboard.SetAsHandle(CF_UNICODETEXT, MemHandle); end; end;
沒有留言:
發佈留言