Delphi OpenDialog 不允许用户导航离开初始目录

发布于 2024-09-18 18:18:07 字数 169 浏览 6 评论 0原文

我正在尝试创建一个打开的对话框(在 Windows 7 中),其中用户被限制在初始目录中。在打开的对话框中,我已将 optionsEX 设置为 [ofExNoPlacesBar],这会删除允许他们快速选择文件夹和目录的栏,但用户可以使用面包屑地址工具向上一级并键入不同的目录进入文件名文本框以更改目录。

谢谢

I am trying to create an open dialog (in Windows 7) where the user is confined to the initial directory. On the open dialog I have set the optionsEX to [ofExNoPlacesBar] and that removes the bar that would let them select folders and directories to go to quickly but the user can use the bread crumb address tool to go up a level and type a different directory into the filename text box to change directories.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剩一世无双 2024-09-25 18:18:08

如果您使用的是 Delphi 2009+,则有一个 TFileOpenDialog。使用它,并设置

procedure TForm3.FileOpenDialog1FolderChange(Sender: TObject);
begin
  FInitiated := true;
end;

procedure TForm3.FileOpenDialog1FolderChanging(Sender: TObject;
  var CanChange: Boolean);
begin
  CanChange := not FInitiated;
end;

procedure TForm3.btnOpenClick(Sender: TObject);
begin
  FInitiated := false;
  FileOpenDialog1.DefaultFolder := 'C:\MyFolder\';
  FileOpenDialog1.Execute;
end;

其中

var
  FInitiated: boolean;

(请注意,每个 TFileOpenDialog 应该正好有一个 FInitiated。因此,如果 FileOpenDialog的私有成员>TForm3,让 FInitiated 也成为 TForm3 的私有成员。)

为了改善用户体验,您可能会使用

procedure TForm3.FileOpenDialog1FolderChanging(Sender: TObject;
  var CanChange: Boolean);
begin
  CanChange := not FInitiated;
  if not CanChange then beep;
end;

procedure TForm3.FileOpenDialog1FolderChanging(Sender: TObject;
  var CanChange: Boolean);
begin
  CanChange := not FInitiated;
  if not CanChange then
    MessageBox(Handle, PChar('Directory selection is not allowed.'), PChar(Caption), MB_ICONINFORMATION);
end;

If you are using Delphi 2009+, there is a TFileOpenDialog. Use this, and set

procedure TForm3.FileOpenDialog1FolderChange(Sender: TObject);
begin
  FInitiated := true;
end;

procedure TForm3.FileOpenDialog1FolderChanging(Sender: TObject;
  var CanChange: Boolean);
begin
  CanChange := not FInitiated;
end;

procedure TForm3.btnOpenClick(Sender: TObject);
begin
  FInitiated := false;
  FileOpenDialog1.DefaultFolder := 'C:\MyFolder\';
  FileOpenDialog1.Execute;
end;

where

var
  FInitiated: boolean;

(Notice that there should be exactly one FInitiated per TFileOpenDialog. So, if FileOpenDialog is a private member of TForm3, let FInitiated be a private member of TForm3 as well.)

To improve the user experience, you will probably use

procedure TForm3.FileOpenDialog1FolderChanging(Sender: TObject;
  var CanChange: Boolean);
begin
  CanChange := not FInitiated;
  if not CanChange then beep;
end;

or

procedure TForm3.FileOpenDialog1FolderChanging(Sender: TObject;
  var CanChange: Boolean);
begin
  CanChange := not FInitiated;
  if not CanChange then
    MessageBox(Handle, PChar('Directory selection is not allowed.'), PChar(Caption), MB_ICONINFORMATION);
end;
风渺 2024-09-25 18:18:08

使用不同的打开对话框(自己制作一个没有文件夹导航,只有文件列表框的表单),或者只是审核与初始目录不匹配的路径并拒绝实际打开文件。

Use a different open dialog (make a form yourself with no folder navigation, only a file list box), or simply audit for a path not matching the initial dir and refuse to actually open the file.

秉烛思 2024-09-25 18:18:08

“FileOpenDialog”有一个 OnFolderChanging 类型为 TFileDialogFolderChangingEvent 具有布尔 CanChange 参数。我希望将此参数设置为 false 可以达到目的。

编辑:

根据雷米的评论使用示例(如果我理解正确的话);

procedure TForm1.FileOpenDialog1FolderChanging(Sender: TObject;
  var CanChange: Boolean);
var
  Dlg: TFileOpenDialog;
  DefFolder: IShellItem;
  iOrder: Integer;
begin
  CanChange := False;
  Dlg := Sender as TFileOpenDialog;
  if Succeeded(SHCreateItemFromParsingName(PWideChar(WideString(Dlg.DefaultFolder)), nil, IShellItem, DefFolder)) then
  try
    CanChange := Dlg.ShellItem.Compare(DefFolder, SICHINT_ALLFIELDS, iOrder) = S_OK;
  finally
    DefFolder := nil;
  end;
end;

下面的方法也有效,但更容易受到路径变化的影响(请参阅下面 Andreas 的评论);

procedure TForm1.FileOpenDialog1FolderChanging(Sender: TObject;
  var CanChange: Boolean);
begin
  CanChange := SameFileName(TFileOpenDialog(Sender).FileName,
                            TFileOpenDialog(Sender).DefaultFolder);
end;

The 'FileOpenDialog' has an OnFolderChanging event of type TFileDialogFolderChangingEvent which have a boolean CanChange parameter. I'd expect setting this parameter to false would serve the purpose.

edit:

Example usage as per Remy's comments (if I understood correctly);

procedure TForm1.FileOpenDialog1FolderChanging(Sender: TObject;
  var CanChange: Boolean);
var
  Dlg: TFileOpenDialog;
  DefFolder: IShellItem;
  iOrder: Integer;
begin
  CanChange := False;
  Dlg := Sender as TFileOpenDialog;
  if Succeeded(SHCreateItemFromParsingName(PWideChar(WideString(Dlg.DefaultFolder)), nil, IShellItem, DefFolder)) then
  try
    CanChange := Dlg.ShellItem.Compare(DefFolder, SICHINT_ALLFIELDS, iOrder) = S_OK;
  finally
    DefFolder := nil;
  end;
end;

The below also works but more vulnerable to path variations (see Andreas' comments below);

procedure TForm1.FileOpenDialog1FolderChanging(Sender: TObject;
  var CanChange: Boolean);
begin
  CanChange := SameFileName(TFileOpenDialog(Sender).FileName,
                            TFileOpenDialog(Sender).DefaultFolder);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文