我有一個打開的Windows資源管理器窗口的句柄。 L::\程序到d:\程序如何更改現有Windows資源管理器窗口的路徑?
我如何才能從
例如改變路徑發送命令給它。
直到現在我用ShellExecute()
但它會打開一個新的窗口。這不好(用戶體驗)。
我有一個打開的Windows資源管理器窗口的句柄。 L::\程序到d:\程序如何更改現有Windows資源管理器窗口的路徑?
我如何才能從
例如改變路徑發送命令給它。
直到現在我用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 project
和the blog post
從中我已經採取了靈感。
謝謝@TLama。很好的回答 – 2013-03-03 17:23:19
很高興我能幫忙(雖然有很大的延遲:-) – TLama 2013-03-03 17:44:23
你有一個HWND探險家?你不是說爲了這個使用IShellBrowser和IShellView嗎? – 2012-07-06 10:01:42
@DavidHeffernan,我會爲你的推薦google一些信息。謝謝。 – 2012-07-06 10:26:25
@Kabamaru:不需要Google,因爲你知道你正在走向MSDN:[''IShellBrowser'](http://msdn.microsoft.com/en-us/library/windows/desktop/bb775123(v = vs。 (http://msdn.microsoft.com/en-us/library/windows/desktop/bb774834(v = vs.85).aspx) – 2012-07-06 10:33:16