码迷,mamicode.com
首页 > Web开发 > 详细

Webbrowser控件判断网页加载完毕的简单方法 (转)

时间:2014-07-12 13:53:59      阅读:381      评论:0      收藏:0      [点我收藏+]

标签:des   cWeb   style   blog   http   color   

摘自:http://blog.csdn.net/cometnet/article/details/5261192

一般情况下,当ReadyState属性变成READYSTATE_COMPLETE时,Webbrowser控件会通过触发DocumentCompleted事件来指示网页加载完毕。但当加载的网页包含frame时,可能会多次触发该事件,所以不能简单地通过它来判断网页加载完毕。

从微软的官方网站上了解到,并非每个frame都对应了一个DocumentCompleted事件,只有触发了DownloadBegin事件的frame才会有相应的DocumentCompleted事件。另外,最外层的frame总是最后触发DocumentCompleted事件。DocumentCompleted事件具有一个IDispatch *类型的参数,它指示了是在哪个frame上触发的该事件。所以,要判断文档是否加载完毕,只需要判断IDispatch *参数是否是Webbrowser控件的IDispatch。

微软support网站上关于这个问题的说明:

http://support.microsoft.com/?scid=kb%3Ben-us%3B180366&x=9&y=14

 

原文及译文如下:(我用Google翻译的,不一定正确,大家见谅)

The Internet Explorer WebBrowser control fires the DocumentComplete event when it is finished downloading a Web page. You can create a event handler function in your application for this event. This article describes the steps to take in determining if a the WebBrowser control is finished downloading a Web page.

在Internet Explorer WebBrowser控件激发DocumentComplete事件完成时下载网页。您可以在应用程序中创建的这一事件的事件处理函数。本文介绍的步骤可以参与决定,如果WebBrowser控件下载完成一个网页。

The WebBrowser control fires the DocumentComplete event when its ReadyState property is changed to READYSTATE_COMPLETE. This indicates that the WebBrowser control has completed downloading the Web page. Here are some important points regarding this event:

  • In the case of a page with no frames, DocumentComplete is fired once after everything is done.
  • In case of multiple frames, DocumentComplete gets fired multiple times. Not every frame fires this event, but each frame that fires a DownloadBegin event fires a corresponding DocumentComplete event.
  • The DocumentComplete event has a IDispatch* parameter, which is the IDispatch of the frame (shdocvw) for which DocumentComplete is fired.
  • The top-level frame fires the DocumentComplete in the end. So, to check if a page is done downloading, you need to check if the IDispatch* parameter is same as the IDispatch of the WebBrowser control.

WebBrowser控件激发DocumentComplete事件时,它的readyState属性更改为READYSTATE_COMPLETE。这表明,WebBrowser控件已完成下载的网页。以下是一些对这起事件的要点: 在网页的情况下,没有框架,一旦被激发的DocumentComplete一切完成之后。 在多个帧的情况,得到的DocumentComplete多次发射。不是每帧触发这一事件,但每个帧触发一个DownloadBegin事件触发相应的DocumentComplete事件。 DocumentComplete事件的IDispatch *有一个参数,它是框架(shdocvw),这些被激发的DocumentComplete的IDispatch。 在顶级框架火灾最终的DocumentComplete。因此,要检查一个页面完成下载,您需要检查的IDispatch *参数作为WebBrowser控件的IDispatch相同。

 

For Visual Basic, here is code that performs this check:

  1. Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object,URL As Variant)  
  2.    If (pDisp Is WebBrowser1.Object) Then  
  3.       Debug.Print "Web document is finished downloading"  
  4.    End If  
  5. End Sub  
  6.       

 

To handle the DocumentComplete event in Visual C++ and determine if the download of the Web page is complete, follow these steps.
Note that the steps you follow depend on the way you use the WebBrowser control.

  • If you are creating the WebBrowser control in a CWnd/CView object, you must follow steps 1 to 4.
  • If you are creating the WebBrowser control in a CDialog/CFormView object, only need to follow step 4.
  • If you are using the CHtmlView class provided with Visual C++ 6.0, override CHtmlView::DocumentComplete() and follow step 4, using the m_pBrowserApp member of the CHtmlView class to access the WebBrowser control.

为了处理在Visual C + + DocumentComplete事件,并确定如果网页下载完成后,按照下列步骤。
请注意,您按照步骤取决于你如何使用WebBrowser控件。 如果你在创建一个CWnd / CView对象WebBrowser控件,则必须按照步骤1至4。 如果您要创建一个CDialog在WebBrowser控件/ CFormView对象,只需要按照步骤4。 如果您使用的是CHtmlView类提供与Visual C + + 6.0,重写CHtmlView::的DocumentComplete()和后续步骤4,使用该CHtmlView类m_pBrowserApp成员访问WebBrowser控件。

