第六章-文件管理(三)(3)2007-05-076.4.5.3 一致的界面 文件拷贝、文件移动、 文件更名以及后边的改变当前目录在形式上都表现为从一个源文件到一个目标文件。因而可以采用统一的用户界面,即ChangeForm对话框这四个菜单项共用一个Click事件处理过程,通过对Sender参数的检测,决定将要打开对话框的标题和显示内容。当用户按OK键关闭且目标文件(目录)非空时,程序弹出一个消息对话框要求用户进一步确认,而后执行相应的动作。共用的事件处理过程FileChange的程序清单如下: procedure TFMForm.FileChange(Sender: TObject);varChangeForm: TChangeForm;IsFile: Boolean;beginChangeForm := TchangeForm.Create(Self);IsFile := True;with ChangeForm dobeginif Sender = Move1 then Caption := "Move"else if Sender = Copy1 then Caption := "Copy"else if Sender = Rename1 then Caption := "Rename"else if Sender = ChangeDirectory1 thenbeginCaption:="Change Directory";IsFile:=False;endelse Exit;if IsFile thenbeginCurrentDir.Caption := FileList.Directory;FromFileName.Text := FileList.FileName;ToFileName.Text := "";endelsebeginCurrentDir.Caption := DriveTabSet.Tabs[DriveTabSet.TabIndex];FromFileName.Text := DirectoryOutline.Directory;ToFileName.Text := "";end;if (ShowModal <> idCancel) and (ToFileName.Text <> "") thenConfirmChange(Caption, FromFileName.Text, ToFileName.Text);end;end; 其中用到的自定义私有过程ConfirmChange用于执行相应的动作: procedure TFMForm.ConfirmChange(const ACaption, FromFile, ToFile: String);beginif MessageDlg(Format("%s %s to %s", [ACaption, FromFile, ToFile]),mtConfirmation, [mbYes, mbNo], 0) = idYes thenbeginif ACaption = "Move" thenMoveFile(FromFile, ToFile)else if ACaption = "Copy" thenCopyFile(FromFile, ToFile)else if ACaption = "Rename" thenRenameFile(FromFile, ToFile)else if ACaption = "Change Directory" thenchangeDirectory(ToFile);FileList.Update;end;end; 6.4.5.4 显示文件属性 当程序执行Properties 菜单项的Click 事件处理过程时,首先弹出一个TFileAttrForm类型的对话框,显示文件的属性当用户修改并确认后程序重新设置文件属性。Properties菜单项的Click事件处理过程如下: procedure TFMForm.Properties1Click(Sender: TObject);varAttributes, NewAttributes: Word;FileAttrForm: TFileAttrForm;beginFileAttrForm := TFileAttrForm.Create(self);ShowFileAttr(FileAttrForm,FileList.FileName,FileList.Directory);end;其中过程ShowFileAttr的实现如下: procedure TFMForm.ShowFileAttr(FileAttrForm:TFileAttrForm;AFileName,Directory:String);varAttributes,NewAttributes: Word;beginwith FileAttrForm dobeginFileName.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 thenbeginNewAttributes := Attributes;if ReadOnly.Checked then NewAttributes := NewAttributes or faReadOnlyelse NewAttributes := NewAttributes and not faReadOnly;if Archive.Checked then NewAttributes := NewAttributes or faArchiveelse NewAttributes := NewAttributes and not faArchive;if System.Checked then NewAttributes := NewAttributes or faSysFileelse NewAttributes := NewAttributes and not faSysFile;if Hidden.Checked then NewAttributes := NewAttributes or faHiddenelse NewAttributes := NewAttributes and not faHidden;if NewAttributes <> Attributes thenFileSetAttr(AFileName, NewAttributes);end;end;end; 以上过程中用到的函数FileDataTime在fmxutils单元中定义,返回一个TDatatime类型的变量。 function FileDateTime(const FileName: String): System.TDateTime;beginResult := FileDateToDateTime(FileAge(FileName));end;