檔案複製、移動、更名、變更目錄皆屬「來源→目標」操作,共用 TChangeForm 對話框與 FileChange 事件處理器。依 Sender 判斷操作類型,設定對話框標題與初始值:
procedure TFMForm.FileChange(Sender: TObject);
var ChangeForm: TChangeForm; IsFile: Boolean;
begin
ChangeForm := TChangeForm.Create(Self);
IsFile := True;
with ChangeForm do
begin
if Sender = Move1 then Caption := 'Move'
else if Sender = Copy1 then Caption := 'Copy'
else if Sender = Rename1 then Caption := 'Rename'
else if Sender = ChangeDirectory1 then begin Caption := 'Change Directory'; IsFile := False; end
else Exit;
if IsFile then begin CurrentDir.Caption := FileList.Directory; ToFileName.Text := ''; end
else begin CurrentDir.Caption := DriveTabSet.Tabs[DriveTabSet.TabIndex]; ToFileName.Text := ''; end;
if (ShowModal <> idCancel) and (ToFileName.Text <> '') then
ConfirmChange(Caption, FromFileName.Text, ToFileName.Text);
end;
end;
procedure TFMForm.ConfirmChange(const ACaption, FromFile, ToFile: String);
begin
if MessageDlg(Format('%s %s to %s', [ACaption, FromFile, ToFile]), mtConfirmation, [mbYes, mbNo], 0) = idYes then
begin
if ACaption = 'Move' then MoveFile(FromFile, ToFile)
else if ACaption = 'Copy' then CopyFile(FromFile, ToFile)
else if ACaption = 'Rename' then RenameFile(FromFile, ToFile)
else if ACaption = 'Change Directory' then ChangeDirectory(ToFile);
FileList.Update;
end;
end;
Properties 選單彈出 TFileAttrForm,讀取/設定屬性位元(faReadOnly/faArchive/faSysFile/faHidden):
procedure TFMForm.ShowFileAttr(FileAttrForm: TFileAttrForm; AFileName, Directory: String);
var Attributes, NewAttributes: Word;
begin
with FileAttrForm do
begin
FileName.Caption := AFileName; FilePath.Caption := Directory;
ChangeDate.Caption := DateTimeToStr(FileDateTime(AFileName));
Attributes := FileGetAttr(AFileName);
ReadOnly.Checked := (Attributes and faReadOnly) = faReadOnly;
Archive.Checked := (Attributes and faArchive) = faArchive;
System.Checked := (Attributes and faSysFile) = faSysFile;
Hidden.Checked := (Attributes and faHidden) = faHidden;
if ShowModal <> idCancel then
begin
NewAttributes := Attributes;
if ReadOnly.Checked then NewAttributes := NewAttributes or faReadOnly else NewAttributes := NewAttributes and not faReadOnly;
if Archive.Checked then NewAttributes := NewAttributes or faArchive else NewAttributes := NewAttributes and not faArchive;
if System.Checked then NewAttributes := NewAttributes or faSysFile else NewAttributes := NewAttributes and not faSysFile;
if Hidden.Checked then NewAttributes := NewAttributes or faHidden else NewAttributes := NewAttributes and not faHidden;
if NewAttributes <> Attributes then FileSetAttr(AFileName, NewAttributes);
end;
end;
end;
FileDateTime 封裝於 fmxutils:Result := FileDateToDateTime(FileAge(FileName))。