winform 通过 html 与swf 交互 简单案例

在上一篇 winform 与 html 交互 简单案例 中讲了winform与html之间的简单交互,接下来的内容是在winform中以html为中转站,实现将swf嵌入winform中并实现交互。

1.首先建立一个fla文件,函数ExternalInterface.addCallback("接口函数:来源js中",返回处理函数:as中接收js数据的相关处理);函数ExternalInterface.call("接口函数:来源js中",参数);

as关键代码:

var num:int;

if(flash.external.ExternalInterface.available)
{
    flash.external.ExternalInterface.addCallback("swfHello",swfHello);
}

btn.addEventListener(MouseEvent.CLICK,clickFun);
function clickFun(e:MouseEvent)
{
    //调用js
    if(flash.external.ExternalInterface.available)
    {
        flash.external.ExternalInterface.call("asCallJs","as 调用 js");
    }
}

function swfHello()
{
    //js返回
    num++;
    txt.text = String("js 调用 as"+num);
}

2.html中需要对as发出的请求进行处理。<style></style>标签中是对html以及将加载的swf进行布局。<object></object>中则是对swf文件进行加载,这里加载的swf文件名为test.swf,而这个swf对象命名为“myFlash”。

html代码:

<html>
    <head>
        <title>this is a test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style type="text/css" media="screen">
        html, body { height:100%; background-color: #000000;}
        body { margin:0; padding:0; overflow:hidden; }
        #flashContent { 100%; height:100%; }
        </style>
        
        <script type ="text/javascript">
            function Hello() {
                 window.external.Hello();//alert("hello");
             }

             function WfToHtml() {
                 alert("wf调用html里面的js函数");
             }

             function swfHello() {
                 try {
                     if (myFlash) {
                         myFlash.swfHello();
                     }
                 } catch (e) { 
                }
             }
        //as调用js js调用c# function asCallJs(str) { alert(
"你好:" + str); window.external.Hello(); } </script> </head> <body> <button id="btn" onclick="Hello()">hello</button> <button id="btn2" onclick="swfHello()">swfHello 触发</button> <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="myFlash" align="middle">       <param name="allowScriptAccess" value="always" />        <param name="movie" value="test.swf" />        <param name="wmode" value="transparent" />        <param name="quality" value="high" />        <param name="bgcolor" value="#ffffff" />        <embed src="test.swf" quality="high" bgcolor="#ffffff" width="100%" height="100%" name="myFlash" align="middle" allowscriptaccess="always" swliveconnect="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />     </object> </body> </html>

 3.至于c#代码同上一篇,未变。

至此,已经实现了如何在winform中通过html(js)加载swf,这样与直接加载swf对比了有了更多的交互,可以外接其他设备通过js同winform以及flash进行交互,实现多端控制。

初学者,内容简单,哈哈。。。

原文地址:https://www.cnblogs.com/zeroLove/p/3912845.html