介绍

这是一篇关于从 Delphi 打开 Windows 资源管理器的文章。

打开资源管理器

无论如何,让我们从 Delphi 打开 Explorer。

ShellExecute() API

使用ShellExecute() API 打开资源管理器很容易。

对于 Delphi,将 Winapi.ShellAPI 替换为用途您可以通过添加来使用它。

  ShellExecute(0, 'open', 'explorer.exe', nil, nil, SW_SHOWNORMAL);

常量SW_SHOWNORMALWinapi.Windows 中定义。

资源管理器启动选项

Explorer 具有启动选项,其中一些不再起作用并且已从 Microsoft 的文档中消失。

选项 功能
/n 为默认选择打开一个新的单窗格窗口。这通常是安装 Windows 的驱动器的根目录。
/e 在其默认视图中打开 Windows 资源管理器。
/root,<object> 打开指定对象的窗口视图。
/select,<object> 打开带有选定文件夹、文件或程序的窗口视图。

TExplorer记录

我实现了一个可以使用 Explorer 启动选项打开的 TExporer 记录。

uExplorer.pas
unit uExplorer;

interface

uses
  Winapi.Windows, Winapi.ShellAPI, Dialogs, shlobj;

type
  TExplorerOpenStyle = (eosNone, eosSinglePane, eosDefaultView);
  TExplorer = record
  public
    /// <summary>
    ///   Explorer を開く
    /// </summary>
    /// <param name="Path">
    ///   開く場所。
    /// </param>
    /// <param name="OpenStyle">
    ///   Explorer の表示スタイル。
    ///     eosNone
    ///       指定なし。
    ///     eosSinglePane
    ///       Windows XP だとシングルペインウィンドウで開く。
    ///     eosDefaultView
    ///       エクスプローラーバーの `フォルダ` が有効の状態で開く。
    /// </param>
    /// <param name="RootDir">
    ///   ルートフォルダ。Windows XP だとルートフォルダよりも上の階層に移動する事はできない。
    /// </param>
    /// <param name="SelectObject">
    ///   選択するオブジェクト。オブジェクトが選択された状態で Explorer が開く。
    /// </param>
    /// <returns>
    ///   ShellExecute() API の戻り値
    /// </returns>
    class function Open(Path: string = ''; OpenStyle: TExplorerOpenStyle = eosNone; RootDir: string = ''; SelectObject: string = ''): HINST; static;
  end;

implementation

{ TExplorer }

class function TExplorer.Open(Path: string; OpenStyle: TExplorerOpenStyle; RootDir,
  SelectObject: string): HINST;
var
  Param: string;
  procedure AddDelimiter;
  begin
    if Param <> '' then
      Param := Param + ',';
  end;
begin
  case OpenStyle of
    eosSinglePane:
      Param := '/n';
    eosDefaultView:
      Param := '/e';
  else
    Param := '';
  end;
  if (Path <> '') and (RootDir = '') and (SelectObject = '') then
    case OpenStyle of
      eosSinglePane,
      eosDefaultView:
        begin
          AddDelimiter;
          Param := Param + Path;
        end;
    else
      Param := Path;
    end;
  if RootDir <> '' then
    begin
      AddDelimiter;
      Param := Param + '/root';
      AddDelimiter;
      Param := Param + RootDir;
    end;
  if SelectObject <> '' then
    begin
      AddDelimiter;
      Param := Param + '/select';
      AddDelimiter;
      Param := Param + SelectObject;
    end;
  result := ShellExecute(0, 'open', 'explorer.exe', PChar(Param), nil, SW_SHOWNORMAL);
end;

end.

您可以使用Open() 方法打开资源管理器。

class function TExplorer.Open(Path: string; OpenStyle: TExplorerOpenStyle; RootDir,
  SelectObject: string): HINST;
代码 启动选项 运动
特克斯波尔河笔; (没有任何) クイックアクセス 在 Windows 11 上打开。只需开始Explorer.exe
TExplorer.Open('C:\WORK'); C:\工作 'C:\WORK' 打开
TExplorer.Open('C:\WORK', eosNone); C:\工作 与 'TExplorer.Open('C:\WORK');' 相同
TExplorer.Open('C:\WORK', eosSinglePane); /n,C:\WORK “C:\WORK”在 Windows XP 上的单个窗格窗口中打开
TExplorer.Open('C:\WORK', eosDefaultView); /e,C:\WORK 在 Windows XP 上,“C:\WORK”打开并在资源管理器栏中启用 フォルダ
TExplorer.Open('', eosDefaultView, 'C:\WORK'); /e,/root,C:\WORK 打开“C:\WORK”作为根文件夹。在 Windows XP 中,无法移动到根文件夹之上。
TExplorer.Open('', eosDefaultView, '', 'C:\WORK\TEST.EXE'); /e,/select,C:\WORK\TEST.EXE 打开“C:\WORK”文件夹并在那里选择TEST.EXE

Path,RootDir,SelectObject代表特殊文件夹CLSID也可以指定。如果您指定 CLSID,请在其前面加上两个冒号。

::<CLSID>

例如,以下代码打开桌面文件夹并选择回收站。

TExplorer.Open('', eosDefaultView, '', '::{645FF040-5081-101B-9F08-00AA002F954E}');

实现 Shell() 方法

在资源管理器中或ファイル名を指定して実行壳:可以使用协议。

例如,shell:C:\WORK 将在资源管理器中打开 C:\WORKshell:::{645FF040-5081-101B-9F08-00AA002F954E} 将打开垃圾箱。

Shell() 方法的实现现在看起来像这样。

uExplorer.pas
    /// <summary>
    ///   Explorer を開く
    /// </summary>
    /// <param name="Param">
    ///   shell: コマンドに渡すパラメータ。
    /// </param>
    /// <returns>
    ///   ShellExecute() API の戻り値
    /// </returns>
    class function Shell(Param: string): HINST; static;

...

class function TExplorer.Shell(Param: string): HINST;
begin
  Param := 'shell:' + Param;
  result := ShellExecute(0, 'open', PChar(Param), nil, nil, SW_SHOWNORMAL);
end;

Param地点名称也可以通过。例如,以下代码打开控制面板。

  TExplorer.Shell('ControlPanelFolder');

可以从注册表中的HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\FolderDescriptions 获取位置名称。

实现 Search() 方法

