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 动态加载删除字体

Delphi fdconnection查看所有表包含用户表和系统表

Delphi图片格式判断

Delphi键盘按键伪码多类型

Delphi 使用泛型的 tarray 从动态数组中查找指定元素

Delphi xe8 numberbox使用方法及存在的问题

Delphi 系统appdata local 路径

Delphi 获取listbox1多选的值

Delphi 获取路径分隔符

Delphi 字符串加单引号

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



打赏

取消

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

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

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

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

评论

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