Mega Code Archive

 
Categories / Delphi / Strings
 

How to copy a string to the Clipboard and read it back

Title: How to copy a string to the Clipboard and read it back uses ClipBrd; procedure StrToClipbrd(StrValue: string); var S: string; hMem: THandle; pMem: PChar; begin hMem := GlobalAlloc(GHND or GMEM_SHARE, Length(StrValue) + 1); if hMem 0 then begin pMem := GlobalLock(hMem); if pMem nil then begin StrPCopy(pMem, StrValue); GlobalUnlock(hMem); if OpenClipboard(0) then begin EmptyClipboard; SetClipboardData(CF_TEXT, hMem); CloseClipboard; end else GlobalFree(hMem); end else GlobalFree(hMem); end; end; function GetStrFromClipbrd: string; begin if Clipboard.HasFormat(CF_TEXT) then Result := Clipboard.AsText else begin ShowMessage('There is no text in the Clipboard!'); Result := ''; end; end; // write "Hallo" to the clipboard and read it back. procedure TForm1.Button1Click(Sender: TObject); begin StrToClipbrd('Hallo'); ShowMessage(GetStrFromClipbrd); end;