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.
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.
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.