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 emptyrecycle() 清空回收站

Delphi xe7开发的直接以管理员模式启动cmd命令行界面

Delphi 比较俩组mac地址

Delphi 文件查找记录类型 - tsearchrec

Delphi richedit 加入链接

Delphi fastreport 直接列印

Delphi操作usb摄像头类

Delphi 动态创建一个ipedit控件

Delphi xe ioutils 单元(1): 初识 tdirectory.getfiles

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



打赏

取消

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

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

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

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

评论

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