{"id":2884,"date":"2024-11-10T16:16:49","date_gmt":"2024-11-10T08:16:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2884\/"},"modified":"2024-11-10T16:16:49","modified_gmt":"2024-11-10T08:16:49","slug":"winform-%e5%b5%8c%e5%85%a5-html-%e5%90%8e%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8-js-%e5%87%bd%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2884\/","title":{"rendered":"WinForm \u5d4c\u5165 HTML \u540e\u5982\u4f55\u8c03\u7528 JS \u51fd\u6570\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173090267212282.jpg\" class=\"aligncenter\" title=\"WinForm \u5d4c\u5165 HTML \u540e\u5982\u4f55\u8c03\u7528 JS \u51fd\u6570\uff1f\u63d2\u56fe\" alt=\"WinForm \u5d4c\u5165 HTML \u540e\u5982\u4f55\u8c03\u7528 JS \u51fd\u6570\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>winform \u5d4c\u5165 html \u9875\u9762\u540e\u5982\u4f55\u8c03\u7528 js<\/strong><\/p>\n<p>\u5728\u5c06 winform \u754c\u9762\u5d4c\u5165\u5230 html \u9875\u9762\u540e\uff0c\u82e5\u8981\u8ba9 winform \u8c03\u7528\u9875\u9762\u4e2d\u7684 javascript\uff0c\u53ef\u53c2\u8003\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u52a0\u8f7d javascript \u8fd0\u884c\u65f6<\/strong><\/p>\n<p>\u5728 html \u9875\u9762\u4e2d\uff0c\u4f7f\u7528 script \u6807\u7b7e\u52a0\u8f7d javascript \u8fd0\u884c\u65f6\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>&lt;script src=\"runtime.js\"&gt;&lt;\/script&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d runtime.js \u662f\u63d0\u4f9b javascript \u8fd0\u884c\u65f6\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>2. \u5c06 winform \u5d4c\u5165 html<\/strong><\/p>\n<p>\u4f7f\u7528 object \u6216 embed \u6807\u7b7e\u5c06 winform \u5d4c\u5165\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;object id=\"winform\" classid=\"clsid:d69e3e91-d688-4574-bd67-d82c71ee8094\"&gt;\n  &lt;param name=\"src\" value=\"winform.exe\" \/&gt;\n&lt;\/object&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. com \u6865\u63a5\u5668<\/strong><\/p>\n<p>\u4f7f\u7528 com \u6865\u63a5\u5668\u5728 winform \u548c javascript \u4e4b\u95f4\u5efa\u7acb\u901a\u4fe1\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u6865\u63a5\u5668\u7c7b\uff1a<\/p>\n<pre>[comvisible(true)]\npublic class combridge\n{\n    public void calljsfunction(string script)\n    {\n        object obj = runtime.getruntimeobject(\"winform\");\n        obj.invokemember(\"eval\", reflection.bindingflags.invokemethod, null, null, new object[] { script });\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u6ce8\u518c\u6865\u63a5\u5668<\/strong><\/p>\n<p>\u4f7f\u7528 registerforcominterop \u5c06\u6865\u63a5\u5668\u6ce8\u518c\u4e3a com \u5bf9\u8c61\uff1a<\/p>\n<pre>public static void registercominterop()\n{\n    intptr hkey = nativemethods.regopenkeyex(nativemethods.hkey_classes_root, \"clsid\\{d69e3e91-d688-4574-bd67-d82c71ee8094}\", 0, nativemethods.key_write | nativemethods.key_wow64_64key, intptr.zero);\n    if (hkey != intptr.zero)\n    {\n        nativemethods.regsetvalueex(hkey, \"inprocserver32\", 0, nativemethods.reg_sz, $\"{directory.getcurrentdirectory()}\\combridge.dll\", combridge.dll.length * 2);\n        nativemethods.regclosekey(hkey);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u8c03\u7528 js \u51fd\u6570<\/strong><\/p>\n<p>\u5728 winform \u4e2d\uff0c\u53ef\u901a\u8fc7 com \u6865\u63a5\u5668\u8c03\u7528 js \u51fd\u6570\uff1a<\/p>\n<pre>this._comBridge.CallJsFunction(\"alert('Hello JS');\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fWinForm \u5d4c\u5165 HTML \u540e\u5982\u4f55\u8c03\u7528 JS \u51fd\u6570\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>winform \u5d4c\u5165 html \u9875\u9762\u540e\u5982\u4f55\u8c03\u7528 js \u5728\u5c06 winform \u754c\u9762\u5d4c\u5165\u5230 html \u9875\u9762\u540e\uff0c\u82e5\u8981\u8ba9 winform \u8c03\u7528\u9875\u9762\u4e2d\u7684 javascript\uff0c\u53ef\u53c2\u8003\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u52a0\u8f7d javascript \u8fd0\u884c\u65f6 \u5728 html \u9875\u9762\u4e2d\uff0c\u4f7f\u7528 script \u6807\u7b7e\u52a0\u8f7d javascript \u8fd0\u884c\u65f6\uff0c\u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b &lt;script src=&#8221;runtime.js&#8221;&gt;&lt;\/script&gt; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d runtime.js \u662f\u63d0\u4f9b javascript \u8fd0\u884c\u65f6\u7684\u6587\u4ef6\u3002 2. \u5c06 winform \u5d4c\u5165 html \u4f7f\u7528 object \u6216 embed \u6807\u7b7e\u5c06 winform \u5d4c\u5165\uff0c\u4f8b\u5982\uff1a &lt;object id=&#8221;winform&#8221; classid=&#8221;clsid:d69e3e91-d688-4574-bd67-d82c71ee8094&#8243;&gt; &lt;param name=&#8221;src&#8221; value=&#8221;winform.exe&#8221; \/&gt; &lt;\/object&gt; \u767b\u5f55\u540e\u590d\u5236 3. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2884","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=2884"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2884\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}