我正在学习用c#写一个BHO,我已经为DocumentComplete和BeforeNavigate2编写了事件处理程序。在调试过程中,我注意到某些网页(例如yahoo.co.in)会多次触发它们。
据我所知,这可能是因为页面中存在多个帧。 我的问题是: -
添加一些示例代码
private InternetExplorer iExplorer;
int IObjectWithSite.SetSite(object pUnkSite)
{
if (pUnkSite != null)
{
ieInstance = (InternetExplorer)pUnkSite;
// Register the DocumentComplete event.
ieInstance.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ieInstance_DocumentComplete);
ieInstance.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(ieInstance_BeforeNavigate2);
}
return 0;
}
private void ieInstance_BeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
{
}
private void ieInstance_DocumentComplete(object pDisp, ref object URL)
{
}
答案 0 :(得分:2)
使用pDisp
和BeforeNavigate2
事件处理程序的DocumentComplete
参数,它指的是与框架(或顶部浏览器)对应的SHDocVw.WebBrowser
对象的实例:
static bool IsTop(object pDisp)
{
var thisBrowser = pDisp as SHDocVw.WebBrowser;
var parent = thisBrowser .Parent as SHDocVw.WebBrowser;
return (parent == thisBrowser || parent == null);
}