
winform 嵌入 html 页面后如何调用 js
在将 winform 界面嵌入到 html 页面后,若要让 winform 调用页面中的 javascript,可参考以下步骤:
1. 加载 javascript 运行时
在 html 页面中,使用 script 标签加载 javascript 运行时,例如:
立即学习“前端免费学习笔记(深入)”;
其中 runtime.js 是提供 javascript 运行时的文件。
2. 将 winform 嵌入 html
使用 object 或 embed 标签将 winform 嵌入,例如:
3. com 桥接器
使用 com 桥接器在 winform 和 javascript 之间建立通信。例如,创建桥接器类:
[comvisible(true)]public class combridge{ public void calljsfunction(string script) { object obj = runtime.getruntimeobject("winform"); obj.invokemember("eval", reflection.bindingflags.invokemethod, null, null, new object[] { script }); }}
4. 注册桥接器
使用 registerforcominterop 将桥接器注册为 com 对象:
public static void registercominterop(){ intptr hkey = nativemethods.regopenkeyex(nativemethods.hkey_classes_root, "clsid{d69e3e91-d688-4574-bd67-d82c71ee8094}", 0, nativemethods.key_write | nativemethods.key_wow64_64key, intptr.zero); if (hkey != intptr.zero) { nativemethods.regsetvalueex(hkey, "inprocserver32", 0, nativemethods.reg_sz, $"{directory.getcurrentdirectory()}combridge.dll", combridge.dll.length * 2); nativemethods.regclosekey(hkey); }}
5. 调用 js 函数
在 winform 中,可通过 com 桥接器调用 js 函数:
this._comBridge.CallJsFunction("alert('Hello JS');");
以上就是WinForm 嵌入 HTML 后如何调用 JS 函数?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1495964.html
微信扫一扫
支付宝扫一扫