给一个实现类似SelectDirectory功能的函数. (20分)

  • 主题发起人 主题发起人 jdelphi
  • 开始时间 开始时间
J

jdelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
SelectDirectory总是显示在右下角,其他函数又无法包括网上邻居,无法实现SelectDirectory一样的效果
 
var str:string;<br>begin<br>&nbsp;SelectDirectory(str,[sdAllowCreate,sdPerformCreate,sdPrompt],0);<br>end;<br>注意:<br>&nbsp;SelectDirectory()函數,有兩種參數結構,它是重載函數。<br>&nbsp;用示例上一種,就不會出現在右下角。
 
不好意思,沒看到你需要找網上鄰居,<br>當我沒說。
 
这个:<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;
 
yzhshi:没有网上邻居。
 
uses shlobj,ActiveX;//注意,一定要use这两个!!!!<br>//函数功能:获取网上邻居目录<br>//参数: &nbsp;caption-对话框标题,Directory,选择的目录,Result:是否有选择;<br>//author:雪鹰<br>function SelectNetDir(const Caption: string;<br>&nbsp; out 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; 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; SHGetSpecialFolderLocation(Application.handle,CSIDL_NETWORK,RootItemIDList);<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; 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 := &nbsp;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><br><br>example:<br>&nbsp; if selectNetDir('test',od) then<br>&nbsp; &nbsp; showmessage(od)<br>
 
可以通过SHBrowseForFolder函数实现你的需求,附示例代码:<br>const<br>&nbsp; MAX_PATH=256;<br>var<br>&nbsp; TitleName:string;<br>&nbsp; lpItemID : PItemIDList;<br>&nbsp; BrowseInfo : TBrowseInfo;<br>&nbsp; DisplayName : array[0..MAX_PATH] of char;<br>&nbsp; TempPath : array[0..MAX_PATH] of char;<br><br>begin<br>&nbsp; FillChar(BrowseInfo, sizeof(TBrowseInfo), #0);<br>&nbsp; BrowseInfo.hwndOwner := FrmPathSetup.Handle;<br>&nbsp; BrowseInfo.pszDisplayName := @DisplayName;<br>&nbsp; TitleName := '请选择素材库路径';<br>&nbsp; BrowseInfo.lpszTitle := PChar(TitleName);<br>&nbsp; BrowseInfo.ulFlags := BIF_RETURNONLYFSDIRS;<br>&nbsp; lpItemID := SHBrowseForFolder(BrowseInfo);<br>&nbsp; if lpItemId &lt;&gt; nil then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; SHGetPathFromIDList(lpItemID, TempPath);<br>&nbsp; &nbsp; &nbsp; etPath.Text:=TempPath;<br>&nbsp; &nbsp; &nbsp; GlobalFreePtr(lpItemID);<br>&nbsp; &nbsp; end;<br>end;
 
在uses中加入shlobj<br><br>procedure TForm1.Button1Click(Sender: TObject);<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;
 
好久没看这个帖子了,其实我的可以,下面语句就出现晚上邻居了。<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; aa: String;<br>begin<br>&nbsp; SelectDirectory1(Handle,'aa','Desktop',aa);<br>end;<br>
 
接受答案了.
 
后退
顶部