VC++整合Flex
1.开发环境
VC++6.0和Adobe Flex Builder 3
2.创建Flex项目firstDemo
firstDemo.mxml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" width="928" height="276"
initialize="registerFunction();">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import flash.external.ExternalInterface;
//注册方法 给VC++调用
public function registerFunction():void{
if(flash.external.ExternalInterface.available){
ExternalInterface.addCallback("changeNewText",changeNewText);
}
Register.text="Registe Already";
}
//flex内部方法
public function changeNewText(newText:String):void{
myText.text=newText;
}
//flex调用VC++的useCPlus方法 传递一个字符串
public function UseCPlus():void{
fscommand("useCPlus","Flex wants to use C++");
}
//flex调用VC++的mouseDown方法 传递鼠标按下的坐标
public function onMouseDown(event:MouseEvent):void{
var s:String=event.localX+":"+event.localY;
//Alert.show(s,"提示");
fscommand("mouseDown",s);
}
//flex调用VC++的closeWnd方法
public function onClose():void{
//Alert.show("关闭","提示");
fscommand("closeWnd","");
}
]]>
</mx:Script>
<mx:TitleWindow x="0" y="0" width="928" height="32" layout="absolute"
showCloseButton="true"
mouseDown="onMouseDown(event);"
close="onClose();">
</mx:TitleWindow>
<mx:TextInput x="137" y="175" id="myText" text="who could change me??" />
<mx:Button x="137" y="124" label="use C++ Fun" click="UseCPlus();" />
<mx:Label x="137" y="78" id="Register" text="Registe your API" />
</mx:Application>
3.创建MFC AppWizard(exe)项目flexDemo
一. 添加com控件
二. 在对话框中插入Shockwave Flash Object控件并创建变量m_contorlFlex
//{{AFX_INCLUDES()
#include "shockwaveflash.h"
//}}AFX_INCLUDES
#if !defined(AFX_FLEXDEMODLG_H__CE750617_916D_4D5E_AAA3_D2146EB7D049__INCLUDED_)
#define AFX_FLEXDEMODLG_H__CE750617_916D_4D5E_AAA3_D2146EB7D049__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/
// CFlexDemoDlg dialog
class CFlexDemoDlg : public CDialog
{
// Construction
public:
CFlexDemoDlg(CWnd* pParent = NULL);// standard constructor
// Dialog Data
//{{AFX_DATA(CFlexDemoDlg)
enum { IDD = IDD_FLEXDEMO_DIALOG };
CShockwaveFlashm_controlFlex;
三. 修改CFlexDemoDlg::OnInitDialog()代码
BOOL CFlexDemoDlg::OnInitDialog()
{
...省略这部分不变的代码
// TODO: Add extra initialization here
ModifyStyle( WS_CAPTION, WS_MINIMIZEBOX, SWP_DRAWFRAME );//屏蔽窗口的标题栏
SetWindowText("flexDemo");//设置窗口标题
AfxEnableControlContainer();//允许嵌入activex控件
m_controlFlex.LoadMovie(0,"E:\\flexWorkspace\\firstDemo\\bin-debug\\firstDemo.swf");//加载flex文件
m_controlFlex.Play();//播放felx文件
return TRUE; // return TRUE unless you set the focus to a control
}
四. VC++调用Flex方法
m_controlFlex.CallFunction(strXML);
五. Flex调用VC++方法
{
// TODO: Add your control notification handler code here
//响应flex端通过fscommand(command,args);调用VC++方法
if(0==strcmp("useCPlus",command)){
fun_useCPlus(args);
}else if(0==strcmp("mouseDown",command)){
fun_mouseDown(args);
}else if(0==strcmp("closeWnd",command)){
fun_closeWnd(args);
}
}
AfxMessageBox(args);
}
void CFlexDemoDlg::fun_mouseDown(CString args){
CString str = args;
CStringArray strArr;
if(DivStr(str,strArr,':') <= 0)
{
AfxMessageBox( _T("参数不合法!"));
return;
}else{
CPoint point=CPoint(atoi(strArr[0]),atoi(strArr[1]));
//向系统发送HTCAPTION消息,让系统以为鼠标点在标题栏上
CDialog::OnLButtonDown(1, point);
}
}
void CFlexDemoDlg::fun_closeWnd(CString args){
this->DestroyWindow();
}
UINT CFlexDemoDlg::DivStr(CString str,CStringArray& Arr,char ch)
{
int nFindposi = str.Find(ch);
if( nFindposi <0 )
return 0;
while( nFindposi > 0)
{
Arr.Add(str.Left(nFindposi) );
str = str.Right( str.GetLength() - nFindposi -1);
str.TrimLeft(ch); //warning
nFindposi = str.Find(ch);
}
if( !str.IsEmpty() )
Arr.Add(str);
return Arr.GetSize();
}
4.运行效果