检查Windows资源管理器是否已在给定路径上打开 [英] Check if windows explorer already opened on given path

查看:168
本文介绍了检查Windows资源管理器是否已在给定路径上打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查看Windows资源管理器是否已通过特定路径打开?我不希望我的应用程序打开许多重复的窗口。我无法用这种方式做到这一点:

How can I see if windows explorer is already opened with certain path ? I don't want my application opens many duplicated windows. I was unable to do it with this way :

var
  H: hwnd;
begin
  if FileExists(edt8.Text) then
  begin
    H := FindWindow(0, PChar(ExtractFilePath(edt8.Text)));
    if H <> 0 then
      ShowMessage('explorer already opened')//explorer bring to front
    else
      ShellExecute(Application.Handle, nil, 'explorer.exe',
        PChar(ExtractFilePath(edt8.Text)), nil, SW_NORMAL);
  end;
end;


推荐答案

IShellWindows :: FindWindowSW方法



有一个不错的方法 FindWindowSW ,它应该找到现有的Shell窗口,其中也包括Windows资源管理器窗口。因此,希望我可以轻松找到一个现有窗口,并编写了以下代码:

IShellWindows::FindWindowSW method

There is a nice method FindWindowSW that should find an existing Shell window, which includes Windows Explorer windows as well, I'd say. So, in a hope I'll find an existing window easily I wrote this code:

uses
  ActiveX, ShlObj, SHDocVw, ComObj;

function IDListFromPath(const Path: WideString): PItemIDList;
var
  Count: ULONG;
  Attributes: ULONG;
  ShellFolder: IShellFolder;
begin
  OleCheck(SHGetDesktopFolder(ShellFolder));
  OleCheck(ShellFolder.ParseDisplayName(0, nil, PWideChar(Path), Count, Result, Attributes));
end;

function GetExplorerHandle(const Path: WideString): HWND;
var
  IDList: PItemIDList;
  Unused: OleVariant;
  Location: OleVariant;
  ShellWindows: IShellWindows;
begin
  OleCheck(CoCreateInstance(CLASS_ShellWindows, nil, CLSCTX_LOCAL_SERVER, IID_IShellWindows, ShellWindows));
  Unused := Unassigned;
  IDList := IDListFromPath(Path);
  PVariantArg(@Location).vt := VT_VARIANT or VT_BYREF;
  PVariantArg(@Location).pvarVal := PVariant(IDList);
  ShellWindows.FindWindowSW(Location, Unused, SWC_EXPLORER, Integer(Result), SWFO_INCLUDEPENDING);
end;

但是它永远找不到带有给定文件夹路径的Windows资源管理器窗口(它总是返回0)。我已使用 SWC_EXPLORER 类仅搜索Windows资源管理器窗口,构建绝对ID列表,并使用适当的 VT_VARIANT |地理位置的VT_BYREF 变体(至少希望如此,如果没有,请告诉我)。而且我还尝试返回 IDispatch ,包括 SWFO_NEEDDISPATCH 选项(方法始终返回nil引用)。所以我放弃了这种方法(没有找到任何示例)。

But it never finds the Windows Explorer window with the given folder path (it always returns 0). I've used SWC_EXPLORER class to search only for Windows Explorer windows, build the absolute ID list, used a proper VT_VARIANT | VT_BYREF variant for location (at least I hope so, if not, please let me know). And I also tried to return IDispatch by including the SWFO_NEEDDISPATCH option (method always returned nil reference). So I gave up on this method (haven't found any example).

代码的灵感来自 本文 此示例 。这是一个方案:

The following code was inspired by this article and this example. Here is a scheme:

1. IShellWindows.Item(n)
2. ⤷ IDispatch.QueryInterface(IWebBrowserApp)
3.   ⤷ IWebBrowserApp.QueryInterface(IServiceProvider)
4.     ⤷ IServiceProvider.QueryService(STopLevelBrowser, IShellBrowser)
5.       ⤷ IShellBrowser.QueryActiveShellView
6.         ⤷ IShellView.QueryInterface(IFolderView)
7.           ⤷ IFolderView.GetFolder(IPersistFolder2)
8.             ⤷ IPersistFolder2.GetCurFolder
9.               ⤷ ITEMIDLIST

