SelectDirectory使用方法以及EnableTaskWindows

格式

Delphi syntax:

On Windows:

  function SelectDirectory(const Caption: string; const Root: WideString; out Directory: string): Boolean;

On Linux:

  function SelectDirectory(const Caption: WideString; const Root: string; var Directory: string; ShowHidden: Boolean = False): Boolean;

 

SelectDirectory弹出对话框让用户选择目录,不改变目录的键值,在linux和windows中格式有所不同,

caption标题是长字符串

const Root指定浏览的根目录

out Directory返回所选目录

ShowHidden标识所选目录是否显示隐藏子目录

例:

uses FileCtrl;

 

const

  SELDIRHELP = 1000;

procedure TForm1.Button1Click(Sender: TObject);

var

  Dir: string;

begin

  Dir := 'C:\MYDIR';

  if SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP) then

    Label1.Caption := Dir;

end;

 

由于显示的对话框不是居中显示 重新定义函数在程序中

uses  

      ShlObj,   ActiveX;   //必须的控件包

   

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

      OwnerWindow:   THandle;   out   Directory:   string):   Boolean;  

  var  

      WindowList:   Pointer;  

      BrowseInfo:   TBrowseInfo;  

      Buffer:   PChar;  

      RootItemIDList,   ItemIDList:   PItemIDList;  

      ShellMalloc:   IMalloc;  

      IDesktopFolder:   IShellFolder;  

      Eaten,   Flags:   LongWord;  

  begin  

      Result   :=   False;  

      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   :=   OwnerWindow;  

                  pidlRoot   :=   RootItemIDList;  

                  pszDisplayName   :=   Buffer;  

                  lpszTitle   :=   PChar(Caption);  

                  ulFlags   :=   BIF_RETURNONLYFSDIRS;  

              end;  

              WindowList   :=   DisableTaskWindows(0);  

              try  

                  ItemIDList   :=   ShBrowseForFolder(BrowseInfo);  

              finally  

                  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.Button1Click(Sender:   TObject);  

  var  

      vDirectory:   string;

  begin  

      SelectDirectory('Select   Path',   '',   Handle,   vDirectory); //此处handle即是窗口位置句柄

  end;  

http://www.cnblogs.com/dashan9zj/archive/2009/01/19/1378289.html

posted @   findumars  Views(1783)  Comments(0Edit  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2014-12-01 市场哲学
2014-12-01 Delphi是座宝山,有待挖掘
点击右上角即可分享
微信分享提示