出处:
http://blog.csdn.net/lifesoftware/article/details/5389265
http://www.cnblogs.com/naiking/archive/2009/01/14/1375771.html
1、添加组件
打开VS2008,工具-选择工具箱项-COM组件,勾选Shockwave Flash Object,确定。
2、将Flash组件放入窗体中
将工具箱中的Shockwave Flash Object组件拖放到窗体中,设置其属性。
3、as代码
import flash.external.ExternalInterface;
//向C#发送数据
if (ExternalInterface.available)
{
var strResult:String = ExternalInterface.call("userValidate", userid, password);
}
//接收C#返回的结果
if (ExternalInterface.available)
{
ExternalInterface.addCallback("userValidate", userValidate);
}
//根据C#返回的结果处理
public function userValidate(str:String):void
{
trace(str);
}
//向C#发送数据
if (ExternalInterface.available)
{
var strResult:String = ExternalInterface.call("userValidate", userid, password);
}
//接收C#返回的结果
if (ExternalInterface.available)
{
ExternalInterface.addCallback("userValidate", userValidate);
}
//根据C#返回的结果处理
public function userValidate(str:String):void
{
trace(str);
}
4、c#代码
public mainForm()
{
InitializeComponent();
mainFlash.Movie = Application.StartupPath + "\\swf\\DayBook.swf";//设置flash地址
mainFlash.Menu = false;
mainFlash.FlashCall += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(mainFlash_FlashCall);
}
private void mainFlash_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)
{
XmlDocument document = new XmlDocument();
document.LoadXml(e.request);
XmlAttributeCollection attributes = document.FirstChild.Attributes;
// 获取函数名
String command = attributes.Item(0).InnerText;
// 获取参数
XmlNodeList list = document.GetElementsByTagName("arguments");
int count = list[0].ChildNodes.Count;//参数数量
string[] arr_paras = new string[count];
//处理参数
for (int i = 0; i < count; ++i)
{
arr_paras[i] = list[0].ChildNodes[i].InnerText.ToString();
}
switch (command)
{
case "userValidate":
userValidate(arr_paras, "userValidate");
break;
case "getPeopleList":
getPeopleList(arr_paras, "getPeopleList");
break;
}
}
//C#传给Flash的值
private void callFunction(string funName, string arg)
{
//funName:调用as中的函数
//arg:参数
mainFlash.CallFunction("<invoke name=\"" + funName + "\" returntype=\"xml\"><arguments><string>" + arg + "</string></arguments></invoke>");
}
//登录验证
public void userValidate(string[] arr_data, string functionName)
{
string password = arr_data[1];
password = OperClass.Encrypt(password, 1);
string sql = "select id,username from users where state='0' and userid=" + OperClass.str(arr_data[0]) + " and password=" + OperClass.str(password);
DataSet ds = dbc.ExecuteSqlDsForAccess(sql, "info");
string result = "";
if (ds != null && ds.Tables["info"].Rows.Count > 0)
{
result = ds.Tables["info"].Rows[0]["id"].ToString() + "㊣" + ds.Tables["info"].Rows[0]["username"].ToString();
}
else
{
result = "-1";
}
callFunction(functionName, result);
}
{
InitializeComponent();
mainFlash.Movie = Application.StartupPath + "\\swf\\DayBook.swf";//设置flash地址
mainFlash.Menu = false;
mainFlash.FlashCall += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(mainFlash_FlashCall);
}
private void mainFlash_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)
{
XmlDocument document = new XmlDocument();
document.LoadXml(e.request);
XmlAttributeCollection attributes = document.FirstChild.Attributes;
// 获取函数名
String command = attributes.Item(0).InnerText;
// 获取参数
XmlNodeList list = document.GetElementsByTagName("arguments");
int count = list[0].ChildNodes.Count;//参数数量
string[] arr_paras = new string[count];
//处理参数
for (int i = 0; i < count; ++i)
{
arr_paras[i] = list[0].ChildNodes[i].InnerText.ToString();
}
switch (command)
{
case "userValidate":
userValidate(arr_paras, "userValidate");
break;
case "getPeopleList":
getPeopleList(arr_paras, "getPeopleList");
break;
}
}
//C#传给Flash的值
private void callFunction(string funName, string arg)
{
//funName:调用as中的函数
//arg:参数
mainFlash.CallFunction("<invoke name=\"" + funName + "\" returntype=\"xml\"><arguments><string>" + arg + "</string></arguments></invoke>");
}
//登录验证
public void userValidate(string[] arr_data, string functionName)
{
string password = arr_data[1];
password = OperClass.Encrypt(password, 1);
string sql = "select id,username from users where state='0' and userid=" + OperClass.str(arr_data[0]) + " and password=" + OperClass.str(password);
DataSet ds = dbc.ExecuteSqlDsForAccess(sql, "info");
string result = "";
if (ds != null && ds.Tables["info"].Rows.Count > 0)
{
result = ds.Tables["info"].Rows[0]["id"].ToString() + "㊣" + ds.Tables["info"].Rows[0]["username"].ToString();
}
else
{
result = "-1";
}
callFunction(functionName, result);
}