简体   繁体   English

如何更改现有Windows资源管理器窗口的路径?

[英]How to change path of an existing Windows Explorer window?

I have the handle of an opened Windows Explorer window. 我有一个打开的Windows资源管理器窗口的句柄。

How can I send a command to it in order to change the path from 如何向其发送命令以更改路径
example: m:\\programs to d:\\programs . 例如:m:\\ programs到d:\\ programs


例

Till now I was using ShellExecute() but it opens a new window. 直到现在我使用的是ShellExecute()但它会打开一个新窗口。 This is not good (user experience). 这不好(用户体验)。

The following BrowseToFolder function navigates the existing instance of a Windows Explorer of the given AHandle handle (if exists) to a AFolderPath folder (if exists). 以下BrowseToFolder函数将给定AHandle句柄(如果存在)的Windows资源管理器的现有实例导航到AFolderPath文件夹(如果存在)。 If you won't specify the second parameter, the topmost window should be taken to navigate (or at least the documentation claims that; reality seems to take the oldest existing window). 如果您不指定第二个参数,则应该采用最顶层的窗口进行导航(或者至少文档声称:现实似乎采用最旧的现有窗口)。 The function returns True, if the navigation has been successful, False otherwise: 如果导航成功,则函数返回True,否则返回False:

uses
  ActiveX, ShlObj, ShellAPI, SHDocVw;

const
  IID_IServiceProvider: TGUID = '{6D5140C1-7436-11CE-8034-00AA006009FA}';
  SID_STopLevelBrowser: TGUID = '{4C96BE40-915C-11CF-99D3-00AA004AE837}';

function GetItemIDListFromPath(const AFolderPath: WideString): PItemIDList;
var
  Count: ULONG;
  Attributes: ULONG;
  ShellFolder: IShellFolder;
begin
  Result := nil;
  if Succeeded(SHGetDesktopFolder(ShellFolder)) then
  begin
    Count := 0;
    if Failed(ShellFolder.ParseDisplayName(0, nil, PWideChar(AFolderPath),
      Count, Result, Attributes))
    then
      Result := nil;
  end;
end;

function BrowseToFolder(const AFolderPath: WideString;
  AHandle: HWND = HWND_TOPMOST): Boolean;
var
  I: Integer;
  WndIface: IDispatch;
  ItemIDList: PItemIDList;
  ShellBrowser: IShellBrowser;
  ShellWindows: IShellWindows;
  WebBrowserApp: IWebBrowserApp;
  ServiceProvider: IServiceProvider;
begin
  Result := False;

  if Succeeded(CoCreateInstance(CLASS_ShellWindows, nil, CLSCTX_LOCAL_SERVER,
    IID_IShellWindows, ShellWindows)) then
  begin
    for I := 0 to ShellWindows.Count - 1 do
    begin
      if (AHandle <> HWND_TOPMOST) then
        WndIface := ShellWindows.Item(VarAsType(I, VT_I4))
      else
        WndIface := ShellWindows.Item(VarAsType(SWC_EXPLORER, VT_UI4));

      if Succeeded(WndIface.QueryInterface(IID_IWebBrowserApp,
        WebBrowserApp)) then
      begin
        if (AHandle = HWND_TOPMOST) or (WebBrowserApp.HWnd = AHandle) then
        begin
          if Succeeded(WebBrowserApp.QueryInterface(IID_IServiceProvider,
            ServiceProvider)) then
          begin
            if Succeeded(ServiceProvider.QueryService(SID_STopLevelBrowser,
              IID_IShellBrowser, ShellBrowser)) then
            begin
              ItemIDList := GetItemIDListFromPath(AFolderPath);
              Result := Succeeded(ShellBrowser.BrowseObject(ItemIDList,
                SBSP_SAMEBROWSER or SBSP_ABSOLUTE));
            end;
          end;
          Break;
        end;
      end;
    end;
  end;
end;

Here is the example usage: 以下是示例用法:

procedure TForm1.Button1Click(Sender: TObject);
var
  ExplorerHandle: HWND;
begin
  ExplorerHandle := 123456;

  if not BrowseToFolder('c:\Windows\System32\', ExplorerHandle) then
    ShowMessage('Navigation to a folder failed!')
  else
    ShowMessage('Navigation to a folder succeeded!');
end;

Here is a complete testing project and the blog post from which I've taken the inspiration. 这是一个complete testing projectthe blog post ,我从中获得了灵感。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Windows资源管理器捕获文件夹路径 - Capturing folder path from Windows Explorer 在 Windows 7 64 位上运行的 Delphi 6 中找不到代码资源管理器 Window。 怎么找回来? - Can't find Code Explorer Window in Delphi 6 running on Windows 7 64-bit. How to get it back? Windows资源管理器在复制时重命名文件并找到现有文件 - Windows Explorer renames files when copying and an existing file is found 有没有办法在Windows 7中的Windows资源管理器中更改项目的文本背景颜色? - Is there a way to change text background color of the items in Windows Explorer in Windows 7? 如何永久终止Windows资源管理器(“explorer.exe”进程)? - How to permanently terminate Windows Explorer (the “explorer.exe” process)? 如何在Windows资源管理器中获取Delphi中的排序顺序? - How to get the sort order in Delphi as in Windows Explorer? 如何在Windows Vista和7中检索Windows资源管理器使用的文件预览? - How to retrieve the file previews used by windows explorer in Windows vista and seven? 如何向Windows资源管理器添加按钮。 (不适用于IE) - How can I add a button to Windows Explorer. (not to IE) 如何在delphi中显示像windows资源管理器一样的文件缩略图? - how to show the files thumbnails like windows explorer in delphi? 如何防止 Windows 资源管理器干扰删除文件夹? - How do I keep Windows Explorer from interfering with deleting a folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM