使用SelectDirectory函数时的一点疑惑?(100分)

  • 主题发起人 主题发起人 wisewang
  • 开始时间 开始时间
W

wisewang

Unregistered / Unconfirmed
GUEST, unregistred user!
[?]使用SelectDirectory函数时怎样使浏览文件夹窗口显示于桌面中间,能否讲一下关于SelectDirectory函数的详细用法?谢谢!!![?]
 
Delphi本身的那个不能让其位于屏幕中间,可以使用下面的,其实就是调用API来实现具体功能。<br>句柄使用Form的,可以使其显示在所有者中心。能够返回所选择的目录,以及添加的目录,但是功能似乎还不太完善。<br><br>uses shlobj;<br><br>function SelectDirectory1(handle: hwnd; const Caption: string; const Root: WideString; out Directory: string): Boolean;<br>var<br>&nbsp; lpbi: _browseinfo;<br>&nbsp; buf: array[0..MAX_PATH] of char;<br>&nbsp; id: ishellfolder;<br>&nbsp; eaten, att: cardinal;<br>&nbsp; rt: pitemidlist;<br>&nbsp; initdir: pwidechar;<br>begin<br>&nbsp; result := false;<br>&nbsp; lpbi.hwndOwner := handle;<br>&nbsp; lpbi.lpfn := nil;<br>&nbsp; lpbi.lpszTitle := pchar(caption);<br>&nbsp; lpbi.ulFlags := BIF_RETURNONLYFSDIRS + 16;<br>&nbsp; SHGetDesktopFolder(id);<br>&nbsp; initdir := pwchar(root);<br>&nbsp; id.ParseDisplayName(0, nil, initdir, eaten, rt, att);<br>&nbsp; lpbi.pidlRoot := rt;<br>&nbsp; getmem(lpbi.pszDisplayName, MAX_PATH);<br>&nbsp; try<br>&nbsp; &nbsp; result := shgetpathfromidlist(shbrowseforfolder(lpbi), buf);<br>&nbsp; except<br>&nbsp; &nbsp; freemem(lpbi.pszDisplayName);<br>&nbsp; end;<br>&nbsp; if result then directory := buf;<br>end;<br><br>-----------------------------------------------------------------------------------------<br>据说可以设置初始目录,没有具体实验<br>function SelectDirectory(const Caption: string; const Root: WideString;<br>&nbsp; var Directory: string): Boolean;<br>var<br>&nbsp; WindowList: Pointer;<br>&nbsp; BrowseInfo: TBrowseInfo;<br>&nbsp; Buffer: PChar;<br>&nbsp; RootItemIDList, ItemIDList: PItemIDList;<br>&nbsp; ShellMalloc: IMalloc;<br>&nbsp; IDesktopFolder: IShellFolder;<br>&nbsp; Eaten, Flags: LongWord;<br>begin<br>&nbsp; Result := False;<br>&nbsp; if not DirectoryExists(Directory) then<br>&nbsp; &nbsp; Directory := '';<br>&nbsp; FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);<br>&nbsp; if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc &lt;&gt; nil) then<br>&nbsp; begin<br>&nbsp; &nbsp; Buffer := ShellMalloc.Alloc(MAX_PATH);<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; RootItemIDList := nil;<br>&nbsp; &nbsp; &nbsp; if Root &lt;&gt; '' then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; SHGetDesktopFolder(IDesktopFolder);<br>&nbsp; &nbsp; &nbsp; &nbsp; IDesktopFolder.ParseDisplayName(Application.Handle, nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; POleStr(Root), Eaten, RootItemIDList, Flags);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; with BrowseInfo do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; hwndOwner := Application.Handle;<br>&nbsp; &nbsp; &nbsp; &nbsp; pidlRoot := RootItemIDList;<br>&nbsp; &nbsp; &nbsp; &nbsp; pszDisplayName := Buffer;<br>&nbsp; &nbsp; &nbsp; &nbsp; lpszTitle := PChar(Caption);<br>&nbsp; &nbsp; &nbsp; &nbsp; ulFlags := BIF_RETURNONLYFSDIRS;<br>&nbsp; &nbsp; &nbsp; &nbsp; if Directory &lt;&gt; '' then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpfn := SelectDirCB;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lParam := Integer(PChar(Directory));<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; WindowList := DisableTaskWindows(0);<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; ItemIDList := ShBrowseForFolder(BrowseInfo);<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; EnableTaskWindows(WindowList);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; Result := ItemIDList &lt;&gt; nil;<br>&nbsp; &nbsp; &nbsp; if Result then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ShGetPathFromIDList(ItemIDList, Buffer);<br>&nbsp; &nbsp; &nbsp; &nbsp; ShellMalloc.Free(ItemIDList);<br>&nbsp; &nbsp; &nbsp; &nbsp; Directory := Buffer;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; ShellMalloc.Free(Buffer);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>
 
