whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

39 Visitors Online


 
...copy a string to the Clipboard and read it back?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (6):  
     


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.
// "Hallo" in die Zwischenablage schreiben und wieder zurücklesen

procedure TForm1.Button1Click(Sender: TObject);
begin
  
StrToClipbrd('Hallo');
  ShowMessage(GetStrFromClipbrd);
end;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners