📄 unit1.pas
字号:
// ==========================================
// Note:
// 1. For Delphi versions prior v6, search
// for, and unrem {$IFNDEF DEL6_OR_HIGHER}
// conditionals.
// 2. Due to the use of MemoryStreams, do not
// attempt to compress huge amounts of data
// to the clipboard. At time of this writing,
// I am not certain of the memory implications
// with regard to amount of data to be
// compressed.
// ==========================================
Unit Unit1;
Interface
Uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
ExtCtrls,
StdCtrls,
Buttons,
ztvStreams;
// include file - to determine Delphi version (for use with conditionals)
{$i ZipTV.inc} // if using Delphi version conditionals, don't forget to include this!
Type
TForm1 = Class(TForm)
Edit1: TEdit;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
OpenDialog1: TOpenDialog;
Label1: TLabel;
BitBtn1: TBitBtn;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Procedure Button1Click(Sender: TObject);
Procedure Button2Click(Sender: TObject);
Procedure BitBtn1Click(Sender: TObject);
Private
{ Private declarations }
Public
// TStream[XXX]32 are defined in ztvStreams.pas
//{$ifndef DEL6_OR_HIGHER} // must include ziptv.inc (see this units header)
// Procedure HandleDelphi5Backward(cStream: TStream32);
//{$endif}
End;
Var
Form1: TForm1;
Implementation
{$R *.DFM}
Uses
ClipBrd,
ztvRegister,
ztvBase,
ztvInflate,
ztvDeflate,
ztvConsts;
// ==================================================================
// fill Edit1.Text with an existing file to compress
// ==================================================================
Procedure TForm1.BitBtn1Click(Sender: TObject);
Begin
OpenDialog1.Options := [ofFileMustExist, ofHideReadOnly];
If Edit1.Text <> '' Then
Begin
OpenDialog1.InitialDir := ExtractFilePath( Edit1.Text );
OpenDialog1.FileName := ExtractFilename( Edit1.Text )
End Else
OpenDialog1.FileName := '*.*';
OpenDialog1.Title := 'Select file to compress to clipboard';
If OpenDialog1.Execute() Then
Edit1.Text := OpenDialog1.FileName;
End;
// ==================================================================
// Compress to clipboard...
// In this example, we use a TFileStream32 to load data into a stream
// for compression. You can also use data from a TMemoryStream32.
// ==================================================================
Procedure TForm1.Button1Click(Sender: TObject);
Var
Crc: Cardinal;
level: TDeflateType;
CompressedSize: Int64;
FileStream: TFileStream32;
Begin
Memo1.Clear();
If Not FileExists(Edit1.Text) Then Exit;
FileStream := TFileStream32.Create( Edit1.Text, fmOpenRead );
If (FileStream.Handle < 0) Then
Exit;
Try
// ==========================================================
// possible values for level:
// dtDeflateS (Stored - no compression)
// dtDeflateF (Fast - less compression, greater speed)
// dtDeflateN (Normal - normal compression, normal speed)
// dtDeflateX (eXtra - greater compression, less speed)
level := dtDeflateN;
// ==========================================================
// Set the stream position prior to entry of ztvCompress_StreamToClipBoard.
// In this case, we want to compress the entire file so we set it at byte
// position 0
FileStream.Seek(0, soBeginning);
// ztvCompress_StreamToClipboard is in ztvDeflate.pas
Crc :=
ztvCompress_StreamToClipboard(
FileStream, // stream to be compressed
FileStream.Size, // size of data to be compressed
CompressedSize, // returns the CompressedSize
level); // compression level
// CRC_MASK is a constant defined in ztvConsts.pas. It is the default
// return value of ztvCompress_StreamToClipboard if comperssion fails.
//
// show the result
If (Crc <> CRC_MASK) Then
Begin
ShowMessage(
'Crc: ' + IntToStr(Crc) + #13 +
'Compressed: ' + IntToStr(CompressedSize) + ' bytes'#13 +
'Decompressed: ' + IntToStr(FileStream.Size) + ' bytes' )
End Else
ShowMessage('Stream compression to Clipboard failed');
Finally
FileStream.Free();
End;
End;
// ==================================================================
// Decompress from clipboard...
// ==================================================================
Procedure TForm1.Button2Click(Sender: TObject);
Var
Crc: Cardinal;
cStream: TMemoryStream32;
Begin
// check if compressed data is in the clipboard
If Clipboard.HasFormat( CF_COMPRESSED_DATA ) Then
Begin
cStream := TMemoryStream32.Create();
Try
// ztvDecompress_StreamFromClipboard is in ztvInflate.pas
Crc :=
ztvDecompress_StreamFromClipboard( cStream );
// CRC_MASK is a constant defined in ztvConsts.pas. It is the default
// return value of ztvCompress_StreamToClipboard if comperssion fails.
If Crc <> CRC_MASK {cStream.Size > 0} Then
Begin
// Fill the Memo1 control with decompressed data
//{$ifndef DEL6_OR_HIGHER} // must include ziptv.inc (see this units header)
// HandleDelphi5Backward( cStream );
//{$else}
// fill Memo1 with data
Memo1.Lines.LoadFromStream( cStream );
//{$endif}
// show the results
ShowMessage(
'Crc: ' + IntToStr(Crc) + #13 +
'Decompressed: ' + IntToStr(cStream.Size) + ' bytes');
End Else
ShowMessage('Stream decompression from Clipboard failed');
Finally
cStream.Free();
End;
End Else
ShowMessage('Clipboard contains no compressed data');
Button1.SetFocus();
End;
// ==================================================================
// convert TMemoryStream32 to a Delphi TStream (for compatibility
// with Delphi versions up to (and including) Delphi 5)
// ==================================================================
//{$ifndef DEL6_OR_HIGHER} // must include ziptv.inc (see this units header)
//Procedure TForm1.HandleDelphi5Backward(cStream: TStream32);
//Var
// ConvertedStream: TMemoryStream;
//Begin
// ConvertedStream := TMemoryStream.Create();
// Try
// // do the conversion (ztvStreamToTStream is in ztvBase.pas)
// ztvStreamToTStream(cStream, ConvertedStream, cStream.Size);
// // fill Memo1 with data
// Memo1.Lines.LoadFromStream( ConvertedStream );
// Finally
// ConvertedStream.Free();
// End;
//End;
//{$endif}
End.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -