第六章檔案管理 三

2021-03-13 17:47:24 字數 5134 閱讀 8004

2.畫出每個自畫專案

這在tabset的ondrawtab事件處理過程中完成。這一事件處理過程的引數中包含了待畫專案索引、畫板、待畫區域、是否被選中等。這裡我們只利用了前三個引數。

事實上利用最後乙個引數,我們可以對被選中的標籤進行一些特殊的視覺效果處理。這一工作就留給讀者自己去完成。

procedure tfmform.drivetabsetdrawtab(sender: tobject; tabcanvas: tcanvas;

r: trect; index: integer; selected: boolean);

varbitmap: tbitmap;

begin

bitmap := tbitmap(drivetabset.tabs.objects[index]);

with tabcanvas do

begin

draw(r.left, + 4, bitmap);

textout(r.left + 2 + bitmap.width, + 2, drivetabset.tabs[index]);

end;

end;

6.4.5 檔案管理基本功能的實現

在子視窗的file選單中,定義了檔案管理的基本功能,它們是:

● open :開啟或執行乙個檔案(從檔案列表框雙擊該檔案可實現同樣效果)

● move :檔案在不同目錄間的移動

● copy :檔案拷貝

● delete :檔案刪除

● rename :檔案更名

● properties :顯示檔案屬性

6.4.5.1 檔案開啟

檔案開啟功能可以執行乙個可執行檔案,或把檔案在與之相關聯的應用程式中開啟。檔案總是與建立它的應用程式相關聯,這種關聯可以在windows的檔案管理器中修改。要注意的是:

檔案的關聯是以字尾名為標誌的,因而對乙個檔案關聯方式的修改將影響所有相同字尾名的檔案。

檔案開啟功能實現的關鍵是利用了windows api函式shellexecute 。由於windows api函式的引數要求字串型別是pchar,而delphi中一般用的是有結束標誌的string型別,因此為呼叫方便我們把這一函式進行了重新定義如下。

function executefile(const filename, params, defaultdir: string;

showcmd: integer): thandle;

varzfilename, zparams, zdir: array[0..79] of char;

begin

result := shellexecute(application.mainform.handle, nil,

strpcopy(zfilename, filename), strpcopy(zparams, params),

strpcopy(zdir, defaultdir), showcmd);

end;

以上函式在fmxutils單元中定義。fmxutils是乙個自定義**單元。

有關shellexecute中各引數的具體含義讀者可查閱聯機help檔案。

strpcopy把乙個pascal型別的字串拷貝到乙個無結束符的pchar型別字串中。

在子視窗的open1click事件處理過程中:

procedure tfmform.open1click(sender: tobject);

begin

with filelist do

executefile(filename, '', directory, sw_show) ;

end;

如果filelist允許顯示目錄的話(即filetype屬性再增加一項ftdirectory),那麼對於乙個目錄而言,開啟的含義應該是顯示它下邊的子目錄和檔案。程式修改如下。

procefure tfmform.open1click(sender: tobject);

begin

with filelist do

begin

if hasattr(filename,fadirectory) then

directoryoutline.directory := filename

else

executefile(filename,' ' ,directory,sw_show);

end;

end;

其中hasattr是乙個fmxutils單元中的自定義函式,用於檢測指定檔案是否具有某種屬性。

function hasattr(const filename: string; attr: word): boolean;

begin

result := (filegetattr(filename) and attr) = attr;

end;

6.4.5.2 檔案拷貝、移動、刪除、更名

檔案拷貝的關鍵是使用了以檔案控制代碼為操作物件的檔案管理函式,因而提供了一種底層的i/o通道。在object pascal中這一點是利用無型別檔案實現的。

在檔案拷貝中首先檢查目標檔名是否是乙個目錄。如是則把原檔案的檔名新增到目標路徑後,生成目標檔案全路徑名。而後提取原始檔的時間戳,以備拷貝完成後設定目標檔案。

