C #, webbrowser determines whether the webpage has been loaded

Source: Internet
Author: User

Private void button#click (object sender, EventArgs e)

{

Wb. Url = new Uri ("http://www.baidu.com ");

Wb. DocumentCompleted + = new WebBrowserDocumentCompletedEventHandler (wb_DocumentCompleted );

// This is an event for adding a load to wb.

}

Private void wb_DocumentCompleted (object sender, EventArgs e) // This is the operation to be performed after the webpage is loaded.

{

RichTextBox1.Text = wb. DocumentText; // output HTML code loaded into the webpage to the text box

}

Generally, when the ReadyState attribute changes to READYSTATE_COMPLETE, The Webbrowser control triggers the DocumentCompleted event to indicate that the webpage has been loaded. However, when a webpage contains a frame, this event may be triggered multiple times, so you cannot simply use it to determine that the webpage has been loaded.

I learned from Microsoft's official website that not every frame corresponds to a DocumentCompleted event. Only the frame that triggers the DownloadBegin event will have the corresponding DocumentCompleted event. In addition, frames at the outermost layer always trigger the DocumentCompleted event. The DocumentCompleted event has an IDispatch * type parameter, indicating the frame on which the event is triggered. Therefore, to determine whether the document has been loaded, you only need to determine whether the IDispatch * parameter is the IDispatch of the Webbrowser control.

----------------------- The following part is from msdn ----------------------------------

When the ReadyState attribute of the DocumentComplete event triggered by the WebBrowser control is changed to READYSTATE_COMPLETE. This indicates that the web page has been downloaded by the WebBrowser control. The following are some important points about this event:
When there is no frame page, the DocumentComplete trigger is triggered once after all is completed.
When multiple frames exist, the DocumentComplete acquisition is triggered multiple times. Not every framework will trigger this event, but each framework that triggers the DownloadBegin event will trigger the corresponding DocumentComplete event.
The DocumentComplete event has an IDispatch * parameter that is used to trigger the IDispatch of the DocumentComplete framework (Shdocvw.
The top-level framework triggers the final documentcomplete. To check whether page downloads are not performed, check whether the idispatch * parameter is the same as the idispatch of the webbrowser control.

For Visual Basic, the following code executes this check: private sub webbrowserappsdocumentcomplete (byval Pdisp as object,
URL as variant)
If (Pdisp is webbrowser1.object) then
Debug. Print "Web Document is finished downloading"
End if
End sub

To process the documentcomplete event in Visual C ++ and determine whether the download of the webpage is complete, perform these steps.

Note that the steps you perform depend on how you use the webbrowser control.
To create a webbrowser control cwnd/cview object, you must perform steps 1 to 4.
If you create a webbrowser control in the cdialog/cformview object, you only need to perform step 4.
If you use the CHtmlView class provided with Visual C ++ 6.0, overwrite the CHtmlView: DocumentComplete (), and follow Step 4 to use the m_pBrowserApp Member of the CHtmlView class to access the WebBrowser control.
Define the OnDocumentComplete method in the header file CWnd/CView derived class: afx_msg void OnDocumentComplete (LPDISPATCH lpDisp,
Variant far * URL );

Declare the event receiver in the same header file: DECLARE_EVENTSINK_MAP ()

In the implementation file (. cpp) as the CWnd/CView derived class, implement the event receiver ing: BEGIN_EVENTSINK_MAP (CYourView, CView)
ON_EVENT (CWBTstView, ID_WEB_BROWSE, 259/* DocumentComplete */,
OnDocumentComplete, VTS_DISPATCH VTS_PVARIANT)
END_EVENTSINK_MAP ()

OnDocumentComplete method: void CWBTstView: OnDocumentComplete (LPDISPATCH lpDisp,
Variant far * URL)
{
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 ();
}

This method is applicable when the webbrowser control locates the page for modifying the top-level framework. Assume that the scene is in the navigation, and the final documentcomplete text frame and non-top-layer frame are triggered within the range. For example, consider the following situations.

The webbrowser control carries a framework set. In a framework of a framework set, you click a link to open a new page in the external frame itself, and keep the rest of the framework set unchanged. The new page may contain multiple frameworks again. Therefore, there will be multiple documentcomplete notifications (one for each new framework ). However, because of the changes, the top-level framework will not change the final documentcomplete framework.

If you are interested in checking that the final document is completed in this case, perform the following operations: Check whether the idispatch parameter of the documentcomplete is the same as the first navigatecomplete2 event of the idispatch parameter. Because the first navigatecomplete2 is a top-level framework, and the last documentcomplete is a top-level framework, executing such a comparison will tell you whether to complete page download. Below are some examples of C ++ code: lpdispatch glpdisp = NULL; // global lpdispatch, can also
// Be of Class scope

// Navigatecomplete2 event
Void cwebbrdlg: onnavigatecomplete2assumer1 (lpdispatch Pdisp,
Variant far * URL)
{
// Check if glpdisp is null. If null, that means it is
// The top level navigatecomplete2. Save the lpdispatch
If (! Glpdisp)
Glpdisp = Pdisp;
}

Void cwebbrdlg: ondocumentcompletetrigger1 (lpdispatch Pdisp,
Variant far * URL)
{
If (glpDisp & glpDisp = pDisp)
{
// If the LPDISPATCH are same, that means
// It is the final DocumentComplete. Reset glpDisp
TRACE ("Document is done downloading ");
GlpDisp = NULL;
}
}

Bytes -------------------------------------------------------------------------------------------

Another method is as follows:

When a webpage is loaded using the Webbrowser control, the DocumentCompleted event is usually used to indicate that the webpage has been loaded. However, when a webpage contains a frame, this event may be triggered multiple times, so you cannot simply use it to determine that the webpage has been loaded. It must be determined by WebBrowserNavigatedEventArgs.

P rivate void webbrowserappsdocumentcompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
{
If (e. Url! = WebBrowser1.Document. Url) return;
 
// When e. Url = webBrowser1.Document. Url, the webpage we want is loaded.
// Load completed
//...
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.