从 C# 在现有 IE 窗口的选项卡中启动 URL [英] Launch a URL in a tab in an existing IE window from C#

查看:16
本文介绍了从 C# 在现有 IE 窗口的选项卡中启动 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 browserExe 指向 Firefox、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 库(添加对它的引用,您可以在 windowssystem32 中找到它)您可以获得实例列表并使用 newtab 参数调用导航:

Using shdocvw library (add reference to it, you can find it in windowssystem32) 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 shell(在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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