
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');");











