|
源:http://52wuxinle.blog.163.com/blog/static/8339236020110702019118/ 問題一:在Win98中右擊“我的文檔”,選屬性,在彈出的“我的文檔 屬性”窗口 中點擊“瀏覽”按鈕就會彈出一個“瀏覽文件夾”對話框。請問這個對話框是怎么做出 來的? 答案:要做這個對話框有三種方法。 ?。ㄒ唬┑谝环N方法是用Delphi提供的組件(在Win 3.1面板上)模仿在上面看到的對 話框自己組裝一個“瀏覽文件夾”窗體。具體的做法是: 1. 在你的Project里增加一個BorderStyle為bsDialog的新窗體; 2.放置一個DirectoryListBox組件; 3. 放置一個DriveComboBox組件,設置DirList為DirectoryListBox1; 4.然后再放上兩個Button。一個“確定”(ModalResult為mrOk),一個“取消” (ModalResult為mrCancel); 5.最后只要在調(diào)用這個瀏覽文件夾的地方加上一下代碼就算大功告成了: if Form2.ShowModal = mrOk then Memo1.Lines.Add(Form2.DirectoryListBox1.Directory); ?。ǘ┑诙N方法,在Delphi中可以通過調(diào)用SelectDirectory函數(shù)得到這種效果。 SelectDirectory在Delphi 4中的申明如下(請注意,一共有兩個重載的申明): type TSelectDirOpt = (sdAllowCreate, sdPerformCreate, sdPrompt); TSelectDirOpts = set of TSelectDirOpt; function SelectDirectory(var Directory: string; Options: TSelectDirOpts; HelpCtx: Longint): Boolean; overload; function SelectDirectory(const Caption: string; const Root:WideString; o ut Directory: string): Boolean; overload; 第一種語法的Options參數(shù)指定了“瀏覽文件夾”對話框的一些選項;參數(shù)HelpCtx 指定上下文敏感的Help ID;Directory初始化了對話框的值,并且攜帶返回值。 第二種語法的Caption參數(shù)指定對話框標題(比如′請選擇XXX的文件夾′);參數(shù) Root指定用來瀏覽的根目錄;所選擇文件夾返回在參數(shù)Directory中。 不管是哪種語法,如果在對話框中選擇了路徑并按下“確定”按鈕,SelectDirect ory函數(shù)返回True;在其它情況下,函數(shù)SelectDirectory就返回False。 ?。ㄈ┑谌N方法是比較高明的解決方案。在Windows中已經(jīng)有一個專門用來處理這 種問題的ShellAPI函數(shù)——SHBrowseForFolder(事實上,第二種方法的第二種語法就是 調(diào)用了這個API,這在Delphi的源代碼中可以得到證實)。因為它是使用系統(tǒng)已有的API ,這樣就不會占用太多的系統(tǒng)資源,從而減小代碼長度、提高程序運行速度,并且在Wi ndows的不同語言版本中會自動的和Windows相適應。具體代碼如下: var Info: TBrowseInfo; Dir: array[0..260] of char; ItemId: PItemIDList; begin with Info do begin hwndOwner := self.Handle; pidlRoot := nil; pszDisplayName := nil; lpszTitle := ′請選擇XXX的文件夾′; ulFlags := 0; lpfn := nil; lParam := 0; iImage := 0; end; ItemId := SHBrowseForFolder(Info); if ItemId <> nil then begin SHGetPathFromIDList(ItemId, @Dir); Result := string(Dir); end else Result := ′′; end; 如果你對最后的這種方法感興趣,以Browsing for Folders為主題在Windows API Help中檢索將會得到更多的文檔。 問題二:在許多軟件的制作過程中我都遇到文件復制這個問題,但我對文件操作很 不熟悉。請問,在Delphi中有簡單的方法能夠?qū)崿F(xiàn)這個功能嗎? 答案:實現(xiàn)它的源代碼如下: var DesFile, SourFile: File; Buf: Byte; begin AssignFile(SrcFile, ″c:\autoexec.bat″); Reset(SrcFile, 1);// 1 = 逐個字節(jié)操作 AssignFile(DesFile, ″c:\autoexec.bak″); Rewrite(DesFile, 1); // 同上 while not Eof(SrcFile) do begin BlockRead(SrcFile, Buf, SizeOf(Byte)); // 從源文件中讀出來 BlockWrite(DesFile, Buf, SizeOf(Byte)); // 寫到目標文件中去 end; CloseFile(SrcFile); CloseFile(DesFile); end; 另外還有一個高招——直接使用API函數(shù)CopyFile。這個API的原型如下: BOOL CopyFile( LPCTSTR lpExistingFileName, // pointer to name of an existing file LPCTSTR lpNewFileName, // pointer to filename to copy to BOOL bFailIfExists // flag for operation if file exists ); 如:CopyFile( PChar(′c:\autoexec.bat′), PChar(′c:\autoexec.bak′), True);
Delphi如何實現(xiàn)瀏覽文件夾,并得出所選擇的文件夾名: SelectDirectory函數(shù)(FileCtrl單元) 1、function SelectDirectory(const Caption: string; const Root: WideString; out Directory: string): Boolean; overload; 2、function SelectDirectory(var Directory: string; Options: TSelectDirOpts; HelpCtx: Longint): Boolean; overload; 注意:使用前請uses FileCtrl; 第1種調(diào)用格式示例為: const sCaption = '文件夾'; //彈出框標題名(非彈出框窗體名) sRoot = ''; //初始文件夾(如'C:\','D:\DownLoad'等, 不存在則從桌面) var sDir: string; begin if SelectDirectory(sCaption, sRoot, sDir) then //已返回所選文件夾路徑給sDir,自行處理 end; 第2種調(diào)用格式示例為: const SELDIRHELP = 1000; var sDir: string; //初始文件夾(如'C:\','D:\DownLoad'等) begin sDir := ''; if SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt], SELDIRHELP) then //已返回所選文件夾路徑給sDir,自行處理 end;
|