如何使选择文件的对话框的根目录是网上邻居(150分)

  • 主题发起人 主题发起人 bluerain
  • 开始时间 开始时间
B

bluerain

Unregistered / Unconfirmed
GUEST, unregistred user!
我需要使用选择文件的对话框,但是选择的根目录是网上邻居,而不出现本机的硬盘<br>路径,例如c:,d:或桌面等。<br>急,急。各位大侠帮帮忙。
 
请看一下代码<br><br>LPITEMIDLIST itemIdList=(LPITEMIDLIST)CoTaskMemAlloc(sizeof(ITEMIDLIST));<br> LPITEMIDLIST pItem;<br>&nbsp; &nbsp; BROWSEINFO bi;<br> ZeroMemory(&amp;bi,sizeof(bi));<br> TCHAR pszDisplayname[MAX_PATH];<br> TCHAR pszPathName[MAX_PATH];<br> SHGetSpecialFolderLocation(GetSafeHwnd(),CSIDL_NETWORK,&amp;itemIdList);<br> bi.hwndOwner=GetSafeHwnd();<br> bi.pidlRoot=itemIdList;<br> bi.lpszTitle=_T("Select a Folder");<br> bi.ulFlags=0;<br> <br>&nbsp; &nbsp; bi.pszDisplayName=pszDisplayname;<br> pItem=SHBrowseForFolder(&amp;bi);<br> if(NULL!=pItem)<br> {<br>&nbsp; &nbsp; &nbsp; SHGetPathFromIDList(pItem,pszPathName);<br> &nbsp;AfxMessageBox(pszPathName);<br> &nbsp;CoTaskMemFree(pItem);<br> }<br> CoTaskMemFree(itemIdList);
 
研究一下 &nbsp;Delphi 运行时间库中的一个 函数SelectDirectory 并做一些小小的改动<br>就可以了。
 
function SelectDirectory(const Caption: string;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; if SHGetSpecialFolderLocation(Application.Handle, CSIDL_NETWORK, RootItemIDList) &lt;&gt; S_OK then<br>&nbsp; &nbsp; &nbsp; &nbsp;Abort;<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>从 Delphi 的 SelectDirectory 改了一点,用 SHBrowseFolder 只能选目录,计算机和打印<br>机等,不能选文件的。
 
多人接受答案了。
 
后退
顶部