下面列出了该问题的解决方案。我怀疑这种方法是否是最佳实践,但它似乎确实有效。
Visual Studio 中的 Web 浏览器窗口具有“EnvDTE.Constants.vsWindowKindWebBrowser”的“ObjectKind”值。“Object”值是 Internet Explorer 浏览器并实现“IWebBrowser2”。尽管“IWebBrowser2”不是.Net,“System.Windows.Forms.WebBrowser”(显然)是“IWebBrowser2”的包装。“System.Windows.Forms.WebBrowser”有一个可以被覆盖的“AttachInterfaces”方法,它允许我们传递窗口对象并让“System.Windows.Forms.WebBrowser”完成繁重的工作。
最终结果是:
public class VisualStudioWebBrowser : System.Windows.Forms.WebBrowser
{
protected VisualStudioWebBrowser(object IWebBrowser2Object)
{
this.IWebBrowser2Object = IWebBrowser2Object;
}
protected object IWebBrowser2Object { get; set; }
public static void Evaluate(EnvDTE.Window WindowReference, Action<System.Windows.Forms.WebBrowser> OnEvaluate)
{
//Note: Window of EnvDTE.Constants.vsWindowKindWebBrowser type contains an IWebBrowser2 object
if (VisualStudioWebBrowser.IsWebBrowserWindow(WindowReference))
{
using (System.Threading.ManualResetEvent evt = new System.Threading.ManualResetEvent(false))
{
System.Threading.Thread STAThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart((o) =>
{
try
{
using (VisualStudioWebBrowser Browser = new VisualStudioWebBrowser(o))
{
try
{
OnEvaluate.Invoke((System.Windows.Forms.WebBrowser)Browser);
}
catch { }
}
}
catch { }
evt.Set();
}));
STAThread.SetApartmentState(System.Threading.ApartmentState.STA);
STAThread.Start(WindowReference.Object);
evt.WaitOne();
}
}
}
public static bool IsWebBrowserWindow(EnvDTE.Window WindowReference)
{
return (WindowReference != null && WindowReference.ObjectKind.Equals(EnvDTE.Constants.vsWindowKindWebBrowser, StringComparison.InvariantCultureIgnoreCase));
}
public static IEnumerable<EnvDTE.Window> GetWebBrowserWindows(EnvDTE.DTE EnvDTEReference)
{
List<EnvDTE.Window> BrowserWindows = new List<EnvDTE.Window>();
foreach (EnvDTE.Window WindowReference in EnvDTEReference.Windows)
{
if (VisualStudioWebBrowser.IsWebBrowserWindow(WindowReference) == true)
{
BrowserWindows.Add(WindowReference);
}
}
return BrowserWindows;
}
public static Uri GetWebBrowserWindowUrl(EnvDTE.Window WindowReference)
{
Uri BrowserUrl = new Uri("", UriKind.RelativeOrAbsolute);
VisualStudioWebBrowser.Evaluate(WindowReference, new Action<System.Windows.Forms.WebBrowser>((wb) =>
{
BrowserUrl = wb.Url;
}));
return BrowserUrl;
}
public static IEnumerable<Uri> GetWebBrowserWindowUrls(EnvDTE.DTE EnvDTEReference)
{
List<Uri> BrowserUrls = new List<Uri>();
foreach (EnvDTE.Window BrowserWindow in VisualStudioWebBrowser.GetWebBrowserWindows(EnvDTEReference))
{
try
{
Uri BrowserUrl = VisualStudioWebBrowser.GetWebBrowserWindowUrl(BrowserWindow);
if (String.IsNullOrWhiteSpace(BrowserUrl.LocalPath) == false)
{
BrowserUrls.Add(BrowserUrl);
}
}
catch { }
}
return BrowserUrls;
}
protected override void AttachInterfaces(object nativeActiveXObject)
{
base.AttachInterfaces(this.IWebBrowser2Object);
//base.AttachInterfaces(nativeActiveXObject);
}
protected override void DetachInterfaces()
{
base.DetachInterfaces();
this.IWebBrowser2Object = null;
}
}
然后通过以下方式找到打开的网页列表:
IEnumerable<Uri> Urls = VisualStudioWebBrowser.GetWebBrowserWindowUrls(EnvDTEReference);