Friday, March 17, 2017

R/W Clipboard

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

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





https://www.thoughtco.com/basic-clipboard-operations-cut-copy-paste-1058406


//Read from it...

{
Place Timage and Tmemo  on the form.
Retrieve what is in the clipboard. If it is image, it will be placed in Image1, and if text - in Memo1.
}
 

uses Clipboard; 
... 

procedure TForm1.Button1Click(Sender: TObject); 
var 
  MyHandle: THandle; 
begin 
  Clipboard.Open; 
  if Clipboard.HasFormat(CF_TEXT) then 
  begin 
    MyHandle:=Clipboard.GetAsHandle(CF_TEXT); 
    Memo1.Lines.Add(StrPas(GlobalLock(MyHandle))); 
    GlobalUnlock(MyHandle); 
  end; 
  if (Clipboard.HasFormat(CF_BITMAP)) or 
    (Clipboard.HasFormat(CF_PICTURE)) then 
    Image1.Picture.Assign(Clipboard); 
  Clipboard.Close; 
end;



//Monitor the content of clipboard at runtime

{
Create Event handler which tracks changes in the clipboard.
}
 

  private 
    
procedure DisplayClipboard(var Msg: TWMDrawClipBoard); 
      message WM_DRAWCLIPBOARD; 
... 
var 
  Form1: TForm1; 
  ClipboardWindow: HWND; 

implementation 

{$R *.DFM} 


procedure TForm1.DisplayClipboard(var Msg: TWMDrawClipBoard); 
var 
  MyHandle: THandle; 
begin 
  if IsClipboardFormatAvailable(CF_TEXT) or 
     IsClipboardFormatAvailable(CF_BITMAP) then 
  begin 
    OpenClipboard(Form1.Handle); 
    if IsClipboardFormatAvailable(CF_TEXT)then 
    begin 
      MyHandle:=Clipboard.GetAsHandle(CF_TEXT); 
      Memo1.Lines.Add(StrPas(GlobalLock(MyHandle))); 
      GlobalUnlock(MyHandle); 
    end; 

    if IsClipboardFormatAvailable(CF_BITMAP) then 
    begin 
      Panel1.Visible:=True; 
      Image1.Picture.Assign(Clipboard); 
    end; 
    CloseClipboard; 
  end; 
  SendMessage(ClipboardWindow, WM_DRAWCLIPBOARD, 0, 0); 
end; 


procedure TForm1.FormCreate(Sender: TObject); 
begin 
  OpenClipboard(Form1.Handle); 
  EmptyClipboard; 
  CloseClipboard; 
  Panel1.Visible:=False; 
  ClipboardWindow:=SetClipboardViewer(Form1.Handle); 
end;