Windows API 函数SHBrowseForFolder在Delphi中的用法,要求初始定位目录为程序运行目录.(50分)

  • 主题发起人 主题发起人 peng_qs
  • 开始时间 开始时间
查一下SelectDirectory源码,好象是在FileCtrl单元。<br>这个函数是个重载函数,其中一个使用了SHBrowseForFolder,应该完全符合你的要求。
 
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><br>end. <br>
 
接受答案了.
 
后退
顶部