(* //
Title: Streaming of clipboard
Description: If you want to borrow a clipboard, you have this set of functions.
Design: zswang
Date: 2003-11-14
Support: wjhu111@21cn.com
// *)
/// begin Source
Uses clipbrd;
Type
TDATAIDNET = Array [0..2] of char;
TclipboardFileHead = Packed Record
Rident: tdataidnet;
Rcount: Word;
END;
TclipboardFileItem = Packed Record
RFORMAT: WORD;
rsize: longword;
RDATA: POINTER;
END;
Const rdataidnet: tdataidnet = 'cbf';
Function ClipboardsaveTroupTream (MSTREAM: TSTREAM): Boolean
VAR
VclipboardFileHead: TclipboardFileHead;
VclipboardFileItem: TclipboardFileItem;
I: integer;
VDATA: THANDLE;
Begin
Result: = FALSE;
IF NOT ASSIGNED (MSTREAM).
VclipboardFileHead.Rident: = rdataidnet;
VclipboardFileHead.rcount: = clipboard.formatcount;
MStream.write (vclipboardfilehead, sizeof (vclipboardfilehead);
Try
CLIPBOARD.OPEN
For i: = 0 to clipboard.formatcount - 1 Do Begin
VDATA: = GetClipboardData (Clipboard.Formats [i]);
VclipboardFileItem.rformat: = clipboard.formats [i];
VclipboardFileItem.Rsize: = GlobalSize (VDATA);
VclipboardFileItem.rdata: = Globalock (VDATA);
Try
MStream.write (VclipboardFileItem, Sizeof (VclipboardFileItem) -
SizeOf (vclipboardfileItem.rdata);
MStream.write (vclipboardfileitem.rdata ^, vclipboardfileIletem.rs);
Finally
GlobalUnlock (VDATA);
END;
END;
Finally
CLIPBOARD.CLOSE;
END;
RESULT: = TRUE;
End; {clipboardsavetrouptream}
Function ClipboardLoadFromstream (MSTream: TSTREAM): Boolean
VAR
VclipboardFileHead: TclipboardFileHead;
VclipboardFileItem: TclipboardFileItem;
I: integer;
VDATA: THANDLE;
Begin
Result: = FALSE;
IF NOT ASSIGNED (MSTREAM).
IF mstream.size <= sizeof (vclipboardfilehead).
MSTream.read (vclipboardfilehead, sizeof (vclipboardfilehead); if vclipboardfilehead .rident <> rdataidnet kil;
CLIPBOARD.CLEAR;
CLIPBOARD.OPEN
Try
For i: = 0 to vclipboardfilehead.rcount - 1 Do Begin
MSTream.read (VclipboardFileItem, Sizeof (VclipboardFileItem) -
SizeOf (vclipboardfileItem.rdata);
vData: = GLOBALLOC (GMEM_MOVEABLE GMEM_DDESHARE,
vclipboardfileItem.rsize;
Try
VclipboardFileItem.rdata: = Globalock (VDATA);
Try
MSTream.read (vclipboardfileitem.rdata ^, vclipboardfileitem.rs);
SetClipboardData (vclipboardfileItem.rformat, vdata);
Finally
GlobalUnlock (VDATA);
END;
Finally
GlobalFree (VDATA);
END;
END;
Finally
CLIPBOARD.CLOSE;
END;
RESULT: = TRUE;
End; {ClipboardLoadFromstream}
/// end source
/// Begin Demo
Procedure TFormClipboard.ButtonsaveClick (Sender: TOBJECT);
VAR
VMemoryStream: tmemorystream;
Begin
IF not savedialog1.execute dam
If FileExists (Savedialog1.FileName) and
(MessageDLG (Format ('Is it overwritten "% s"?', [SaveDialog1.FileName]),
MTWARNING, [MBYES, MBNO], 0).
VMemoryStream: = TMEMORYSTREAM.CREATE
Try
ClipboardsaveTroup (VMemoryStream);
VMemoryStream.savetofile (Savedialog1.FileName);
Finally
VMemoryStream.free;
END;
END;
Procedure TFormClipboard.ButtonLoadClick (Sender: TOBJECT);
VAR
VMemoryStream: tmemorystream;
Begin
IF not opendialog1.execute kilod;
IF not fileexists (openDialog1.filename).
VMemoryStream: = TMEMORYSTREAM.CREATE
Try
vMemoryStream.LoadFromFile (OpenDialog1.FileName);
ClipboardLoadFromstream (VMemoryStream);
Finally
VMemoryStream.free;
END;
END;
/// end demo