在上一篇 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 { width: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進行交互,實現多端控制。
初學者,內容簡單,哈哈。。。