SelectDirectory使用方法

格式

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;  

转载于:https://www.cnblogs.com/dashan9zj/archive/2009/01/19/1378289.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值