拷貝過程中使用了返回檔案控制代碼或以檔案控制代碼為引數的檔案管理函式fileopen、filecreate、fileread、filewrite、fileclose。為保證檔案的正常關閉和記憶體的釋放,在拷貝過程中進行異常保護。

過程copyfile實現上述功能,它定義在fmxutils單元中。

procedure copyfile(const filename, destname: tfilename);

varcopybuffer: pointer;

timestamp, bytescopied: longint;

source, dest: integer;

destination: tfilename;

const

chunksize: longint = 8192;

begin

destination := expandfilename(destname);

if hasattr(destination, fadirectory) then

destination := destination + '\' + extractfilename(filename);

timestamp := fileage(filename);

getmem(copybuffer, chunksize);

trysource := fileopen(filename, fmsharedenywrite);

if source < 0 then

raise efopenerror.create(fmtloadstr(sfopenerror, [filename]));

trydest := filecreate(destination);

if dest < 0 then

raise efcreateerror.create(fmtloadstr(sfcreateerror,[destination]));

tryrepeat

bytescopied := fileread(source, copybuffer^, chunksize);

if bytescopied > 0 then

filewrite(dest, copybuffer^, bytescopied);

until bytescopied < chunksize;

finally

filesetdate(dest,timestamp);

fileclose(dest);

end;

finally

fileclose(source);

end;

finally

freemem(copybuffer, chunksize);

end;

end;

如果我們不使用filesetdate過程,windows自動把當前時間作為時間戳寫入檔案。

檔案移動事實上是檔案拷貝與檔案刪除的結合。fmxutils單元中的movefile過程實現了這一功能。

procedure movefile(const filename, destname: tfilename);

vardestination: 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);

有關自定義異常類請參閱第十二章。

檔案刪除、檔案更名直接呼叫delphi檔案管理過程deletefile、renamefile。它們都以檔名為引數。操作執行前應彈出乙個對話方塊進行確認,執行完畢後應呼叫update方法更新filelist的顯示。

6.4.5.3 一致的介面

檔案拷貝、檔案移動、 檔案更名以及後邊的改變當前目錄在形式上都表現為從乙個原始檔到乙個目標檔案。因而可以採用統一的使用者介面,即changeform對話方塊

這四個選單項共用乙個click事件處理過程,通過對sender引數的檢測,決定將要開啟對話方塊的標題和顯示內容。當使用者按ok鍵關閉且目標檔案(目錄)非空時,程式彈出乙個訊息對話方塊要求使用者進一步確認,而後執行相應的動作。

共用的事件處理過程filechange的程式清單如下:

procedure tfmform.filechange(sender: tobject);

varchangeform: tchangeform;

isfile: boolean;

begin

changeform := tchangeform.create(self);

第六章,檔案管理

15.對目錄管理的要求,首先是能實現按名訪問,其次是提高對目錄的檢索速度,同時應允許多個使用者共享檔案,以及允許檔案重名,以便不同使用者能按自己的習慣對檔案命名。16.在採用樹形目錄結構的檔案系統中,樹的結點分為三類 根結點表示根目錄,枝結點表示子目錄檔案,葉結點表示資料檔案。17.在利用線性檢索法...

第六章管理

答案 c 是指生產者至使用者或消費者之間的物流。a.物流 b.生產物流 c.銷售物流 d.營銷物流 答案 c 是企業生產物流系統的終點,也是銷售物流系統的起點。a.運輸 b.配送 c.包裝 d.庫存 答案 c 銷售物流服務有四個要素,即 可靠性 通訊和方便性。a.空間 b.地點 c.時間 d.數量 ...

第六章第六章財務計畫

6.1 資金 投資比例餅圖 希吉雅食品責任 成立初期,準備籌集資金100萬元。發起人自投60萬元,申請大學生創業貸款30萬元,10萬元尋求投資,企業固定資產作投資160萬元,向銀行貸款100萬元。共計註冊資本360萬元。投資比例如圖所示 圖8 1 投資比例 創業自籌資金由創業者個人以其個人名義籌集的...