C#與Flash互動

來源:互聯網
上載者:User

前段日子公司要求做一個C#與Flash互動的東西,用來C#與簡訊貓通訊將資料傳到Flash上顯示與操作的應用。

第一步C#添加組件
開啟VS2005-工具-選擇工具箱項-COM組件-選擇Shockwave Flash Object-確定
添加好組件往情境上拖放,如果提示註冊需求註冊
c# 註冊控制項-在運行輸入-斷行符號(flash9f.ocx這個檔案以系統中實際檔案為準。)
regsvr32 c:\windows\system32\macromed\flash\flash9f.ocx

第二步將Flash組件拖入情境
將Flash組件拖入情境,設定載入的swf路徑。設定組件id。

第三步AS程式碼片段
剛開始用FSCommand與網頁VBSCRIPT做了一個通訊,以為這樣就能與C#通訊了,結果錯誤了。
還得用ExternalInterface.addCallback,ExternalInterface.call的方法來與C#通訊。

程式碼import flash.external.*;
//向C#發送資料
ExternalInterface.call ("test", "str", Math.random ());
//接受C#發來的資料
ExternalInterface.addCallback ("c2flash", null, c2flash);
function c2flash (s:String)
{
    out_txt.text = s;
}

第四步C#程式碼片段

程式碼private void Form1_Load(object sender, EventArgs e)
{
    flash.Movie = "E:/c2flash.swf";
    flash.FlashCall += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(flash_FlashCall);
}

void flash_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)
{
    string s = nodeXml(e.request.ToString())[0].ChildNodes[0].InnerText.ToString();
    //接受Flash傳來的值
    this.textBox1.Text = s;
    throw new Exception("The method or operation is not implemented.");    
}
private void button1_Click(object sender, EventArgs e)
{
    //向Flash發送資料
    callFunction("c2flash",this.textBox1.Text);
}
private void callFunction(string funName,string arg)
{
    //C#傳給Flash的值
    flash.CallFunction("<invoke name=\"" + funName + "\" returntype=\"xml\"><arguments><string>"+arg+"</string></arguments></invoke>");
}
private XmlNodeList nodeXml(string s)
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(s);
    XmlNodeList list = doc.GetElementsByTagName("arguments");
    return list;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.