问题描述
下面code打开一个链接在现有的浏览器窗口时browserExe指向火狐,Safari或Chrome。当指向IEXPLORE.EXE(IE7)一个新的窗口被打开。
The following code opens a link in an existing browser window when browserExe is pointing to Firefox, Safari or Chrome. When pointing to IEXPLORE.EXE (IE7) a new windows is opened.
ProcessStartInfo pi = new ProcessStartInfo(browserExe, url);
Process.Start(pi);
这将打开现有的窗口,一个标签如预期,当IE为默认浏览器。
This opens a tab in an existing window as intended, when IE is the default browser.
ProcessStartInfo pi = new ProcessStartInfo(url);
Process.Start(pi);
如何我重用现有的IE窗口,当IE是不是默认的浏览器?
How to i reuse an existing IE windows, when IE is NOT the default browser?
推荐答案
使用SHDOCVW库(在此提及它,你可以找到它在Windows \ System32下),你可以得到实例的列表,并调用导航与newtab参数:
Using shdocvw library (add reference to it, you can find it in windows\system32) you can get the list of instances and call navigate with the newtab parameter:
ShellWindows iExplorerInstances = new ShellWindows();
if (iExplorerInstances.Count > 0)
{
IEnumerator enumerator = iExplorerInstances.GetEnumerator();
enumerator.MoveNext();
InternetExplorer iExplorer = (InternetExplorer)enumerator.Current;
iExplorer.Navigate(url, 0x800); //0x800 means new tab
}
else
{
//No iexplore running, use your processinfo method
}
编辑:在某些情况下,你可能要检查shellwindow对应于一个真实的IEXPLORER一个没有任何其他的Windows外壳程序(W7中的所有实例都回来了,不知道现在的人)
in some cases you may have to check if the shellwindow corresponds to a real iexplorer an not to any other windows shell (in w7 all instances are returned, don't know now for others).
bool found=false;
foreach (InternetExplorer iExplorer in iExplorerInstances)
{
if (iExplorer.Name == "Windows Internet Explorer")
{
iExplorer.Navigate(ur, 0x800);
found=true;
break;
}
}
if(!found)
{
//run with processinfo
}
您还可能会发现这些额外的IE浏览标志有用。的标志完整描述可在http://msdn.microsoft.com/en-us/library/dd565688(v=vs.85).aspx
You may also find these additional IE Navigate Flags useful. Full description of the flags are available at http://msdn.microsoft.com/en-us/library/dd565688(v=vs.85).aspx
enum BrowserNavConstants
{
navOpenInNewWindow = 0x1,
navNoHistory = 0x2,
navNoReadFromCache = 0x4,
navNoWriteToCache = 0x8,
navAllowAutosearch = 0x10,
navBrowserBar = 0x20,
navHyperlink = 0x40,
navEnforceRestricted = 0x80,
navNewWindowsManaged = 0x0100,
navUntrustedForDownload = 0x0200,
navTrustedForActiveX = 0x0400,
navOpenInNewTab = 0x0800,
navOpenInBackgroundTab = 0x1000,
navKeepWordWheelText = 0x2000,
navVirtualTab = 0x4000,
navBlockRedirectsXDomain = 0x8000,
navOpenNewForegroundTab = 0x10000
};
这篇关于在选项卡从C#现有的IE窗口中启动URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!