home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / Clipboard / Clip2.pas < prev   
Pascal/Delphi Source File  |  1998-03-13  |  1KB  |  45 lines

  1. { Copy null-terminated text buffer to clipboard }
  2. procedure TForm1.CopyButtonClick(Sender: TObject);
  3. var
  4.   P: PChar;  { Pointer to character buffer }
  5. begin
  6.   with Memo1 do
  7.   begin
  8.     if SelLength = 0 then   // If no text selected,
  9.       SelectAll;            // select all lines of Memo1.
  10.     if SelLength = 0 then   // If still no text selected,
  11.       Exit;                 // Memo1 is empty--exit now.
  12.     P := StrAlloc(SelLength + 1); // Allocate char buffer
  13.     try
  14.       GetTextBuf(P, SelLength + 1); // Copy text to buffer
  15.       Clipboard.SetTextBuf(P); //Copy text buffer to clipboard
  16.     finally
  17.       StrDispose(P);  // Dispose of character buffer
  18.     end;
  19.   end;
  20. end;
  21.  
  22. { Copy null-terminated text buffer from clipboard }
  23. procedure TForm1.PasteButtonClick(Sender: TObject);
  24. var
  25.   Data: THandle;
  26.   DataPtr: PChar;
  27.   P: PChar;
  28. begin
  29.   Clipboard.Open;
  30.   try
  31.     Data := GetClipboardData(cf_Text);
  32.     if Data = 0 then Exit;
  33.     DataPtr := GlobalLock(Data);
  34.     try
  35.       P := StrNew(DataPtr);
  36.       Memo1.SetTextBuf(P);
  37.     finally
  38.       GlobalUnlock(Data);
  39.       StrDispose(P);
  40.     end;
  41.   finally
  42.     Clipboard.Close;
  43.   end;
  44. end;
  45.