A brief analysis of the use of WebBrowser control in c sharp

  • 2020-04-02 01:21:33
  • OfStack

Let's start with a brief introduction to the webbrowser control, which enables you to add web content to the form. As shown in the picture, I added baidu API in the form.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/201307311008088.jpg ">

Using this control is simple

(1) the first step
Just enter it in form_load


webBrowser1.Navigate(Application.StartupPath + " /map.html");//Place the address in quotation marks for the webpage code, pay attention to use relative address instead of absolute address, so as to have portability, put the webpage in the debug directory of the program, so that you can directly modify the webpage code in vs
webBrowser1.ObjectForScripting = this; //According to

(2) the second step
Some students may need to call some variable data in the webBrowser control in the form. Let's say I want to call the distance between two points measured in baidu map. At this point, first add the following code in the web file:

myDis.addEventListener("drawend", function(e) //This is the code that baidu comes with to measure distance
{  result1=e.distance;
alert(result1); 
    });
function  getdistance()//This section, by itself, returns the value of the distance
{return result1;} 

That's the web section
Then in the form to get the distance in the web, use the following code:

var s = webBrowser1.Document.InvokeScript("getdistance");//Inside the quote is the name of the function. Note that the variable is of type var

Then pass hight = convert.toint32 (s); You can convert var type to int type, and you can reference it at will.


Related articles: