winform调用javascript的小例子
程序员文章站
2023-12-16 10:38:40
复制代码 代码如下: &nb...
复制代码 代码如下:
<html>
<head>
<title></title>
<script type="text/javascript">
function showmessage(message)
{
alert(message);
}
function showwinformsmessage() {
var msg = document.getelementbyid('txtmessage').value;
return window.external.showmessage(msg);
}
</script>
</head>
<body>
<input type="text" id="txtmessage" />
<input type="button" value="show message" onclick="showwinformsmessage()" />
</body>
</html>
复制代码 代码如下:
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
using system.runtime.interopservices;
namespace webbrowserjavascriptexample
{
public partial class frmmain : form
{
public frmmain()
{
initializecomponent();
webbrowser1.objectforscripting = new scriptmanager(this);
}
private void btnshowmessage_click(object sender, eventargs e)
{
object[] o = new object[1];
o[0]=txtmessage.text;
object result = this.webbrowser1.document.invokescript("showmessage", o);
}
private void frmmain_load(object sender, eventargs e)
{
this.webbrowser1.navigate(@"e:\projects\2010\webbrowserjavascriptexample\webbrowserjavascriptexample\testpage.htm");
}
[comvisible(true)]
public class scriptmanager
{
frmmain _form;
public scriptmanager(frmmain form)
{
_form = form;
}
public void showmessage(object obj)
{
messagebox.show(obj.tostring());
}
}
}
}