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.運行效果




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章