簡體   English   中英

如何更改現有Windows資源管理器窗口的路徑?

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

我有一個打開的Windows資源管理器窗口的句柄。

如何向其發送命令以更改路徑
例如:m:\\ programs到d:\\ programs


例

直到現在我使用的是ShellExecute()但它會打開一個新窗口。 這不好(用戶體驗)。

以下BrowseToFolder函數將給定AHandle句柄(如果存在)的Windows資源管理器的現有實例導航到AFolderPath文件夾(如果存在)。 如果您不指定第二個參數,則應該采用最頂層的窗口進行導航(或者至少文檔聲稱:現實似乎采用最舊的現有窗口)。 如果導航成功,則函數返回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;

以下是示例用法:

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;

這是一個complete testing projectthe blog post ,我從中獲得了靈感。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM