为什么首先为顶层框架触发Webbrowser控件DocumentComplete事件? [英] Why is the Webbrowser control DocumentComplete event fired for top level frame first?

查看:160
本文介绍了为什么首先为顶层框架触发Webbrowser控件DocumentComplete事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于MSDN上的这篇文章:如何确定何时在WebBrowser控件中加载页面,以及从以往关于StackOverflow的讨论中,我假设在文档具有多个框架的情况下,DocumentComplete事件将触发多次,而最后一次将针对顶级框架.

Based on this article on MSDN: How To Determine When a Page Is Done Loading in WebBrowser Control, and from past discussions on StackOverflow, I would assume that in case of a document with multiple frames, the DocumentComplete event would fire multiple times, and the last time would be for the top level frame.

但是,使用上面提到的MSDN链接中的确切示例代码,我发现在导航到URL时,如果有多个DocumentComplete事件,则以下代码中的条件是第一次而不是最后一次满足文章似乎指出的时间.由于条件失败,随后对DocumentComplete的调用似乎是针对较低级别的帧.

However, using the exact sample code from the above-mentioned MSDN link, I find that if there are multiple DocumentComplete events when doing a Navigate to a URL, the condition is satisfied in the following code the first time, not the last time as the article seems to indicate. Subsequent invokes of DocumentComplete seem to be for lower level frames, since the condition fails.

IUnknown*  pUnk;
LPDISPATCH lpWBDisp;
HRESULT    hr;
pUnk = m_webBrowser.GetControlUnknown();
ASSERT(pUnk);
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&lpWBDisp);
ASSERT(SUCCEEDED(hr));
if (lpDisp == lpWBDisp )
{
   // Top-level Window object, so document has been loaded
   TRACE("Web document is finished downloading\n");
}
lpWBDisp->Release();

我不确定为什么我观察到的行为与文档中所假设的行为完全相反.任何对此的指点,将不胜感激.

I am not sure why the behaviour I observe is exactly opposite of what it is supposed to be as per the documentation. Any pointers on this would be appreciated.

背景:我在基于对话框的VC ++/MFC应用程序中使用此代码,并且在DocumentComplete事件中,我希望在完全加载文档时获取某些统计信息.因此,我试图使用上面的代码来检测到DocumentComplete触发的特定实例是在页面完全加载时进行的.

Background: I am using this code in a dialog-based VC++ / MFC application, and in the DocumentComplete event I want to get certain statistics when the document is fully loaded. So I was trying to use the above code to detect that a particular instance of DocumentComplete firing is when the page has fully loaded.

推荐答案

IMO,在页面完全加载后得到通知的最可靠方法是附加到此答案说明了如何使用C#和

IMO, the most reliable way to get notified when the page has been fully loaded is to attach to window.onload DOM event for the top window object (IWebBrowser2::get_Document, IHTMLDocument2::get_parentWindow), when DocumentComplete is fired for the first time for a particular navigation. Then, onload event for the top web page will be fired when all inner frames have been loaded. This answer illustrates how it can be done in C# and this answer may help to get it done in C++.

这篇关于为什么首先为顶层框架触发Webbrowser控件DocumentComplete事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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