Beranda > Delphi > How to save clipboard data to a file

How to save clipboard data to a file

Looking for a way to save the data
in the clipboard to a file in a hurry? All you have to do is call the following  function with the file name :

SaveClipboardTextDataToFile( 'c:\temp.txt' );

Please note that the following code is written to handle only text data in the clipboard. You can easily change this by modifying the clipboard data format specified -- CF_TEXT -- and changing the file handling methods. Don't forget to include Clipbrd unit in your uses section.

function SaveClipboardTextDataToFile(
sFileTo : string ) : boolean;
var
ps1,
ps2   : PChar;
dwLen : DWord;
tf    : TextFile;
hData : THandle;
begin
Result := False;
with Clipboard do
begin
try
Open;
if( HasFormat( CF_TEXT ) ) then
begin
hData := GetClipboardData( CF_TEXT );
ps1   := GlobalLock( hData );
dwLen := GlobalSize( hData );
ps2   := StrAlloc( 1 + dwLen );

StrLCopy( ps2, ps1, dwLen );
GlobalUnlock( hData );

AssignFile( tf, sFileTo );
ReWrite( tf );
Write( tf, ps2 );
CloseFile( tf );

StrDispose( ps2 );
Result := True;
end;
finally
Close;
end;
end;
end;

Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: