Select folder dialog

來源:互聯網
上載者:User

Select folder dialog

Yesterday I posted how to display standard dialog for files (open/save). Today I want to show how you may use stamndard dialog when user must select a folder/directory.

Use the next function:

uses SysUtils, ShlObj;function BrowseCallbackProc(hwnd: HWND; uMsg: UINT; lParam: LPARAM; lpData: LPARAM): Integer; stdcall;begin  if (uMsg = BFFM_INITIALIZED) then    SendMessage(hwnd, BFFM_SETSELECTION, 1, lpData);  BrowseCallbackProc := 0;end;function GetFolderDialog(Handle: Integer; Caption: string; var strFolder: string): Boolean;const  BIF_STATUSTEXT           = $0004;  BIF_NEWDIALOGSTYLE       = $0040;  BIF_RETURNONLYFSDIRS     = $0080;  BIF_SHAREABLE            = $0100;  BIF_USENEWUI             = BIF_EDITBOX or BIF_NEWDIALOGSTYLE;var  BrowseInfo: TBrowseInfo;  ItemIDList: PItemIDList;  JtemIDList: PItemIDList;  Path: PAnsiChar;begin  Result := False;  Path := StrAlloc(MAX_PATH);  SHGetSpecialFolderLocation(Handle, CSIDL_DRIVES, JtemIDList);  with BrowseInfo do  begin    hwndOwner := GetActiveWindow;    pidlRoot := JtemIDList;    SHGetSpecialFolderLocation(hwndOwner, CSIDL_DRIVES, JtemIDList);    { return display name of item selected }    pszDisplayName := StrAlloc(MAX_PATH);    { set the title of dialog }    lpszTitle := PChar(Caption);//'Select the folder';    { flags that control the return stuff }    lpfn := @BrowseCallbackProc;    { extra info that's passed back in callbacks }    lParam := LongInt(PChar(strFolder));  end;  ItemIDList := SHBrowseForFolder(BrowseInfo);  if (ItemIDList <> nil) then    if SHGetPathFromIDList(ItemIDList, Path) then    begin      strFolder := Path;      Result := True    end;end;

For example,

s := 'c:/Arch';if GetFolderDialog(Application.Handle, 'Select a folder', s) then  ShowMessage('User selected this folder: ' + s)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.