和一些说明:


  1. 首先,您获得 IShellWindows 接口引用并对其项进行迭代。

  1. As first you obtain the IShellWindows interface reference and iterate its items.

对于每个项目, IShellWindows 界面返回窗口的 IDispatch 接口,然后您可以查询 IWebBrowserApp 界面参考。

For each item, the IShellWindows interface returns window's IDispatch interface which you then query for an IWebBrowserApp interface reference.

获得的 IWebBrowserApp 界面(有关文档,请参考 IWebBrowser2 ,因为它是其实现)除其他外,还提供有关宿主窗口的信息,例如句柄,以后可用于将窗口置于前景。但是我们需要更深入。因此,让我们在此接口参考中查询 IServiceProvider 接口(用于获取给定服务的接口的访问器)。

The obtained IWebBrowserApp interface (for documentation refer to IWebBrowser2, as it's their implementation) provides except others also the information about the host window, like handle which can be later used for bringing the window to foreground. We need to go deeper though. So let's query this interface reference for the IServiceProvider interface (which is an accessor for getting interfaces for the given service).

现在来自最高级的浏览器实现服务查询其 IShellBrowser 界面。对于我们的目标,对该接口的引用仍然不是很有趣。

Now from the top-most browser implementation service query its IShellBrowser interface. A reference of this interface is still not interesting for our aim.

获得的 IShellBrowser 查询所显示的Shell视图对象。

The obtained IShellBrowser query for the displayed Shell view object.

现在我们终于可以说,如果迭代的Shell窗口不是Internet Explorer窗口。到目前为止,他们已经实现了通用接口。现在,如果我们查询获得的 IShellView 用于 IFolderView 界面,它成功了,它不是Internet Explorer,我们可以继续。

Now we can finally say, if the iterated Shell window is not an Internet Explorer window. So far they were having common interfaces implemented. Now if we query the obtained IShellView for the IFolderView interface and it succeeds, it is not Internet Explorer and we can continue.

查询获得的 IFolderView 参考> IPersistFolder2 接口,用于当前显示的文件夹对象。

Query the obtained IFolderView reference for the IPersistFolder2 interface for the currently displayed folder object.

如果我们成功了,甚至得到了 IPersistFolder2 参考,让我们获取 ITEMIDLIST 作为当前文件夹对象。

If we succeeded even there and we got IPersistFolder2 reference, let's get the ITEMIDLIST for the current folder object.

如果即使在最后一步中我们都成功了,我们也会 ITEMIDLIST 显示Windows资源管理器实例(或相同接口实现程序)的文件夹,我们最终可以检查是否获得了 ITEMIDLIST 等于我们为输入路径解析的那个。如果是这样,请将该窗口置于前台,否则请继续进行下一个迭代。

And if we succeeded even with this last step, we have ITEMIDLIST of the currently displayed folder of a Windows Explorer instance (or the same interface implementor) and we can finally check if the obtained ITEMIDLIST equals to the one we parsed for the input path. If so, bring that window to foreground, if not, continue to the next iteration.

这是一个Delphi码。我不知道您需要多少Delphi版本;这是D2009所需的最低要求(手动从Windows SDK 10.0.15063.0翻译而来)。这不是最好的例子;在实际代码中,您可能希望将其包装到类中并具有更灵活的界面,但这取决于您的设计偏好。最后,如果您的Delphi比2009年新,则可能不需要导入的原型,如果更旧,则可能会缺少一些原型:

And here is a Delphi code. I don't know how much do you need for your Delphi version; this was a bare minimum I've needed for D2009 (manually translated from Windows SDK 10.0.15063.0). It's not a best example; in real code you may prefer wrapping this into a class and have more flexible interface, but that's upon your design preference. And finally, if you have Delphi newer than 2009, you may not need the imported prototypes, if older, you might be missing some:

uses
  ActiveX, ShlObj, SHDocVw, ComObj;

{ because of Win32Check }
{$WARN SYMBOL_PLATFORM OFF}
const
  IID_IFolderView: TGUID = '{CDE725B0-CCC9-4519-917E-325D72FAB4CE}';
  IID_IPersistFolder2: TGUID = '{1AC3D9F0-175C-11D1-95BE-00609797EA4F}';
  IID_IServiceProvider: TGUID = '{6D5140C1-7436-11CE-8034-00AA006009FA}';
  SID_STopLevelBrowser: TGUID = '{4C96BE40-915C-11CF-99D3-00AA004AE837}';

type
  IFolderView = interface(IUnknown)
  ['{CDE725B0-CCC9-4519-917E-325D72FAB4CE}']
    function GetCurrentViewMode(out pViewMode: UINT): HRESULT; stdcall;
    function SetCurrentViewMode(ViewMode: UINT): HRESULT; stdcall;
    function GetFolder(const riid: TIID; out ppv): HRESULT; stdcall;
    function Item(iItemIndex: Integer; out ppidl: PItemIDList): HRESULT; stdcall;
    function ItemCount(uFlags: UINT; out pcItems: Integer): HRESULT; stdcall;
    function Items(uFlags: UINT; const riid: TIID; out ppv): HRESULT; stdcall;
    function GetSelectionMarkedItem(out piItem: Integer): HRESULT; stdcall;
    function GetFocusedItem(out piItem: Integer): HRESULT; stdcall;
    function GetItemPosition(pidl: PItemIDList; out ppt: TPoint): HRESULT; stdcall;
    function GetSpacing(var ppt: TPoint): HRESULT; stdcall;
    function GetDefaultSpacing(out ppt: TPoint): HRESULT; stdcall;
    function GetAutoArrange: HRESULT; stdcall;
    function SelectItem(iItem: Integer; dwFlags: DWORD): HRESULT; stdcall;
    function SelectAndPositionItems(cidl: UINT; var apidl: PItemIDList; var apt: TPoint; dwFlags: DWORD): HRESULT; stdcall;
  end;

  EShObjectNotFolder = class(Exception);

function ILGetSize(pidl: PItemIDList): UINT; stdcall;
  external 'shell32.dll' name 'ILGetSize';
function ILIsEqual(pidl1: PItemIDList; pidl2: PItemIDList): BOOL; stdcall;
  external 'shell32.dll' name 'ILIsEqual';
function InitVariantFromBuffer(pv: Pointer; cb: UINT; out pvar: OleVariant): HRESULT; stdcall;
  external 'propsys.dll' name 'InitVariantFromBuffer';
function CoAllowSetForegroundWindow(pUnk: IUnknown; lpvReserved: Pointer): HRESULT; stdcall;
  external 'ole32.dll' name 'CoAllowSetForegroundWindow';

resourcestring
  rsObjectNotFolder = 'Object "%s" is not a folder.';

{ this parses the input folder path and creates ITEMIDLIST structure if the given
  folder path is a valid absolute path to an existing folder }
function GetFolderIDList(const Folder: string): PItemIDList;
const
  SFGAO_STREAM = $00400000;
var
  Count: ULONG;
  Attributes: ULONG;
  ShellFolder: IShellFolder;
begin
  OleCheck(SHGetDesktopFolder(ShellFolder));
  Attributes := SFGAO_FOLDER or SFGAO_STREAM;
  OleCheck(ShellFolder.ParseDisplayName(0, nil, PWideChar(WideString(Folder)), Count, Result, Attributes));
  if not ((Attributes and SFGAO_FOLDER = SFGAO_FOLDER) and (Attributes and SFGAO_STREAM <> SFGAO_STREAM)) then
  begin
    CoTaskMemFree(Result);
    raise EShObjectNotFolder.CreateFmt(rsObjectNotFolder, [Folder]);
  end;
end;

{ translated from the link mentioned in this comment; D2009 does not allow me to
  create an OleVariant of type VT_ARRAY|VT_UI1 which is needed for the Navigate2
  method so I've imported and used the InitVariantFromBuffer function here
  https://msdn.microsoft.com/en-us/library/windows/desktop/gg314982(v=vs.85).aspx }
procedure OpenNewExplorer(IDList: PItemIDList);
var
  Location: OleVariant;
  WebBrowser: IWebBrowser2;
begin
  OleCheck(CoCreateInstance(CLASS_ShellBrowserWindow, nil, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, WebBrowser));
  OleCheck(CoAllowSetForegroundWindow(WebBrowser, nil));
  OleCheck(InitVariantFromBuffer(IDList, ILGetSize(IDList), Location));
  try
    WebBrowser.Navigate2(Location, Unassigned, Unassigned, Unassigned, Unassigned);
  finally
    VariantClear(Location);
  end;
  WebBrowser.Visible := True;
end;

{ translated from the link mentioned in this comment
  https://blogs.msdn.microsoft.com/oldnewthing/20040720-00/?p=38393 }
procedure BrowseInExplorer(const Folder: string);
var
  I: Integer;
  WndIface: IDispatch;
  ShellView: IShellView;
  FolderView: IFolderView;
  SrcFolderID: PItemIDList;
  CurFolderID: PItemIDList;
  ShellBrowser: IShellBrowser;
  ShellWindows: IShellWindows;
  WebBrowserApp: IWebBrowserApp;
  PersistFolder: IPersistFolder2;
  ServiceProvider: IServiceProvider;
begin
  SrcFolderID := GetFolderIDList(Folder);
  try
    OleCheck(CoCreateInstance(CLASS_ShellWindows, nil, CLSCTX_LOCAL_SERVER, IID_IShellWindows, ShellWindows));
    { iterate all Shell windows }
    for I := 0 to ShellWindows.Count - 1 do
    begin
      WndIface := ShellWindows.Item(VarAsType(I, VT_I4));
      { do not use OleCheck here; windows like Internet Explorer do not implement
        all the interfaces; it is the way to distinguish Windows Explorer windows
        actually; so let's get all the references and if we succeed, check if the
        obtained folder equals to the passed one; if so, bring that window to top
        and exit this procedure }
      if Assigned(WndIface) and
        Succeeded(WndIface.QueryInterface(IID_IWebBrowserApp, WebBrowserApp)) and
        Succeeded(WebBrowserApp.QueryInterface(IID_IServiceProvider, ServiceProvider)) and
        Succeeded(ServiceProvider.QueryService(SID_STopLevelBrowser, IID_IShellBrowser, ShellBrowser)) and
        Succeeded(ShellBrowser.QueryActiveShellView(ShellView)) and
        Succeeded(ShellView.QueryInterface(IID_IFolderView, FolderView)) and
        Succeeded(FolderView.GetFolder(IID_IPersistFolder2, PersistFolder)) and
        Succeeded(PersistFolder.GetCurFolder(CurFolderID)) and
        ILIsEqual(SrcFolderID, CurFolderID) then
      begin
        { restore the window if minimized, try to bring it to front and exit this
          procedure }
        if IsIconic(WebBrowserApp.HWnd) then
          Win32Check(ShowWindow(WebBrowserApp.HWnd, SW_RESTORE));
        {$IFNDEF IBelieveThatIWebBrowserAppVisiblePropertyBringsWindowToFront}
        Win32Check(SetForegroundWindow(WebBrowserApp.HWnd));
        {$ELSE}
        OleCheck(CoAllowSetForegroundWindow(WebBrowserApp, nil));
        WebBrowserApp.Visible := True;
        {$ENDIF}
        Exit;
      end;
    end;
    { the procedure was not exited, hence an existing window was not found, so go
      and open the new one }
    OpenNewExplorer(SrcFolderID);
  finally
    CoTaskMemFree(SrcFolderID);
  end;
end;
{$WARN SYMBOL_PLATFORM ON}

可能的用法:

BrowseInExplorer('C:\MyFolder');

这篇关于检查Windows资源管理器是否已在给定路径上打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