Functions of webbrowser

Source: Internet
Author: User
Tags blank page

After so many articles about webbrowser, I will give a comprehensive introduction to it.

From http://blog.sina.com.cn/s/blog_3d7bed6501000c8n.html

 

Introduction to the webbrowser ControlThe webbrowser control has been used for a long time and is an ActiveX control. Previously, it can be referenced and used through COM. However, after. NET Framework 2.0, managed packaging of the webbrowser control is provided. Hosted packaging makes it easier to display web pages on Windows Forms clients. The webbrowser control can be used to simulate the IE function for web access, but it is not limited to this. It can also be used as a simple HTML document viewer by disabling the default IE function, you can even browse documents in various office formats. In addition, webbrowser supports Dom and text to modify the content it carries. This means that you do not need to save the modified HTML to a temporary file, but directly modify and display it in the content to improve performance and save the consumption of temporary file resources. All in all, we can use various methods to seamlessly integrate Web controls and Windows form controls into an application. Use webbrowser to browse Web pagesWeb browsing is an old line of webbrowser. It is easier to host packaged webbrowser. The main methods are as follows: navigate (): navigation page. Different from previous versions, you can only specify one parameter, that is, Uri. Goback ()/goforward ()/Gohome (): forward/back to homepage. Stop (): refresh (): Stop loading/refresh the preceding methods are used to navigate pages, but they do not always take effect. Whether these methods take effect depends on the allownavigation attribute. The default value is true, allowing navigation. Print (): used for printing. (1) HTML document model in webbrowserWe can return the HTML description of the current webpage in the form of Dom or text in the current webbrowser: Document: Dom (Document Object Model. Documenttext: HTML description of the current webpage in text format. Documentstream: an HTML description in the stream format. There is nothing to say about the text form. It is the most direct HTML description, but it is difficult to modify dynamically. In contrast, HTML descriptions in the DOM form are much more flexible, you can add, modify, or delete any node. The main elements of the DOM object model include htmldocument, htmlelement, htmlnode, and htmltext. (2) webbrowser and IEBy default, the webbrowser control features similar to IE and provides basic functions. For example, you can use Gohome (), Goback (), gofoward (), stop (), and refresh () to implement homepage, backward, forward, stop, and refresh on the IE Toolbar; implement the address bar in IE through navigate (); Implement the IE shortcut menu by setting iswebbrowsercontextmenuenabled = true; Implement IE shortcut keys by setting webbrowsershortcutsenabled = true. In some special cases, we may not want webbrowser to behave like ie. For example, we just want webbrowser to act as an HTML display, in this case, you can set allownavigate to false to prevent webbrowser navigation. Set iswebbrowsercontextmenuenabled and webbrowsershortcutsenabled to false to prevent right-click menus and shortcut keys. This prevents some ie features of webbrowser. (3) Script Error Handling in webbrowserWhen the IE browser encounters a script error, a yellow icon will appear in the lower left corner. You can click to view the detailed information of the script error. No error message box is displayed. However, the webbrowser control is not so intelligent. It will pop up an error message box, making the program appear unfriendly and suspending some automatically executed programs. Although webbrowser provides the scripterrorssuppressed attribute, the result is regrettable after the attempt. setting this attribute can solve some problems, but it cannot be completely solved. Two solutions are discussed here: one is to intercept the webbrowser. Document. Window. Error Event and prevent webbrowser from continuing to handle the error. // Register the error handling event of the capture control.
This. webbrowser. Document. Window. Error + = new htmlelementerroreventhandler (window_error );
// Handle errors
Void window_error (Object sender, htmlelementerroreventargs E)
{
E. Handled = true; // block other places from processing
}
The above method can solve most of the problems, but it still cannot be well solved when multiple frameworks are nested. Therefore, you can only encapsulate one mywebbrowser by yourself, then we use our own mywebbrowser to replace webbrowser. As follows:

Public class mywebbrowser: system. Windows. Forms. webbrowser
{
Private shdocvw. iwebbrowser2 iwb2;

Protected override void attachinterfaces (Object nativeactivexobject)
{
Iwb2 = (shdocvw. iwebbrowser2) nativeactivexobject;
Iwb2.silent = true;
Base. attachinterfaces (nativeactivexobject );
}

Protected override void detachinterfaces ()
{
Iwb2 = NULL;
Base. detachinterfaces ();
}
}

This method can solve the problem very well. In fact, some friends who have used the unmanaged webbrowser must have seen it. In fact, shdocvw can be referenced to bypass the managed code and directly use shdocvw. iwebbrowser2.silent = true, which is the original and most effective.

Use webbrowser to access office filesI just said that browsing the Web page is an old webbrowser line. In addition to this line, he can also work on part-time jobs. We know that the Office document ole control is not provided in the. NET form. If you want to embed the Office document, a possible solution is to use the webbrowser control. In fact, office documents can be embedded in IE, so webbrowser has this skill and capability, but it does bring good results. The operation is also simple: This. webbrowser1.navigate (strfilename); webbrowser will open the office documents (Word, Excel, PowerPoint,...) as embedded ,...) the effect is as follows: Precautions for using the webbrowser Control1. The webbrowser control in net 2.0 is incomplete encapsulation of the webbrowser COM component. Therefore, such problems may occur at the same time. There is a price for simplicity. 2. The webbrowser control browses documents asynchronously. When navigate () is called, the control is returned to the application before the document is fully loaded. If you plan to perform automatic operations on the included documents, you must notify the documentcompleted event after the document is loaded. Or isbusy is used to determine whether the current webbrowser is busy loading other documents. However, through practice, the problem is not that simple. 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. 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. So how can we accurately determine that the page has been loaded? The following is a solution:

Int counter = 0; // counter
Private void webbrowser_navigated (Object sender,

Webbrowsernavigatedeventargs e ){
Counter ++;
}
Private void webbrowser_documentcompleted (Object sender,

Webbrowserdocumentcompletedeventargs e ){
Counter --;

If (counter = 0 ){
// Load completed
}
}

 

3. Multiple problems may occur when a project contains multiple webbrowser controls and each control loads office documents of the same type (both word and all Excel files. The most common problem occurs in the office command bar (the command bar is disabled ). If there are two webbrowser controls on the same form, and both controls are loaded with Word documents, only one group of Toolbar will be active, and the other will be disabled and unavailable. Therefore, we recommend that you use only one control for a project and view only one document at a time.


4. to clear the current content in the webbrowser control, use the following code to browse the default blank page:
This. webbrowser1.navigate ("about: blank ");

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.