谢谢yzhshi!<br>&nbsp; &nbsp; 请问使用SelectDirectory1函数时:1.怎样不显示文件夹名的编辑框;<br>2.使用原SelectDirectory函数时刚开始显示窗口时确定按钮是不可选的,选择文件夹<br>后就可用了,请问如何实现;3.用原SelectDirectory函数显示是从桌面开始,而本函<br>数是从我的电脑开始,怎样从桌面开始。<br>&nbsp; &nbsp; 使用function SelectDirectory(const Caption: string; const Root: WideString;<br>&nbsp; var Directory: string): Boolean;时应包括哪些单元才能编译通过。
 
&nbsp; &nbsp;对不起,我没有细看第二个函数就是SelectDirectory的原型,提问有误,见谅。<br>还有去除编辑框和确定按钮问题我已知道了,是把lpbi.ulFlags := BIF_RETURNONLYFSDIRS + 16;<br>换成lpbi.ulFlags := &nbsp;BIF_RETURNFSANCESTORS;即可。<br>&nbsp; &nbsp; 现在的问题是:1.用原SelectDirectory函数显示是从桌面开始,而本函数是从我的电脑开始,<br>请问怎样从桌面开始;2.使用您提供的函数显示时不是在屏幕中间,如何才能在屏幕中间显示。<br>
 
SelectDirectory1(GetDesktopWindow,'aaa','Desktop',bb);<br>设置参数为:Desktop就可以了。
 
这个绝对比SelectDirectory好用!<br>在uses中加入shlobj<br>var<br>&nbsp; tmp: string;<br>&nbsp; udtBI: TBrowseInfo;<br>&nbsp; lpIDList: Pointer;<br>&nbsp; dn: PChar;<br>begin<br>&nbsp; with udtBI do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; hWndOwner:=handle;<br>&nbsp; &nbsp; &nbsp; iImage:=0;<br>&nbsp; &nbsp; &nbsp; lParam:=0;<br>&nbsp; &nbsp; &nbsp; lpfn:=nil;<br>&nbsp; &nbsp; &nbsp; lpszTitle:='打开文件';<br>&nbsp; &nbsp; &nbsp; pidlRoot:=nil;<br>&nbsp; &nbsp; &nbsp; ulFlags:=BIF_ReturnOnlyFSdirs;<br>&nbsp; &nbsp; end;<br>&nbsp; lpIDList:=SHBrowseForFolder(udtBI);<br>&nbsp; if assigned(lpIDList) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; SetLength(tmp,100);<br>&nbsp; &nbsp; &nbsp; SHGetPathFromIDList(lpIDList,PChar(tmp));<br>&nbsp; &nbsp; &nbsp; Edit1.text:=tmp;<br>&nbsp; &nbsp; end;<br>end;
 
yzhshi大侠,您好!<br>&nbsp; &nbsp; 用了SelectDirectory1(GetDesktopWindow,'aaa','Desktop',bb)可以从桌面显示文件<br>夹了,但显示的窗口跑到左上角了,怎么解决,盼复!<br><br>PFans大侠,您好!<br>&nbsp; &nbsp; 您提供的函数好像有问题,具体为:选择根目录时报错;选择深层目录(第二层开始)<br>时报错,希望您能予以验证。<br><br>我的环境为WIN2000专业版(SP2),DELPHI 6(Update Pack 2)。
 
接受答案了.
 
后退
顶部