II. Building Windows applications
3. At the top of the Form1 class, add
[System.Runtime.InteropServices.ComVisibleAttribute (True)]
This is to set the class to COM accessible. If you do not make the declaration, you will get an error.
[System.Runtime.InteropServices.ComVisibleAttribute (True)]
public partial class Form1:form
URL Properties: Page path displayed by the WebBrowser control
ObjectForScripting property: This object can be accessed by the script code contained in the Web page that is displayed in the WebBrowser control.
Set the URL property to the URL path of the page that needs to be manipulated.
System.IO.FileInfo file = new System.IO.FileInfo ("index.htm");
The page path displayed by the WebBrowser control
Webbrowser1.url = new Uri (file. FullName);
To set the current class to be accessible by script
Webbrowser1.objectforscripting = this;
5. C # calls the JavaScript method
Call the JavaScript MessageBox method and pass in the parameters
Object[] objects = new OBJECT[1];
Objects[0] = "C # access JavaScript script";
WebBrowser1.Document.InvokeScript ("MessageBox", objects);
==============================================================================
<script language= "JavaScript" >
function Jsalert (infor)
{
Alert (' I want to open an online shop, with the purchase of independent online system, the most complete template, the most professional system! ’);
Return
}
</script>
C # code is called as follows:
[PermissionSet (SecurityAction.Demand, Name = "FullTrust")]//Note: The following two lines need to be added before the class definition, otherwise the call fails!
function to call:
JS Call C # Function Example:
C # functions are as follows:
public void ShowMessage (String message)
{
MessageBox.Show (message);
}
JS in the call method:
<script language= "JavaScript" >
function Invokefunc ()
{
Window.external.ShowMessage (' hehe ');
}
</script>
In the process of developing the WinForm program using C #, we often encounter a browser control embedded with a webbrowser. Most of the time, we need to control the display of the Web page in the program, or invoke some JS function in the Web page, in turn, it is possible that the Web page also needs to invoke the functions of the program to implement certain functions. Let me explain how we interact with each other.
===========================================================================================================
Can you define a C # variable to get a pointer to a global variable defined in JS
JScript Code
<script language= "javascript" type= "Text/javascript" >
varg_h=55;
Functiondisplayvar (varname)
{
Try
{
Returneval (varname);
}
catch (E)
{
Returne.description;
}
}
</script>
Can the C # implement get the memory space that the G_H variable points to, and then modify his value.
I can only implement it now, show his value, because the Eval method returns a new object, so it can't change the original variable G_h
C # code
Object[] paramarray=newobject[1];
Paramarray[0]= "G_h";
WebBrowser1.Document.InvokeScript ("Displayvar", ParamArray);
You can also use this to invoke:
WebBrowser1.Document.InvokeScript ("JS function name", New Object () {parameter})
Parameter values: Multiple parameters are separated by "," numbers.
For example:
WebBrowser1.Document.InvokeScript ("Go", New Object () {"Home", "Back"})
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
In case 1, we have seen that the HTML element events of the Web page can be responded to by the Windows Form side, and can be seen to some extent as a Web page call WinForm;
In addition to the HTML elements that can directly access the Web page, can WinForm invoke the various script in the Web page?
The first is a function that has already been defined in the script that invokes the Web page. Suppose the following JavaScript is in the HTML:
function Doadd (A, b) {
return a + B;
}
So, we're going to call it in WinForm, just the following code:
Object osum = WebBrowser.Document.InvokeScript ("Doadd", new object[] {1, 2});
int sum = Convert.ToInt32 (osum);
Secondly, what if we want to execute a script that is not originally in a Web page? This. NET class does not provide, it seems to rely on COM. IHTMLWindow2 can put any character
String to execute as script code.
String scriptline01 = @ "function Showpageinfo () {";
String scriptline02 = @ " var numlinks = document.links.length; ";
String scriptline03 = @ " var numforms = document.forms.length; ";
String scriptline04 = @ " var numimages = document.images.length; ";
String scriptline05 = @ " var numscripts = document.scripts.length; ";
String scriptline06 = @ " alert (' Statistical result of the webpage: \ r \ n Link number: ' + numlinks +";
String scriptline07 = @ " ' \ r \ n Table singular: ' + numforms +";
String scriptline08 = @ " ' \ r \ n Image number: ' + numimages +";
String scriptline09 = @ " ' \ r \ n Script number: ' + numscripts);}";
String scriptline10 = @ "Showpageinfo ();";
String strscript = scriptline01 + scriptline02 + scriptline03 + scriptline04 + scriptline05 +
Scriptline06 + scriptline07 + scriptline08 + scriptline09 + scriptline10;
IHTMLWindow2 win = (IHTMLWindow2) WebBrowser.Document.Window.DomWindow;
Win.execscript (Strscript, "Javascript")
Go C # WinForm and JavaScript calls to each other