플래시는 UI 측면에서 강력한 디스플레이 객체 중의 하나입니다.
플래시와 어플 웹 브라우저의 통신 방법으로 주로 이용되는 방법이
fscommand
입니다.
최근에는
external interface
라는 새로운 방법이 추가되기도 하였지만 여전히 상당수가 사용하는 방법 fscommand 입니다.
플래시에서 어플리케이션에 어떤 명령을 요청하는 경우 fscommand 의 command 와 argument 를 이용하며
어플리케이션에서 플래시를 호출할 경우는 명령어와 xml 경로를 전달해 주는 방식을 주로 사용합니다.
플래시의 제작사인 Adobe 에서도 관련하여 많은 샘플코드를 제공하고는 있지만 주로 브라우저 즉 웹 어플리케이션 연동이나 C# 쪽 예제 중심이지 Visual C++ 용 예제는 별로 제공이 되지 않습니다.
제 경험을 토대로 Visual C++ 과 Flash 의 연동방법에 대해 정리해 보았습니다.
다이얼로그 베이스 프로그램에 플래시 오브젝트를 추가하여 연동하는 방법입니다.
1> 플래시 오브젝트를 불러옵니다.
Tools -> Choose ToolBox Items
메뉴를 선택합니다. (로딩되는데 시간이 좀 걸립니다)
Com Components Tab 으로 이동 후
Shockwave Flash Object
를 선택하고 OK 버튼을 클릭합니다,
2>다이얼로그에 플래시 오브젝트를 올립니다.
이름은
IDC_FLASH_MAIN 로 합니다.
3>플래시 wrapper Class 용 파일(flash_main.cpp, flash_main.h)을 프로젝트에 추가합니다.
4>fscommand 연동 관련 작업을 진행합니다.
*참고로 저는
CMobileShellDlg
라는 다이얼로그를 만들어 작업을 했습니다.
다이얼로그 헤더 파일에 아래와 같이 추가해줍니다.
#include "UI/flash_main.h"
#pragma once
// CMobileShellDlg dialog
class CMobileShellDlg : public CDialog
{
// Construction
public:
CMobileShellDlg(CWnd* pParent = NULL); // standard constructor
virtual ~CMobileShellDlg();
// Dialog Data
enum { IDD = IDD_MOBILESHELL_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
// Generated message map functions
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
public:
CFlash_main m_Flash_Main; //플래시 오브젝트
CStringArray m_CommandArray; //플래시와 통신하기 위해 명령어들이 저장되는 Array
public: // Flash
DECLARE_EVENTSINK_MAP()
void FSCommandFlashMain(LPCTSTR command, LPCTSTR args); //플래시에서 쉘로 보내는 명령 void Send_FlashFunction(); //쉘에서 플래시로 명령을 보내는 부분
public:
virtual BOOL DestroyWindow();
};
다이얼로그 cpp 파일을 아래와 같이 작업해 줍니다.
// MobileShellDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MobileShell.h"
#include "MobileShellDlg.h"
#include <afxmt.h>
CCriticalSection g_csFlashCallFunction;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CMobileShellDlg dialog
CMobileShellDlg::CMobileShellDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMobileShellDlg::IDD, pParent)
, m_bLoadMainFlash(false)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bLoadMainFlash = FALSE;
}
CMobileShellDlg::~CMobileShellDlg()
{
m_CommandArray.RemoveAll();
}
void CMobileShellDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_FLASH_MAIN, m_Flash_Main); //IDC_FLASH_MAIN 와 m_Flash_Main 을 연결
BEGIN_MESSAGE_MAP(CMobileShellDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CMobileShellDlg message handlers//플래시 메시지 핸들러 부분
_EVENTSINK_MAP(CMobileShellDlg, CDialog)
ON_EVENT(CMobileShellDlg, IDC_FLASH_MAIN, 150, CMobileShellDlg::FSCommandFlashMain, VTS_BSTR VTS_BSTR)
END_EVENTSINK_MAP()
BOOL CMobileShellDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
//메인 플래시 파일을 로딩하는 부분
TCHAR Buffer[BUFSIZE];
GetCurrentDirectory(BUFSIZE, Buffer);
wcscat_s(Buffer,BUFSIZE,_T("\\sample.swf"));
m_Flash_Main.LoadMovie(0, Buffer); //현재 디렉토리에 있는 sample.swf 파일을 로딩합니다
return TRUE; // return TRUE unless you set the focus to a control
}
//실제 플래시에서 쉘로 보내는 메시지를 받는 부분입니다
void CMobileShellDlg::FSCommandFlashMain(LPCTSTR command, LPCTSTR args)
{
CString sCmd, sArgs; //fscommand 로 날라오는 command 와 argument
sCmd.Format(_T("%s"), command);
sArgs.Format(_T("%s"), args);
CString sFilePath;
TCHAR Buffer[BUFSIZE];
GetCurrentDirectory(BUFSIZE, Buffer);
if( sCmd.CompareNoCase(_T("command1")) == 0 )
//command 명이 command1 일때 아래 코드를 실행합니다.
{
sFilePath.Format(_T("%s"),Buffer);
sFilePath = sFilePath + _T("\\test.xml");
CString sRequest; //실제 플래시에 있는 명령을 호출하기 위해 작업하는 부분입니다.
sRequest.Format(_T("<invoke name=\"responseall\"><arguments><string><![CDATA[%s]]></string></arguments></invoke>"), sFilePath);
//어플리케이션에서 호출할 플래시함수명이 responseall 이며 참조할 xml 파일이 test.xml 이라는 의미입니다.//플래시에 responseall 이라는 외부함수가 존재하지 않는다면 exception에 의해 memory leak 이 발생합니다.
//플래시에 리턴할 명령을 commandArray 객체에 넣습니다.
m_CommandArray.Add(sRequest);
Send_FlashFunction();
//실제 플래시메 명령을 전송합니다.
}
}
//실제로 플래시에 명령을 보내는 부분
void CMobileShellDlg::Send_FlashFunction()
{
CString sReturn;
sReturn.Empty();
try
{
if( m_Flash_Main.GetSafeHwnd() != NULL )
sReturn = m_Flash_Main.CallFunction(sRrequest); //실제로 플래시에 명령을 보내는 부분입니다. //flash_main.h 파일의 callFunction 부분을 참조하시면 됩니다 }
catch(...)
//호출하는 함수명이 플래시내부에 존재하지 않는 경우 이부분에서 예외가 발생합니다
{
}
}
while(FALSE);
}
대략적인 방법을 설명드렸습니다.
완전한 소스 원하시는 분들은 메일 주시면 보내 드리겠습니다.
fscommand 에 대해 좀더 자세히 아시고 싶으신 분들은 아래 URL 을 참조하시기 바랍니다.
http://livedocs.adobe.com/flex/3/html/help.html?content=19_External_Interface_01.html