VC++整合Flex

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方法

CString strXML="<invoke name=\"changeNewText\" returntype=\"xml\"><arguments><string>Hello world</string></arguments></invoke>";
m_controlFlex.CallFunction(strXML);

//说明:changeNewText为flex方法名 Hello world为参数

五. Flex调用VC++方法

添加m_controlFlex控件的FSCommand事件代码如下:
void CFlexDemoDlg::OnFSCommandShockwaveflash1(LPCTSTR command, LPCTSTR args) 
{
      // 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);
      }
}

void CFlexDemoDlg::fun_useCPlus(CString 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消息,让系统以为鼠标点在标题栏上
           PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x, point.y));
           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.运行效果




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值