如何设置SHBrowseForFolder的默认路径(50分)

  • 主题发起人 主题发起人 陈健松
  • 开始时间 开始时间

陈健松

Unregistered / Unconfirmed
GUEST, unregistred user!
function SelectDirectory(handle:hwnd;const Caption: string; const Root: WideString;out Directory: string): Boolean;<br>var<br>&nbsp; lpbi:_BrowseInfoA;<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>&nbsp; p: PItemIDList;<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>如何设置其默认路径。
 
unit Unit1; <br>interface <br>uses <br>  ……shlobj,ActiveX; <br>  ……<br><br>var<br>   Form1: TForm1; <br>   Path: string;   //起始路径<br><br>implementation <br><br>{$R *.DFM} <br><br>function BrowseCallbackProc(hwnd: HWND;uMsg: UINT;lParam: Cardinal;lpData: Cardinal): integer; stdcall; <br>begin <br>  if uMsg=BFFM_INITIALIZED then <br>    result :=SendMessage(Hwnd,BFFM_SETSELECTION,Ord(TRUE),Longint(PChar(Path))) <br>  else <br>    result :=1 <br>end; <br><br>function SelDir(const Caption: string; const Root: WideString; out Directory: string): Boolean; <br>var <br>  WindowList: Pointer; <br>  BrowseInfo: TBrowseInfo; <br>  Buffer: PChar; <br>  RootItemIDList, ItemIDList: PItemIDList; <br>  ShellMalloc: IMalloc; <br>  IDesktopFolder: IShellFolder; <br>  Eaten, Flags: LongWord; <br>begin <br>  Result := False; <br>  Directory := ''; <br>  FillChar(BrowseInfo, SizeOf(BrowseInfo), 0); <br>  if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc &lt;&gt; nil) then <br>  begin <br>    Buffer := ShellMalloc.Alloc(MAX_PATH); <br>    try <br>      RootItemIDList := nil; <br>      if Root &lt;&gt; '' then begin <br>        SHGetDesktopFolder(IDesktopFolder); <br>        IDesktopFolder.ParseDisplayName(Application.Handle, nil, POleStr(Root), Eaten,           RootItemIDList, Flags); <br>      end; <br>      with BrowseInfo do begin <br>        hwndOwner := Application.Handle; <br>        pidlRoot := RootItemIDList; <br>        pszDisplayName := Buffer; <br>        lpszTitle := PChar(Caption); <br>        ulFlags := BIF_RETURNONLYFSDIRS; <br>        lpfn :=@BrowseCallbackProc; <br>        lParam :=BFFM_INITIALIZED; <br>      end; <br>      WindowList := DisableTaskWindows(0); <br>      try <br>        ItemIDList := ShBrowseForFolder(BrowseInfo); <br>      finally <br>        EnableTaskWindows(WindowList); <br>      end; <br>      Result := ItemIDList &lt;&gt; nil; <br>      if Result then begin <br>        ShGetPathFromIDList(ItemIDList, Buffer); <br>        ShellMalloc.Free(ItemIDList); <br>        Directory := Buffer; <br>      end; <br>    finally <br>      ShellMalloc.Free(Buffer); <br>    end; <br>  end; <br>end; <br><br>procedure TForm1.SpeedButton1Click(Sender: TObject); <br>var <br>  Path1: string; <br>begin <br>  Path :=Edit1.Text; <br>  SelDir('SelectDirectory Sample','',Path1); <br>  Edit1.Text :=Path1 <br>end; <br>end.
 
谢谢,先试一下
 
接受答案了.
 
后退
顶部