使用 SHDocVw 时将 IE 窗口与其他窗口区分开来



如何区分 IE 外壳窗口和非 IE 外壳窗口?我有以下代码片段(删除了大量或无关的逻辑),它使用 ShellWindows 对象扫描打开的窗口以查看用户正在浏览的 URL,如果他们浏览到特定 URL,则打算执行某些操作:

// Shell Windows object obtained in another method
private ShellWindows shellWindows = new ShellWindows();
...
 // iterate through all windows
foreach (SHDocVw.IWebBrowser2 shellWindow in shellWindows)
{
    // We only want to process the windows that are Internet explorer windows not file browsers etc
    if (shellWindow is SHDocVw.InternetExplorer ) // <--- This isn't filtering as I'd expect it to.
    {
        // THIS IS AN IE BROWSER WINDOW DO SOMETHING WITH IT.
    }
}

不过,我只对Internet Explorer窗口感兴趣,而对窗口打开的其他随机窗口不感兴趣(代码甚至允许允许您配置任务栏的窗口溜走)。

只有 Internet Explorer 将 HTMLDocument 作为Document对象,因此您可以检查以下内容:

if (shellWindow.Document is mshtml.HTMLDocument) // requires a reference to mshtml
{
    // this is Internet Explorer
}

最新更新