易截截图软件、单文件、免安装、纯绿色、仅160KB

JavaScript与C# Windows应用程序交互方法

一、建立网页
<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);
完整代码如下:
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.IO.FileInfo file = new System.IO.FileInfo("index.htm");
// WebBrowser控件显示的网页路径
webBrowser1.U


相关文档:

JavaScript中匿名函数,函数直接量和闭包(转)

原文出处: http://www.dnew.cn/post/196.htm
先看下下面几种写法
1.function f(x){return x*x;};f(x);
2.(function(x){return x*x;})(x);
3.(function(x){return x*x;}(x));
第一种我们应该都很熟悉了,这是我们经常使用的写法。第二第三种都是匿名函数的写法。
------------------------------------------------ ......

JavaScript 判断文件是否存在

1. 客户端 //主要针对本地IE浏览器访问
<script
language="javascript">
function FileExist()
{
     var sfso=new
ActiveXObject("Scripting.FileSystemObject");
     var fPath="[The path of the
file]";
     if(sfso.FileExists(fP ......

Javascript: setTimeout()使用及 setInterval()使用

Evaluates an expression after a specified number of milliseconds has elapsed.
(在指定时间过后执行指定的表达式)
Syntax:
iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])
Parameters
vCode
Required. Variant that specifies the function pointer or string that indicates the code to be ......

javascript 动态插入行 表格操作

<script>  
    var i = 0;
    function insertTr(obj)  
    {  
        var tr1 =  tb.insertRow(obj.rowIndex+1);  
        ......

javascript 禁止鼠标右键...

<body oncontextmenu="return false" onselectstart="return false" ondragstart="return false" onbeforecopy="return false" onmouseup=document.selection.empty() oncopy=document.selection.empty() onselect=document.selection.empty()></body>
讲上面红色显示的插入到网页中就可以实现鼠标右击无效
......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号