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

时间:2014-01-13 15:51:18

标签: c# internet-explorer com shdocvw

如何区分IE shell窗口和非IE shell窗口?我已经获得了以下代码片段(删除了Lot或无关逻辑),它使用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窗口感兴趣,而不是窗口打开的其他随机窗口(代码甚至让允许您配置任务栏的窗口滑过)。

1 个答案:

答案 0 :(得分:1)

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

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