1.Define the OnDocumentComplete method in the header file for your CWnd/CView-derived class:

  1. afx_msg void OnDocumentComplete(LPDISPATCH lpDisp,VARIANT FAR* URL);  

 

2.Declare the event sink in the same header file:

  1. DECLARE_EVENTSINK_MAP()  

 

3.In the implementation file (.cpp) for your CWnd/CView-derived class, implement the event sink map:

  1. BEGIN_EVENTSINK_MAP(CYourView, CView)  
  2.    ON_EVENT(CWBTstView, ID_WEB_BROWSE, 259 /* DocumentComplete */,  
  3.             OnDocumentComplete, VTS_DISPATCH VTS_PVARIANT)  
  4. END_EVENTSINK_MAP()  

 

4.Implement the OnDocumentComplete method:

  1. void CWBTstView::OnDocumentComplete(LPDISPATCH lpDisp,  
  2.                                     VARIANT FAR* URL)  
  3. {  
  4.    IUnknown*  pUnk;  
  5.    LPDISPATCH lpWBDisp;  
  6.    HRESULT    hr;  
  7.   
  8.    pUnk = m_webBrowser.GetControlUnknown();  
  9.    ASSERT(pUnk);  
  10.   
  11.    hr = pUnk->QueryInterface(IID_IDispatch, (void**)&lpWBDisp);  
  12.    ASSERT(SUCCEEDED(hr));  
  13.   
  14.    if (lpDisp == lpWBDisp )  
  15.    {  
  16.       // Top-level Window object, so document has been loaded  
  17.       TRACE("Web document is finished downloading/n");  
  18.    }  
  19.   
  20.   lpWBDisp->Release();  
  21. }  

 

This approach works when the WebBrowser control navigates to a page that changes the top-level frame. Say if the navigation occurs within a frame itself, then the final DocumentComplete that is fired is that of the frame and not the top-level frame. For example, consider the following scenario.
The WebBrowser control is hosting a frameset. Within one frame of the frameset, the user clicks on a link that opens a new page in the frame itself and keeps the rest of the frameset intact. The new page could contain multiple frames again. So, there will be multiple DocumentComplete notifications (one for each new frame). But, since the top-level frame has not changed, the final DocumentComplete would be that of the frame that has changed.
If you are interested in checking for the final document complete in this scenario, you could do the following:

Check if the IDispatch parameter of the DocumentComplete is the same as the IDispatch parameter of first NavigateComplete2 event. Since the first NavigateComplete2 is of the top-level frame and the last DocumentComplete is also of the top-level frame, doing a comparison in such a fashion will tell whether the page is done downloading.
这种方法在WebBrowser控件时,导航到一个网页,更改顶级框架。说,如果出现导航本身的框架内,那么最后一个DocumentComplete是发射的是,框架,而不是顶级框架。例如,考虑以下情况。
WebBrowser控件主持一个框架。在一个框架集框架,在用户点击链接打开在框架本身就是一种新的一页,保持完整的框架休息。新的页面可能包含多个帧了。因此,将有多个(每个新的框架之一)的DocumentComplete通知。但是,由于顶级框架没有改变,最后的DocumentComplete将是该改变了框架。
如果您是在为最后文件在这种情况下完成检查感兴趣,你可以做以下操作: 检查的一个DocumentComplete的IDispatch参数是相同的第一NavigateComplete2事件的IDispatch参数。自第一NavigateComplete2是顶级帧和最后一个DocumentComplete也是顶级框架,做了这样的方式比较,判断该页面进行下载。
 
Here is some sample C++ code:
  1. LPDISPATCH glpDisp = NULL; // global LPDISPATCH, can also  
  2.                            // be of class scope  
  3.   
  4. // NavigateComplete2 event  
  5. void CWebbrDlg::OnNavigateComplete2Explorer1(LPDISPATCH pDisp,  
  6.                                              VARIANT FAR* URL)  
  7. {  
  8.    // Check if glpDisp is NULL. If NULL, that means it is  
  9.    // the top level NavigateComplete2. Save the LPDISPATCH  
  10.    if (!glpDisp)  
  11.       glpDisp = pDisp;  
  12. }  
  13.   
  14. void CWebbrDlg::OnDocumentCompleteExplorer1(LPDISPATCH pDisp,  
  15.                                             VARIANT FAR* URL)  
  16. {  
  17.    if (glpDisp && glpDisp == pDisp)  
  18.    {  
  19.       // if the LPDISPATCH are same, that means  
  20.       // it is the final DocumentComplete. Reset glpDisp  
  21.       TRACE("Document is done downloading");  
  22.       glpDisp = NULL;  
  23.    }  
  24. }  

 

 

Webbrowser控件判断网页加载完毕的简单方法 (转),布布扣,bubuko.com

Webbrowser控件判断网页加载完毕的简单方法 (转)

标签:des   cWeb   style   blog   http   color   

原文地址:http://www.cnblogs.com/KeenLeung/p/3837853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!