最近由於需要在程序中使用Flash播放,所以學習了下如何播放Flash,這裡使用atl庫中的CAxWindow來處理我們要播放的Flash!由於Flash的很多接口我們都不知道,所以可以
這篇文章中介紹了如何導出Flash的接口,這篇文章主要介紹一下,Flash的時間的通知,以及如何利用windows的API,進行兩個Flash的混合播放!新建一個默認的 win32窗口程序,然後加入後面的頭文件,這裡主要是加入atl庫文件,以及adobe公司的flash的ocx控件。[cpp]
PRE class=cpp name="code"><SPAN style="FONT-SIZE: 14px">#include <atlbase.h>
extern CComModule _Module;
#include <atlwin.h>
#include <comdef.h>
#include "flash32_11_7_700_224.tlh"
#include <string>
using namespace std;
using namespace ShockwaveFlashObjects;</SPAN></PRE><PRE class=cpp name="code"><SPAN style="FONT-SIZE: 14px">CComModule _Module;</SPAN></PRE><SPAN style="FONT-SIZE: 14px"><BR>
</SPAN>
<PRE></PRE>
<SPAN style="FONT-SIZE: 14px">然後我們建立個Flash事件的處理類:</SPAN><PRE class=cpp name="code"><SPAN style="FONT-SIZE: 14px">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)
{
MessageBox(NULL,L"",L"Read",MB_OK);
return S_OK;
}
HRESULT OnProgress(long percentDone )
{
MessageBox(NULL,L"",L"Done",MB_OK);
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;
}
};
</SPAN></PRE><SPAN style="FONT-SIZE: 14px"><BR>
當然這也很簡單,我們只要明白Flash的機制就可以了。後邊是Flash播放的一些簡單的處理</SPAN>
<P></P>
<P></P>
<PRE class=cpp name="code"><SPAN style="FONT-SIZE: 14px">int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代碼。
HRESULT hr = CoInitialize(NULL);
AtlAxWinInit();
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32PROJECT7, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 執行應用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT7));
// 主消息循環:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
CoUninitialize();
return (int) msg.wParam;
}
//
// 函數: MyRegisterClass()
//
// 目的: 注冊窗口類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT7));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT7);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函數: InitInstance(HINSTANCE, int)
//
// 目的: 保存實例句柄並創建主窗口
//
// 注釋:
//
// 在此函數中,我們在全局變量中保存實例句柄並
// 創建和顯示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 將實例句柄存儲在全局變量中
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;
}
//
// 函數: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 處理主窗口的消息。
//
// WM_COMMAND - 處理應用程序菜單
// WM_PAINT - 繪制主窗口
// WM_DESTROY - 發送退出消息並返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
<SPAN style="COLOR: #ff6666">
RECT rc = {0, 0, 300, 300 };
RECT rc1 = {0, 0, 600, 300 };
HWND hwnd_tmp= NULL;
HWND hwnd_tmp1= NULL;
CAxWindow m_pWinContainer;
static CComPtr<IShockwaveFlash> pFlash;
static FlashSink flashSink;
CAxWindow m_pWinContainer1;
static CComPtr<IShockwaveFlash> pFlash1;
static FlashSink flashSink1;</SPAN>
switch (message)
{
<SPAN style="COLOR: #ff6666">case WM_CREATE:
AllocConsole();
freopen("CONOUT$","w+t", stdout);
SetWindowPos(hWnd,NULL,0,0,1024,768,SWP_NOMOVE);
GetClientRect(hWnd,&rc);
GetWindowRect(hWnd,&rc1);
POINT tmpPoint;
tmpPoint.x = rc1.left;
tmpPoint.y = rc1.top;
ScreenToClient(hWnd,&tmpPoint);
rc1.left -= tmpPoint.x;
rc1.top -= tmpPoint.y;
tmpPoint.x = rc.right;
tmpPoint.y = rc.bottom;
ClientToScreen(hWnd,&tmpPoint);
rc1.right = tmpPoint.x;
rc1.bottom = tmpPoint.y;
hwnd_tmp=m_pWinContainer.Create(hWnd,rc,TEXT("ShockwaveFlash.ShockwaveFlash"),WS_CHILD|WS_VISIBLE);
hwnd_tmp1=m_pWinContainer1.Create(hWnd,rc1,TEXT("ShockwaveFlash.ShockwaveFlash"),WS_POPUPWINDOW|WS_VISIBLE,WS_EX_LAYERED);
SetLayeredWindowAttributes(hwnd_tmp1, 0, 123, LWA_ALPHA);
printf("%d",GetLastError());
if (NULL == hwnd_tmp)
{
printf("%s","句柄為空");
}
if (NULL == hwnd_tmp1)
{
printf("%s","窗口2句柄失敗!");
}
HRESULT hr;
hr=m_pWinContainer.QueryControl(__uuidof(IShockwaveFlash),(void**)&pFlash);
m_pWinContainer1.QueryControl(__uuidof(IShockwaveFlash),(void**)&pFlash1);
if(FAILED(hr))
{
return -1L;
}
else
{
flashSink.Init(pFlash);
wchar_t sst[1024];
GetCurrentDirectory(1000,sst);
wprintf(L"%s",sst);
wstring tmppath;
tmppath = sst;
wstring tmppath1;
tmppath1 = tmppath+L"/f146.swf";
wstring tmppath2;
tmppath2 =tmppath+ L"/flash1920.swf";
hr=pFlash->put_Movie((BSTR)tmppath1.c_str());
pFlash1->put_Movie((BSTR)tmppath2.c_str());
if(SUCCEEDED(hr))
{
pFlash->Play();
printf("開始播放!");
}
pFlash1->Play();
}
</SPAN>
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜單選擇:
switch (wmId)
{
case IDM_ABOUT:
//DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
//VARIANT_BOOL isPlayIng;
//pFlash->get_Playing(&isPlayIng);
<SPAN style="COLOR: #ff9966">if (pFlash->IsPlaying())
{
pFlash->Stop();
}
else
{
pFlash->Play();
}
break;
case IDM_EXIT:
if (pFlash1->IsPlaying())
{
pFlash1->Stop();
}
else
{
pFlash1->Play();
}</SPAN>
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意繪圖代碼...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
FreeConsole();
pFlash.Release();
//m_pWinContainer.DestroyWindow();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “關於”框的消息處理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}</SPAN></PRE><SPAN style="FONT-SIZE: 14px" sizcache="0" sizset="31">可以看到紅色部分是被我修改過的地方,這些東西雖然不難,但是自己做起來沒有資料的時候也挺不方便的!這樣Flash的播放就好了,但是不知道為什麼停止和開始播放按鈕對某些Flash動畫不起作用。好了就說這麼多!因為有些童鞋可能比較沒有時間,所以直接附上源碼在最後方便修改和使用!<A href="http://download.csdn.net/detail/xueyunf/5658183">http://download.csdn.net/detail/xueyunf/5658183</A>當然也同樣給出一個運行的結果截圖:<IMG alt="" src="http://img.blog.csdn.net/20130627135400937?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHVleXVuZg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></SPAN>