c# webbrowser与winform交互访问,javascript参数调用控制程序
2012-03-07 17:39
711 查看
<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
4.初始化WebBrowser的Url与ObjectForScripting两个属性。
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);
using System;
using System.Windows.Forms;
using System.Security.Permissions;
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Form1 : Form
{
private WebBrowser webBrowser1 = new WebBrowser();
private Button button1 = new Button();
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1()
{
button1.Text = “call script code from client code”;
button1.Dock = DockStyle.Top;
button1.Click += new EventHandler(button1_Click);
webBrowser1.Dock = DockStyle.Fill;
Controls.Add(webBrowser1);
Controls.Add(button1);
Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.IsWebBrowserContextMenuEnabled = false;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.ObjectForScripting = this;
// Uncomment the following line when you are finished debugging.
//webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentText =
“< " +
"button onclick=\"window.external.Test('called from script code')\">” +
“call client code from script code” +
““;
}
public void Test(String message)
{
MessageBox.Show(message, “client code”);
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript(”test”,
new String[] { “called from client code” });
}
}
< 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
4.初始化WebBrowser的Url与ObjectForScripting两个属性。
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);
using System;
using System.Windows.Forms;
using System.Security.Permissions;
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Form1 : Form
{
private WebBrowser webBrowser1 = new WebBrowser();
private Button button1 = new Button();
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1()
{
button1.Text = “call script code from client code”;
button1.Dock = DockStyle.Top;
button1.Click += new EventHandler(button1_Click);
webBrowser1.Dock = DockStyle.Fill;
Controls.Add(webBrowser1);
Controls.Add(button1);
Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.IsWebBrowserContextMenuEnabled = false;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.ObjectForScripting = this;
// Uncomment the following line when you are finished debugging.
//webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentText =
“< " +
"button onclick=\"window.external.Test('called from script code')\">” +
“call client code from script code” +
““;
}
public void Test(String message)
{
MessageBox.Show(message, “client code”);
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript(”test”,
new String[] { “called from client code” });
}
}
相关文章推荐
- winform 程序调用及参数调用
- 在C# winform程序中直接执行javascript,调用javascript方法
- SilverLight浏览器交互之:SilverLight程序调用外部Html中的Javascript方法(附源码)
- javascript调用exe程序(windows)传递参数
- Android:WebView与Javascript交互(相互调用参数、传值)
- Android:WebView与Javascript交互(相互调用参数、传值)
- js调用winform程序(带参数)
- winform程序访问一个web方法,传递参数,返回数据
- javascript调用exe程序(windows)传递参数
- Javascript模块化编程(一)模块的写法最佳实践六、输入全局变量 独立性是模块的重要特点,模块内部最好不与程序的其他部分直接交互。 为了在模块内部调用全局变量,必须显式地将其他变量输入模块。
- Android:WebView与Javascript交互(相互调用参数、传值)
- Android:WebView与Javascript交互(相互调用参数、传值)
- 页面调用OCX(ActiveX)控件,自动下载、注册及 javascript对ActiveX的访问、控制和事件调用等。
- 在C#的winForm程序中调用和执行javascript
- Android:WebView与Javascript交互(相互调用参数、传值)
- javascript与cs代码互相调用 asp.net中前台javascript与后台C#交互 这里主要包括了javascipt与后台CS代码四种方法互调(其中包括函数与变量的访问)
- JavaScript页面调用C#后台方法或访问参数
- 在C#的winForm程序中调用和执行javascript
- C#winForm程序与html JS交互调用
- Android编程使用WebView实现与Javascript交互的方法【相互调用参数、传值】