C〓〓win formとJavascriptの相互呼び出し
12518 ワード
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<script language="javascript" type="text/javascript">
<!-- C# -->
function messageBox(message)
{
alert(message);
}
</script>
</head>
<body>
<!-- C# -->
<button onclick="window.external.MyMessageBox('javascript C# ')">
javascript C# </button>
</body>
</html><html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<script language="javascript" type="text/javascript">
<!-- C# -->
function messageBox(message)
{
alert(message);
}
</script>
</head>
<body>
<!-- C# -->
<button onclick="window.external.MyMessageBox('javascript C# ')">
javascript C# </button>
</body>
</html>
、 Windows
1. Windows
2. Form1 WebBrowser
3. Form1
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
com 。 。
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
Url :WebBrowser
ObjectForScripting : WebBrowser 。
Url URL 。
JavaScript window.external C# 。 ObjectForScripting 。 :
System.IO.FileInfo file = new System.IO.FileInfo("index.htm");
// WebBrowser
webBrowser1.Url = new Uri(file.FullName);
//
webBrowser1.ObjectForScripting = this;
5.C# JavaScript
WebBrowser Document InvokeScript Javascript 。 :
// JavaScript messageBox ,
object[] objects = new object[1];
objects[0] = "C# JavaScript ";
webBrowser1.Document.InvokeScript("messageBox", objects);
:
==============================================================================
JS :
<script language=”javascript”>
function jsAlert(infor)
{
alert(‘ 、 , , !’);
return;
}
</script>
C# :
using System.Security.Permissions;
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]// : , !
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
:
webBrowser1.Document.InvokeScript("jsAlert ", new string[] {‘ssss’});
JS C# :
C# :
public void ShowMessage(string message)
{
MessageBox.Show(message);
}
JS :
<script language=”javascript”>
function InvokeFunc()
{
window.external.ShowMessage(‘ ’);
}
</script>
C# winform , WebBrowser 。 , , JS , , 。 。
===========================================================================================================
c# js
JScript code
<script language="javascript"type="text/javascript">
varg_h=55;
functiondisplayVar(varname)
{
try
{
returneval(varname);
}
catch(e)
{
returne.description;
}
}
</script>
c# g_h , 。
, , eval , g_h
C# code
object[] paramArray=newobject[1];
paramArray[0]="g_h";
webBrowser1.Document.InvokeScript("displayVar", paramArray);
:
webBrowser1.Document.InvokeScript("JS ", New Object() { })
: ”,“ 。
:
webBrowser1.Document.InvokeScript("Go", New Object() {"home","back"})
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Case 1 , ,Web HTML , Windows Form , Web WinForm;
,WinForm Web HTML , Web Script ?
Web 。 HTML Javascript:
function DoAdd(a, b) {
return a + b;
}
, WinForm , :
object oSum = webBrowser.Document.InvokeScript("DoAdd", new object[] { 1, 2 });
int sum = Convert.ToInt32(oSum);
, Web , ? .Net , COM 。IHTMLWindow2
。
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(' :\r
:' + numLinks + ";
string scriptline07 = @" '\r
:' + numForms + ";
string scriptline08 = @" '\r
:' + numImages + ";
string scriptline09 = @" '\r
:' + 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")