在资源管理器或运行中搜索毫秒:(对于 Vista 及更高版本搜索:您可以使用协议搜索文件。

uExplorer.pas
uses
  ..., System.NetEncoding;

...

    /// <summary>
    ///   Explorer を開く
    /// </summary>
    /// <param name="Location">
    ///   検索する場所。
    /// </param>
    /// <param name="Query">
    ///   検索文字列。
    /// </param>
    /// <param name="DisplayName">
    ///   [省略可能] 検索結果に付ける表示名。
    /// </param>
    /// <returns>
    ///   ShellExecute() API の戻り値
    /// </returns>
    class function Search(Location: string; Query: string; DisplayName: string = ''): HINST; static;

 ...

class function TExplorer.Search(Location, Query, DisplayName: string): HINST;
var
  Param: string;
begin
  Param := 'search-ms:';
  Param := Param + 'query=' + Query + '&';
  Param := Param + 'location=' + TNetEncoding.URL.Encode(Location);
  if DisplayName <> '' then
    Param := Param + '&' + 'displayName=' + Location;
  result := ShellExecute(0, 'open', PChar(Param), nil, nil, SW_SHOWNORMAL);
end;

传递给location= 的字符串是网址编码看来我必须用途,以及使用TNetEncoding.URL.Encode() 方法编码的URL。

以下代码在C:\WORK 中搜索*.pas,并显示名称为ソースファイル 的搜索结果。

  TExplorer.Search('C:\WORK', '*.pas', 'ソースファイル');

它可能无法在 XP 上正常工作(有环境)。

综上所述

我将发布清理后的源代码。

uExplorer.pas
unit uExplorer;

interface

uses
  Winapi.Windows, Winapi.ShellAPI, System.NetEncoding;

type
  TExplorerOpenStyle = (eosNone, eosSinglePane, eosDefaultView);
  TExplorer = record
  public
    /// <summary>
    ///   Explorer を開く
    /// </summary>
    /// <param name="Path">
    ///   開く場所。
    /// </param>
    /// <param name="OpenStyle">
    ///   Explorer の表示スタイル。
    ///     eosNone
    ///       指定なし。
    ///     eosSinglePane
    ///       Windows XP だとシングルペインウィンドウで開く。
    ///     eosDefaultView
    ///       エクスプローラーバーの `フォルダ` が有効の状態で開く。
    /// </param>
    /// <param name="RootDir">
    ///   ルートフォルダ。Windows XP だとルートフォルダよりも上の階層に移動する事はできない。
    /// </param>
    /// <param name="SelectObject">
    ///   選択するオブジェクト。オブジェクトが選択された状態で Explorer が開く。
    /// </param>
    /// <returns>
    ///   ShellExecute() API の戻り値
    /// </returns>
    class function Open(Path: string = ''; OpenStyle: TExplorerOpenStyle = eosNone; RootDir: string = ''; SelectObject: string = ''): HINST; static;
    /// <summary>
    ///   Explorer を開く
    /// </summary>
    /// <param name="Param">
    ///   shell: コマンドに渡すパラメータ。
    /// </param>
    /// <returns>
    ///   ShellExecute() API の戻り値
    /// </returns>
    class function Shell(Param: string): HINST; static;
    /// <summary>
    ///   Explorer を開く
    /// </summary>
    /// <param name="Location">
    ///   検索する場所。
    /// </param>
    /// <param name="Query">
    ///   検索文字列。
    /// </param>
    /// <param name="DisplayName">
    ///   [省略可能] 検索結果に付ける表示名。
    /// </param>
    /// <returns>
    ///   ShellExecute() API の戻り値
    /// </returns>
    class function Search(Location: string; Query: string; DisplayName: string = ''): HINST; static;
  end;

implementation

{ TExplorer }

class function TExplorer.Open(Path: string; OpenStyle: TExplorerOpenStyle; RootDir,
  SelectObject: string): HINST;
var
  Param: string;
  procedure AddDelimiter;
  begin
    if Param <> '' then
      Param := Param + ',';
  end;
begin
  case OpenStyle of
    eosSinglePane:
      Param := '/n';
    eosDefaultView:
      Param := '/e';
  else
    Param := '';
  end;
  if (Path <> '') and (RootDir = '') and (SelectObject = '') then
    case OpenStyle of
      eosSinglePane,
      eosDefaultView:
        begin
          AddDelimiter;
          Param := Param + Path;
        end;
    else
      Param := Path;
    end;
  if RootDir <> '' then
    begin
      AddDelimiter;
      Param := Param + '/root';
      AddDelimiter;
      Param := Param + RootDir;
    end;
  if SelectObject <> '' then
    begin
      AddDelimiter;
      Param := Param + '/select';
      AddDelimiter;
      Param := Param + SelectObject;
    end;
  result := ShellExecute(0, 'open', 'explorer.exe', PChar(Param), nil, SW_SHOWNORMAL);
end;

class function TExplorer.Search(Location, Query, DisplayName: string): HINST;
var
  Param: string;
begin
  Param := 'search-ms:';
  Param := Param + 'query=' + Query + '&';
  Param := Param + 'location=' + TNetEncoding.URL.Encode(Location);
  if DisplayName <> '' then
    Param := Param + '&' + 'displayName=' + Location;
  result := ShellExecute(0, 'open', PChar(Param), nil, nil, SW_SHOWNORMAL);
end;

class function TExplorer.Shell(Param: string): HINST;
begin
  Param := 'shell:' + Param;
  result := ShellExecute(0, 'open', PChar(Param), nil, nil, SW_SHOWNORMAL);
end;

end.

您可能不会经常从应用程序中显示 Explorer 的搜索结果,但您可以打开 Explorer 并选择应用程序输出的文件。

也可以看看:


原创声明:本文系作者授权爱码网发表,未经许可,不得转载;

原文地址:https://www.likecs.com/show-308623630.html

相关文章: