📄 unit7.pas
字号:
(*
Add/Edit comments (best implementation of archive file comment editing)
Currently, comments are supported only with the zip archive format.
The following sequence of actions are necessary to add/edit comments. The
following commands are decendant methods of the TCompBase object (all
compressor components decend from TCompBase). In this demo, we assign
the variable CompressComponent as the archive type (ArcType) of the file
that is currently being displayed in the main unit (main.pas) of this demo.
1. CommentObjInit: initialize the internal comment object. This will usually
be called in the forms OnActivate event, which deals with editing comments.
This method creates a seperate object for each file compressed to an
archive.
This method returns the number of compressed files.
NO METHOD PARAMETERS
2. GetComment: retrieves an individul files comment for editing. Comments are
retrieved according to it's index in the archives file structure.
METHOD PARAMETERS
Index: (integer)
pass the index number into the archives file structure for which to
retrieve the comment. The value of this parameter must be set prior
a GetComment() call.
NOTE: value range is 0 to NumberOfCompressedFiles - 1.
Filename: (string)
returns the name of the compressed file located at Index.
Comment: (pchar)
returns the comment (if one exists). No memory allocation is required.
If a comment exists, memory is allocated when the CommentObjInit
is called and deallocated when the CommentObjDone method is called.
CommentLen: (word)
returns the length of the stored comment.
NOTE: Use these methods ONLY after a call to "CommentObjInit" and before
CommentObjDone. Attempting to access this method before calling
CommentObjInit or after calling CommentObjDone will cause an application
error.
3. SetComment: sets an individul files comment for storage. Comments are
saved according to it's index in the archives file structure.
METHOD PARAMETERS
Index: (integer)
pass the index number into the archives file structure for which to
store the comment.
Comment: (pchar)
pass the comment to store at the index location.
CommentLen: (word)
length of the comment to store.
NOTE: be sure this value of this parameter is accurate! When
retrieving the comment, an incorrect value can render the archive
corrupt.
NOTE: Use this method ONLY after a call to "CommentObjInit" and before
CommentObjDone. Attempting to access this record before calling
CommentObjInit or after calling CommentObjDone will cause an application
error.
4. CommentObjDone: writes the edited comments to the archive and frees any
memory that was allocated with the CommentObjInit call.
NO METHOD PARAMETERS
5. GetArchiveComment
METHOD PARAMETERS
Comment: (pchar)
returns the archive's main comment (if one exists). No memory
allocation is required.
If a comment exists, memory is allocated when the CommentObjInit
is called and deallocated when the CommentObjDone method is called.
CommentLen: (word)
returns the length of the stored archive's main comment.
NOTE: Use this method ONLY after a call to "CommentObjInit" and before
CommentObjDone. Attempting to access this method before calling
CommentObjInit or after calling CommentObjDone will cause an application
error.
6. SetArchiveComment
METHOD PARAMETERS
Comment: (pchar)
pass the comment to store in the archives main header.
CommentLen: (word)
length of the comment to store.
NOTE: Use this method ONLY after a call to "CommentObjInit" and before
CommentObjDone. Attempting to access this method before calling
CommentObjInit or after calling CommentObjDone will cause an application
error.
IMPORTANT: to assure all memory allocations are freed during this procedure, it
is good programming practice to locate the CommentObjDone method call within
a coding block that will insure it is called without fail, even in the event an
exception is raised. In this example, the CommentObjInit method is located in
this forms OnActivate event handler and the CommentObjDone method is located in
the OnDeactivate event handler.
Example:
CommentObjInit(); <---------
try
GetComment();
SetComment();
finally
CommentObjDone(); <----------
end;
In this demo we used the following variables:
NumberOfCompressedFiles: (integer)
this variable is assigned with the CommentObjInit method call. The value
is the total number of compressed files in an archive.
ScrollBar1.Tag:
this variable is used to store the current index number of the file
comment being edited. Note that in the OnActivate event, the initial
index is set according to which file is hilighted in the main form before
this form is shown. Subsequent assignments of this variable are
made when the scrollbar is scrolled in the OnScroll event.
It's value is used as the file index to use when calling the SetComment()
method.
*)
Unit Unit7;
Interface
Uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
StdCtrls,
ExtCtrls,
ztvGbls;
Type
TfrmEditComments = Class(TForm)
Memo1: TMemo;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
btnCancel: TButton;
btnEditMain: TButton;
btnOK: TButton;
lblArchive: TLabel;
lblFilename: TLabel;
ScrollBar1: TScrollBar;
Procedure FormActivate(Sender: TObject);
Procedure FormDeactivate(Sender: TObject);
Procedure btnCancelClick(Sender: TObject);
Procedure btnOKClick(Sender: TObject);
Procedure btnEditMainClick(Sender: TObject);
Procedure ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode; Var ScrollPos:
Integer);
Procedure ScrollBar1Change(Sender: TObject);
Private
ArchiveComment: Pchar;
ArchiveCommentLen: Word;
FileComment: Pchar; // comment retrieved from the archive
FileCommentLen: Word; // length of comment retrieved
NumberOfCompressedFiles: Integer;
Public
{ Public declarations }
End;
Var
frmEditComments: TfrmEditComments;
Implementation
{$R *.DFM}
{$I defines.inc} //use our TZipView or Delphi's slow TListView control?
Uses
Main;
//-------------------------------------------------------------
Procedure TfrmEditComments.FormActivate(Sender: TObject);
Var
FileName: String;
Index: Integer;
Begin
With frmMain Do
Begin
(* currently only TZip supports editing file comments *)
Case ZipTV1.ArcType Of
atZip: CompressComponent := Zip1;
Else
Exit;
End;
CompressComponent.ArchiveFile := ZipTV1.ArchiveFile;
{$IFDEF ListView}
If ListView1.ItemFocused = Nil Then
Index := -1
Else
Index := ListView1.ItemFocused.Index;
{$ELSE}
Index := ZipView1.ItemIndex;
{$ENDIF}
// CommentObjInit returns the number of compressed files
NumberOfCompressedFiles := CompressComponent.CommentObjInit();
ScrollBar1.Min := 1;
If NumberOfCompressedFiles > 0 Then
Begin
ScrollBar1.Max := NumberOfCompressedFiles;
ScrollBar1.Position := Index + 1;
End
Else
ScrollBar1.Max := 1;
ScrollBar1.Tag := Index; // + 1;
// GetComment returns the values for Filename, FileComment, and FileCommentLen
CompressComponent.GetComment(Index, FileName, FileComment, FileCommentLen);
If (FileComment <> Nil) And (Length(FileComment) > 0) Then
Begin
Memo1.Text := StrPas(FileComment);
Memo1.SelStart := 0;
Memo1.SelLength := FileCommentLen;
End
Else
Memo1.Text := '';
Label4.Caption :=
IntToStr(ScrollBar1.Position) + ' of ' + IntToStr(NumberOfCompressedFiles);
lblArchive.Caption := ZipTV1.ArchiveFile;
lblFilename.Caption := FileName;
End;
btnOK.Enabled := True; // enable OK button
btnEditMain.Caption := '&Edit main...';
ScrollBar1.Enabled := True;
End;
//-------------------------------------------------------------
Procedure TfrmEditComments.btnCancelClick(Sender: TObject);
Begin
CompressComponent.Cancel := True; //prevent saving any edited comments
Close;
End;
//-------------------------------------------------------------
Procedure TfrmEditComments.btnOKClick(Sender: TObject);
Begin
If Length(Memo1.Text) > 0 Then
CompressComponent.SetComment(ScrollBar1.Tag, @Memo1.Text[1], Length(Memo1.Text))
Else
If (FileCommentLen > 0) Then
// User deleted old comment (memo1.txt is blank)
CompressComponent.SetComment(ScrollBar1.Tag, '', 0);
End;
//-------------------------------------------------------------
// Edit main zip header comment
Procedure TfrmEditComments.btnEditMainClick(Sender: TObject);
Var
FileName: String;
Begin
If btnEditMain.Caption = '&Edit main...' Then
Begin
btnOK.Enabled := False; // disable OK button
ScrollBar1.Enabled := False;
btnEditMain.Caption := '&Edit file(s)...';
If Length(Memo1.Text) > 0 Then
CompressComponent.SetComment(ScrollBar1.Tag, @Memo1.Text[1], Length(Memo1.Text))
Else
If (FileCommentLen > 0) Then
// User deleted old comment (memo1.txt is blank)
CompressComponent.SetComment(ScrollBar1.Tag, '', 0);
CompressComponent.GetArchiveComment(ArchiveComment, ArchiveCommentLen);
lblFilename.Caption := '*Editing the main archive comment';
Label4.Caption := '';
Memo1.Text := StrPas(ArchiveComment);
Memo1.SelStart := 0;
Memo1.SelLength := ArchiveCommentLen;
Memo1.SetFocus();
End
Else
Begin
If (MessageDlg('Save main archive comment?', mtConfirmation,
[mbYes, mbNo], 0) = mrYes) Then
CompressComponent.SetArchiveComment(Pchar(Memo1.Text), Length(Memo1.Text));
btnOK.Enabled := True; // enable OK button
ScrollBar1.Enabled := True;
btnEditMain.Caption := '&Edit main...';
// GetComment returns Filename, FileComment, & FileCommentLen
// Use the stored ScrollBar1.Tag as the file index
CompressComponent.GetComment(ScrollBar1.Tag, FileName, FileComment,
FileCommentLen);
If (FileComment <> Nil) And (Length(FileComment) > 0) Then
Begin
Memo1.Text := StrPas(FileComment);
Memo1.SelStart := 0;
Memo1.SelLength := FileCommentLen;
End
Else
Memo1.Text := '';
Label4.Caption :=
IntToStr(ScrollBar1.Position) + ' of ' + IntToStr(NumberOfCompressedFiles);
lblArchive.Caption := frmMain.ZipTV1.ArchiveFile;
lblFilename.Caption := FileName;
End;
End;
//-------------------------------------------------------------
Procedure TfrmEditComments.ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode; Var
ScrollPos: Integer);
Var
FileName: String;
Begin
If ScrollCode = scEndScroll Then
Begin
btnOKClick(Sender);
// GetComment returns the values for Filename, FileComment, and FileCommentLen
CompressComponent.GetComment(ScrollBar1.Position - 1, FileName, FileComment,
FileCommentLen);
lblFilename.Caption := FileName;
Label4.Caption :=
IntToStr(ScrollBar1.Position) + ' of ' + IntToStr(NumberOfCompressedFiles);
Memo1.Text := StrPas(FileComment);
Memo1.SelStart := 0;
Memo1.SelLength := FileCommentLen;
ScrollBar1.Tag := ScrollPos - 1;
End;
End;
//-------------------------------------------------------------
Procedure TfrmEditComments.FormDeactivate(Sender: TObject);
Begin
CompressComponent.CommentObjDone();
End;
//-------------------------------------------------------------
Procedure TfrmEditComments.ScrollBar1Change(Sender: TObject);
Begin
Memo1.SetFocus();
End;
//-------------------------------------------------------------
End.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -