Use of webbrowser in C #

Source: Internet
Author: User

In fact, there are many such articles on the Internet. I will talk about my experience from the beginning.
1. Add reference
Right-click the control bar, select "Add/Remove items", select COM components, and select "Microsoft Web Browser ". Then you can drag a control to the form (an object of the control is called webbrowser below ). There are not many controls that can be adjusted. From some materials, we know that menubar and statusbar in webbrowser are not actually implemented.
(Note:. NET 2.0 has encapsulated a control and does not need to add COM components .)
2. Control Functions
Some of its functions are strange because it is a control from Com. The basic navigate function is of the ref type except the first parameter, and must be explicitly written to the ref for calling. Generally, to direct it to a page, first set an empty object, object objnull = NULL; then
Webbrowser. navigate (strpage, ref objnull, ref objnull );
You can. For details about the other four parameters, refer to the relevant documentation.
Another common function is how to make the Browser display its HTML document. One method is to use the innertext of the DOM Regan element:
(Mshtml.htmldocumentclass?webbrowser.document=.doc umentelement. innertext = shtml;

(Note: The above is the code under. NET 1.1)
It is equivalent to the code used to set web pages in JavaScript. This method is convenient and convenient, but there are many problems. Most of the written JavaScript functions cannot be used normally, that is, it seems useless to use <SCRIPT defer>; in this way, you cannot use forward or backward to browse history. You cannot view the source code ...... In fact, the most critical thing is that JavaScript cannot be used and cannot meet general needs.
The second method is to write your webpage to a temporary file and then navigate to the file. Foxmail does this. The disadvantage is that a lot of temporary files will be generated. It takes a lot of time to read and write files on the disk, and remember to clean them in time.
The third method is to use ipersiststreaminit in COM and the like. It is said that Outlook does this. This is obviously the best way to form a webpage in the memory and perform operations quickly. But I cannot find this interface in. net, so I don't know how to implement it.

3. Interaction with Applications
The interaction between a webpage and an application is similar to clicking a link on the webpage or navigating the browser to a new link through other channels, such as form submission. Therefore, I use the beforenavigate2 event, this event has flags, headers, postdata, targetframename, URL, and other attributes, which are sufficient for processing. In this way, our program is like a webserver that can process links and submitted forms on webpages. Then navigate the browser to the result webpage after processing.
On the Internet, we can see that someone uses system. runtime. interopservices. expando. iexpando:
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 );
In this way, the document. form1 In the webpage is the control of the winform. You can use the script to call form1! This is also a wonderful method.

4. Browser/Webpage operations for applications:
The webbrowser control provides some functions, such as execwb, that allow the browser to execute some internally defined operations, such as saving:
Webbrowser. execwb (shdocvw. olestmid. olecmdid_saveas, shdocvw. olecmdexecopt. olecmdexecopt_dodefault, ref objnull, ref objnull );
However, if you press Cancel here, an exception will be thrown, so I will catch it with an empty try {} catch (exception) {}, so there will be no error.
You can also use some methods in Dom to operate the webpage. For example, you can call a JavaScript function func () written on the webpage as follows ():
  (Mshtml.htmldocumentclass?webbrowser.document=.parentwindow.exe cscript ("func ()", "jscript ");
(Note: The same. NET 1.1 code)
For the time being, this is what is commonly used. For other things, let's continue to use them.
(Note: This article was found to have been transcribed in many places. Note that it only applies to. NET 1.1)

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.