Delphi 选择文件夹对话框 (有新建文件夹按钮)修正版


本文整理自网络,侵删。

  

其他网站也有这份代码,不过有问题,我修改后发表上来.

uses

  ShlObj,ActiveX;


function SelectDirCB(Wnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer stdcall;

begin

  if (uMsg = BFFM_INITIALIZED) and (lpData <> 0) then

   SendMessage(Wnd, BFFM_SETSELECTION, Integer(True), lpdata);

  result := 0;

end;



function SelectDirectory(const Caption: string; const Root: WideString;

  var Directory: string): Boolean;

var

  WindowList: Pointer;

  BrowseInfo: TBrowseInfo;

  Buffer: PChar;

  OldErrorMode: Cardinal;

  RootItemIDList, ItemIDList: PItemIDList;

  ShellMalloc: IMalloc;

  IDesktopFolder: IShellFolder;

  Eaten, Flags: LongWord;

begin

  Result := False;

  if not DirectoryExists(Directory) then

    Directory := '';

  FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);

  if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then

  begin

    Buffer := ShellMalloc.Alloc(MAX_PATH);

    try

     RootItemIDList := nil;

     if Root <> '' then

     begin

       SHGetDesktopFolder(IDesktopFolder);

       IDesktopFolder.ParseDisplayName(Application.Handle, nil,

         POleStr(Root), Eaten, RootItemIDList, Flags);

     end;

     with BrowseInfo do

     begin

       hwndOwner := Application.Handle;

       pidlRoot := RootItemIDList;

       pszDisplayName := Buffer;

       lpszTitle := PChar(Caption);

       ulFlags := BIF_RETURNONLYFSDIRS or BIF_NEWDIALOGSTYLE;

       if Directory <> '' then

       begin

         lpfn := SelectDirCB;

         lParam := Integer(PChar(Directory));

       end;

     end;

     WindowList := DisableTaskWindows(0);

     OldErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);

     try

       ItemIDList := ShBrowseForFolder(BrowseInfo);

     finally

       SetErrorMode(OldErrorMode);

       EnableTaskWindows(WindowList);

     end;

     Result :=  ItemIDList <> nil;

     if Result then

     begin

       ShGetPathFromIDList(ItemIDList, Buffer);

       ShellMalloc.Free(ItemIDList);

       Directory := Buffer;

     end;

   finally

     ShellMalloc.Free(Buffer);

    end;

  end;

end;



用法:

procedure TForm1.Button6Click(Sender: TObject);

var

  strPath: string; // 用户选定后的目录

begin

  strPath := '';

  if SelectDirectory('请选择文件目录路径', '', strPath) then

  begin

  LabeledEdit1Savepath.Text := strPath;

  end;

end;

相关阅读 >>

Delphi执行cmd命令和bat文件

Delphi中判断某个文件是否已经打开

Delphi 查找一个特定的exe是否在内存中运行

Delphi 学习 sql 语句 - insert、update、delete

elphi中取出字符串中指定位置的几个字符

Delphi 2007 使用shelltreeview控件

Delphi中获取客户区坐标

Delphi varchar和nvarchar区别

Delphi treeview基本用法

Delphi concat 连接两个或多个字符串为一个字符串

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...