procedure MoveFile(const FileName, DestName: TFileName);
var
Destination: TFileName;
begin
Destination := ExpandFileName(DestName);
if not RenameFile(FileName, Destination) then
begin
if HasAttr(FileName, faReadOnly) then
raise EFCantMove.Create(Format(SFCantMove, [FileName]));
CopyFile(FileName, Destination);
DeleteFile(FileName);
end;
end;
EFCanMove是一個自定義異常類:
type
EFCanMove := Class(EStreamError);
有關(guān)自定義異常類請參閱第十二章。
文件刪除、文件更名直接調(diào)用Delphi文件管理過程DeleteFile、RenameFile。它們都以文件名為參數(shù)。操作執(zhí)行前應(yīng)彈出一個對話框進(jìn)行確認(rèn),執(zhí)行完畢后應(yīng)調(diào)用Update方法更新FileList的顯示。
6.4.5.3 一致的界面
文件拷貝、文件移動、 文件更名以及后邊的改變當(dāng)前目錄在形式上都表現(xiàn)為從一個源文件到一個目標(biāo)文件。因而可以采用統(tǒng)一的用戶界面,即ChangeForm對話框
這四個菜單項共用一個Click事件處理過程,通過對Sender參數(shù)的檢測,決定將要打開對話框的標(biāo)題和顯示內(nèi)容。當(dāng)用戶按OK鍵關(guān)閉且目標(biāo)文件(目錄)非空時,程序彈出一個消息對話框要求用戶進(jìn)一步確認(rèn),而后執(zhí)行相應(yīng)的動作。
共用的事件處理過程FileChange的程序清單如下:
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;
FromFileName.Text := FileList.FileName;
ToFileName.Text := '';
end
else
begin
CurrentDir.Caption := DriveTabSet.Tabs[DriveTabSet.TabIndex];
FromFileName.Text := DirectoryOutline.Directory;
ToFileName.Text := '';
end;
if (ShowModal <> idCancel) and (ToFileName.Text <> '') then
ConfirmChange(Caption, FromFileName.Text, ToFileName.Text);
end;
end;
其中用到的自定義私有過程ConfirmChange用于執(zhí)行相應(yīng)的動作:
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;
6.4.5.4 顯示文件屬性
當(dāng)程序執(zhí)行Properties 菜單項的Click 事件處理過程時,首先彈出一個TFileAttrForm類型的對話框,顯示文件的屬性
當(dāng)用戶修改并確認(rèn)后程序重新設(shè)置文件屬性。
Properties菜單項的Click事件處理過程如下:
procedure TFMForm.Properties1Click(Sender: TObject);
var
Attributes, NewAttributes: Word;
FileAttrForm: TFileAttrForm;
begin
FileAttrForm := TFileAttrForm.Create(self);
ShowFileAttr(FileAttrForm,FileList.FileName,FileList.Directory);
end;
其中過程ShowFileAttr的實現(xiàn)如下:
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;
以上過程中用到的函數(shù)FileDataTime在fmxutils單元中定義,返回一個TDatatime類型的變量。
function FileDateTime(const FileName: String): System.TDateTime;
begin
Result := FileDateToDateTime(FileAge(FileName));
end;
6.4.6 其它文件管理功能的實現(xiàn)
在子窗口的Function菜單中,定義了一些其它的文件管理功能:
● Search :查找一個給定名字的文件,若存在則顯示該文件屬性
● Disk View :顯示當(dāng)前驅(qū)動器的大小和剩余空間
● View type :確定顯示文件的類型
6.4.6.1 文件查找
當(dāng)用戶單擊Search菜單項時,程序彈出一個對話框(如圖6.10),要求輸入待查找的文件名和查找路徑。文件名可以是通配符。當(dāng)用戶確認(rèn)后程序顯示第一個匹配文件的屬性(如圖6.9)。查找不到匹配文件則給出相應(yīng)的信息。
在實現(xiàn)這一功能的最初設(shè)計中,我試圖使用FileSearch函數(shù),這個函數(shù)允許在多個不同路徑中查找。但可惜的是:也許由于系統(tǒng)設(shè)計者的失誤,這個函數(shù)并沒有返回它應(yīng)該返回的東西(第一個匹配文件的全路徑名),而是仍把輸入的匹配符返回。
沒有辦法我只能再次使用FindFirst,這個函數(shù)的特性在6.3節(jié)中已進(jìn)行了介紹。下面是這一功能的實現(xiàn)代碼。
procedure TFMForm.search1Click(Sender: TObject);
var
SearchForm: TSearchForm;
FileAttrForm: TFileAttrForm;
FindIt,path: String;
SearchRec: TSearchRec;
Return: Integer;
begin
SearchForm := TSearchForm.Create(self);
with SearchForm do
begin
SearchFile.text := '';
SearchPath.text := DirectoryOutline.Directory;
if (ShowModal <> idCancel) and
(SearchFile.Text <> '') and (SearchPath.text <> '') then
begin
FindIt := SearchPath.text+'\'+SearchFile.text;
Return := FindFirst(FindIt,faAnyFile,SearchRec);
if Return <> 0 then
FindIt := ''
else
FindIt := ExpandFileName(SearchRec.Name);
end;
if FindIt = '' then
MessageDlg('Cannot find the file in current directory.',
mtWarning, [mbOk], 0)
else
begin
Path := ExtractFilePath(FindIt);
FindIt := ExtractFileName(FindIt);
FileAttrForm := TFileAttrForm.Create(self);
ShowFileAttr(FileAttrForm,FindIt,Path);
end;
end;
end;
6.4.6.2 顯示磁盤信息
當(dāng)用戶單擊Disk View菜單項時,將彈出一個TDiskViewForm類型的對話框,用來顯示當(dāng)前磁盤的信息
磁盤信息的獲取是在DiskViewForm中DriveEdit編輯框的OnChange事件處理過程中實現(xiàn)的。
procedure TDiskViewForm.driveEditChange(Sender: TObject);
var
dr: Byte;
Free,Total: LongInt;
begin
Free := DiskFree(0);
Total := DiskSize(0);
FreeSpace.text := IntToStr(Free)+ ' bytes.';
TotalSpace.text := IntToStr(Total) + ' bytes.';
end;
DiskFree、DiskSize帶參數(shù)為0表示當(dāng)前驅(qū)動器。讀者可以很容易把它改成按用戶輸入顯示磁盤信息的情況。
DiskViewForm中的三個編輯框設(shè)計時都令ReadOnly為True。
6.4.6.3 改變顯示文件的類型
改變顯示文件的類型事實上是設(shè)置FileList的Mask屬性。我們利用一個標(biāo)準(zhǔn)的InputBox輸入文件的匹配字符串。而后利用Update方法更新FileList。
procedure TFMForm.Viewtype1Click(Sender: TObject);
var
FileMask: String;
begin
FileMask := InputBox('File type','Input File type For View :',FileList.Mask);
If FileMask = '' then FileMask := '*.*';
FileList.Mask := FileMask;
FileList.Update;
CreateCaption;
end;
其中的CreateCaption私有過程將在(6.4.8)中進(jìn)行介紹。
6.4.7 目錄管理功能的實現(xiàn)
在子窗口的Directory菜單中,提供了目錄管理功能:
● Create Directory :創(chuàng)建一個子目錄
● Delete Directory :刪除一個空的子目錄
● Change Directory :改變當(dāng)前目錄
相關(guān)推薦:2010年9月計算機(jī)等級考試試題及答案解析專題北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |