Flash嵌入纯Win32程序 及 事件接收
但是如果把Falsh嵌入到纯Win32 程序当中并且实现与窗口之间的事件通讯, 这可是一件麻烦事了, 在网上搜索了一大堆相关内容, 却没有找到一个完整的例子. 好不容易才找到一个比较靠谱的, 把它放到Win32代码中, 总算是实现了事件通讯. 代码比较凌乱, 但只要修改一下Flash的路径, 就应该是可以看到效果.
// VC6Flash2.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include <atlbase.h>
extern CComModule _Module;
#include <atlwin.h>
#include <comdef.h>
#pragma comment(lib,"atl")
#define MAX_LOADSTRING 100
#import "C:\windows\system32\Macromed\Flash\Flash10v.ocx"
using namespace ShockwaveFlashObjects;
CComModule _Module;
class FlashSink : public ShockwaveFlashObjects::_IShockwaveFlashEvents
{
public:
LPCONNECTIONPOINT m_ConnectionPoint;
DWORD m_dwCookie;
int m_nRefCount;
public:
FlashSink()
{
m_dwCookie = 0;
m_ConnectionPoint = NULL;
m_nRefCount = 0;
}
virtual ~FlashSink()
{
}
HRESULT Init(CComPtr<IShockwaveFlash> ptrFlash)
{
HRESULT aResult = NOERROR;
LPCONNECTIONPOINTCONTAINER aConnectionPoint = NULL;
if ((ptrFlash->QueryInterface(IID_IConnectionPointContainer, (void**) &aConnectionPoint) == S_OK) &&
(aConnectionPoint->FindConnectionPoint(__uuidof(ShockwaveFlashObjects::_IShockwaveFlashEvents), &m_ConnectionPoint) == S_OK))
{
IDispatch* aDispatch = NULL;
QueryInterface(__uuidof(IDispatch), (void**) &aDispatch);
if (aDispatch != NULL)
{
aResult = m_ConnectionPoint->Advise((LPUNKNOWN)aDispatch, &m_dwCookie);
aDispatch->Release();
}
}
if (aConnectionPoint != NULL)
aConnectionPoint->Release();
return aResult;
}
HRESULT Shutdown()
{
HRESULT aResult = S_OK;
if (m_ConnectionPoint)
{
if (m_dwCookie)
{
aResult = m_ConnectionPoint->Unadvise(m_dwCookie);
m_dwCookie = 0;
}
m_ConnectionPoint->Release();
m_ConnectionPoint = NULL;
}
return aResult;
}
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID* ppv)
{
*ppv = NULL;
if (riid == IID_IUnknown)
{
*ppv = (LPUNKNOWN)this;
AddRef();
return S_OK;
}
else if (riid == IID_IDispatch)
{
*ppv = (IDispatch*)this;
AddRef();
return S_OK;
}
else if (riid == __uuidof(ShockwaveFlashObjects::_IShockwaveFlashEvents))
{
*ppv = (ShockwaveFlashObjects::_IShockwaveFlashEvents*) this;
AddRef();
return S_OK;
}
else
{
return E_NOTIMPL;
}
}
ULONG STDMETHODCALLTYPE AddRef()
{
return ++m_nRefCount;
}
ULONG STDMETHODCALLTYPE Release()
{
int aRefCount = --m_nRefCount;
if (aRefCount == 0)
delete this;
return aRefCount;
}
// IDispatch method
virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(UINT* pctinfo)
{
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
{
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames,
UINT cNames, LCID lcid,DISPID* rgDispId)
{
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE Invoke(DISPID dispIdMember, REFIID riid, LCID lcid,
WORD wFlags, ::DISPPARAMS __RPC_FAR *pDispParams, VARIANT __RPC_FAR *pVarResult,
::EXCEPINFO __RPC_FAR *pExcepInfo, UINT __RPC_FAR *puArgErr)
{
switch(dispIdMember)
{
case 0x7a6:
break;
case 0x96:
if ((pDispParams->cArgs == 2) &&
(pDispParams->rgvarg[0].vt == VT_BSTR) &&
(pDispParams->rgvarg[1].vt == VT_BSTR))
{
FSCommand(pDispParams->rgvarg[1].bstrVal, pDispParams->rgvarg[0].bstrVal);
}
break;
case DISPID_READYSTATECHANGE:
break;
default:
return DISP_E_MEMBERNOTFOUND;
}
return NOERROR;
}
HRESULT OnReadyStateChange (long newState)
{
return S_OK;
}
HRESULT OnProgress(long percentDone )
{
return S_OK;
}
HRESULT FSCommand (_bstr_t command, _bstr_t args)
{
// if (m_pFlashWidget != NULL)
// m_pFlashWidget->FlashCommand((char*) command, (char*) args);
return S_OK;
}
};
//Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
CoInitialize(NULL);
AtlAxWinInit();
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_VC6FLASH2, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_VC6FLASH2);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
CoInitialize(NULL);
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDC_VC6FLASH2);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_VC6FLASH2;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
static CAxWindow WinContainer;
static CComPtr<IShockwaveFlash> pFlash;
static FlashSink flashSink;
switch (message)
{
case WM_CREATE:
{
RECT rc = {0, 0, 1024, 768 };
WinContainer.Create(hWnd,rc,TEXT("ShockwaveFlash.ShockwaveFlash"),WS_CHILD|WS_VISIBLE);
HRESULT hr;
hr=WinContainer.QueryControl(__uuidof(IShockwaveFlash),(void**)&pFlash);
if(FAILED(hr)) return -1L;
flashSink.Init(pFlash);
//HWND h = WinContainer();
// pFlash->BackgroundColor = 0x00FFFF;
//hr=pFlash->put_Movie(CComBSTR(L"http://localhost/myweb/Flash/nobk.swf"));
hr=pFlash->LoadMovie(0, L"D:\\myweb\\Flash\\MainMenu2.swf");
pFlash->BackgroundColor = pFlash->BackgroundColor;
}
break;
case WM_LBUTTONDOWN:
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
{
long lColor = pFlash->GetBackgroundColor();
_bstr_t st = pFlash->GetBGColor();
_bstr_t wm = pFlash->WMode;
int i = lColor;
pFlash->put_WMode(L"transparent");
pFlash->put_Menu(FALSE);
}
//DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
pFlash.Release();
WinContainer.DestroyWindow();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
浙公网安备 33010602011771号