Select folder dialog

本文介绍了一种使用Delphi实现的标准文件夹选择对话框的方法。通过提供的Delphi函数GetFolderDialog,可以轻松地让用户选择一个文件夹,并获取所选文件夹的路径。

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)
def OpenFileDialog(self): # 创建QFileDialog,设置初始标题、默认打开路径等信息 default_dir = self.get_prev_path() self.select_shots_dialog = QFileDialog(self, u'Open...', default_dir, '') # 设置为只能选择目录(文件夹) self.select_shots_dialog.setFileMode(QFileDialog.Directory) self.select_shots_dialog.setOption(QFileDialog.DontUseNativeDialog, True) # 设置对话框不使用本地样式 self.select_shots_dialog.setOption(QFileDialog.ShowDirsOnly, True) # 这里再次强调只显示目录,虽然前面设置了选择模式为目录,但这样更明确 # 设置默认文件名,可以根据你的需求修改这里的默认文件名 self.default_file_name = "untitle.mov" current_file_path = cmds.file(q=True, sceneName=True) #if current_file_path != "": # self.default_file_name = (current_file_path.split("/")[-1]).split(".")[0] + ".mov" if current_file_path != "": if self.rename_id == 1: self.default_file_name = (current_file_path.split("/")[-1]).split(".")[0] + ".mov" elif self.rename_id == 2: self.default_file_name = rename_by_shotPath(current_file_path) + ".mov" # self.select_shots_dialog.selectFile(self.default_file_name) file_view = self.select_shots_dialog.findChild(QListView, 'listView') if file_view: file_view.setSelectionMode(QAbstractItemView.SingleSelection) f_tree_view = self.select_shots_dialog.findChild(QTreeView) if f_tree_view: f_tree_view.setSelectionMode(QAbstractItemView.SingleSelection) if self.select_shots_dialog.exec_() == QDialog.Accepted: selected_folder = self.select_shots_dialog.selectedFiles()[0] self.set_prev_path(selected_folder) # final_path = selected_folder + "/" + self.default_file_name if selected_folder.endswith("/") \ # else selected_folder + "/" + self.default_file_name final_path = selected_folder + "/" + self.default_file_name print(final_path) return final_path else: return "",从 file_view = self.select_shots_dialog.findChild(QListView, 'listView')开始逐行分析
11-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值