The use of WebBrowser in C #

Source: Internet
Author: User
Tags empty
The web actually has a lot of such articles on the Internet, and I'll talk about my own experience roughly from the beginning.
1. Add Reference
Right-click in the control bar, select Add/Remove items, choose COM components, and select Microsoft Web Browser. You can then drag a control onto the form (the control's object is called WebBrowser). Control properties can be adjusted not much, from some data learned that WebBrowser in the menubar, statusbar and so on actually did not realize.

2, function of the control
Because it is COM come over the control, some of its functions are rather strange, the basic navigate function except the first parameter, the other is a ref type, you need to explicitly write ref to invoke. In general, you want to guide it to a page, set an empty object first, the object objnull=null;
webbrowser.navigate (strpage, ref objnull, ref objnull, ref objnull, ref objnull);
Can. The other four parameters can refer to the relevant documentation.
Another common feature is how to get the browser to display its own HTML document, one way is to use the DOM Reagan element innertext:
(MSHTML. Htmldocumentclass) webbrowser.document). documentelement.innertext=shtml;
Code that is quite useful for JavaScript settings Web pages. This method is convenient, but there are many problems, write most of the JavaScript function can not be used normally, that is, the use of <script defer> does not seem to be useless, so you can not use forward, backward for historical browsing, see the source code can not see what ... In fact, the most fatal is that JavaScript can not be used to meet the general needs.
The second method is to write your own web page to a temporary file, and then navigate to the file, Foxmail do so. The disadvantage is that there will be a lot of temporary files, files on the disk to read and write more time-consuming, and remember to clean up in time.
The third approach is to use a stream operation, such as IPersistStreamInit in COM, which is said to be done by Outlook. This is obviously the best way to create a Web page in memory to operate quickly. But I was. NET can not find this interface, so do not know how to implement.

3. Interacting with Applications
The interaction between Web pages and applications is simply clicking on a link on a Web page, or through other means, such as form submission, so that the browser navigates to a new link, so I pass BeforeNavigate2 this event, which has flags, headers, PostData, Targetframename, URLs and other attributes, enough for us to handle. So our program is like a webserver, can handle the links on the Web page, submitted forms and so on. The browser is then navigated to the resulting web page after processing.
And I saw someone using System.Runtime.InteropServices.Expando.IExpando on the Internet:
Mshtml. Htmldocumentclass doc2= (mshtml. Htmldocumentclass) webbrowser.document;
System.Runtime.InteropServices.Expando.IExpando ex= (System.Runtime.InteropServices.Expando.IExpando) doc2;
System.Reflection.PropertyInfo Piform1=ex. AddProperty ("Form1");
Piform1. SetValue (Doc2,this,null);
So, the document in the Web page. Form1 is the control of this WinForm. You can use your script to call form1!. This is a wonderful way to do it.

4. Application Operation browser/Web page:
WebBrowser Browser Controls provide functions, such as EXECWB, that allow browsers to perform some of the internally defined actions, such as executing a Save as:
WEBBROWSER.EXECWB (SHDocVw.OLECMDID.OLECMDID_SAVEAS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref) Objnull, ref objnull);
But it seems like the cancellation here throws an exception, so I use an empty Try{}catch (Exception) {} To catch it, there is no error.
You can also use some of the methods in DOM to manipulate a Web page, for example, to invoke a JavaScript function written in a Web page func ():
   (MSHTML. Htmldocumentclass) webbrowser.document). Parentwindow.execscript ("func ()", "JScript");

For the time being, this is more commonly used. Other let us continue to use in the process of understanding it slowly.


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.