MFC和 WTL 对Flash 控件的使用,都差不多,以WTL为例进行说明:
1. 首先使用向导创建一个基于对话框的应用程序,然后在 在maindlg 创建之前加入 AtlAxWinInit(),
2. 在主对话框中增加 CAxWindow m_PlayerHostWnd;
CComPtr<IShockwaveFlash> m_FlashPlayer;
3. 在资源中增加一个Flash控件,右键菜单,Insert ActiveX control 的哪项,然后进行拖放大小。
4.
// Load the flash player
m_PlayerHostWnd = GetDlgItem( IDC_SHOCKWAVEFLASH_PLAYER );
LRESULT lResult = m_PlayerHostWnd.QueryControl( &m_FlashPlayer );
if ( FAILED( lResult ) )
{
MessageBox(L"Can not Load the active X");
return FALSE;
}
如下的语句可以初始化完毕了,
5. _bstr_t strMovieName( strPath.GetBuffer(strPath.GetLength()) );
lResult = m_FlashPlayer->put_Movie( strMovieName );
就是播放了。
6. 增加Flash的Event响应,那么就要将自己的类派生自IDispEventImpl<IDC_SHOCKWAVEFLASH_PLAYER,CMainDlg>
class CMainDlg : public CAxDialogImpl<CMainDlg>,
public IDispEventImpl<IDC_SHOCKWAVEFLASH_PLAYER,CMainDlg>
7. 增加如下代码在 LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) 中
AtlAdviseSinkMap( this, true );
8. 使用
BEGIN_SINK_MAP(CMainDlg)
SINK_ENTRY(IDC_SHOCKWAVEFLASH_PLAYER, 150, HandleFSCommand)
SINK_ENTRY(IDC_SHOCKWAVEFLASH_PLAYER, 1958, OnFlashPlayerProgress)
END_SINK_MAP()
建立 事件处理分配表,并且实现响应的函数。
9. 一定要记得在关闭窗口的时候,或者窗口类销毁的时候,调用AtlAdviseSinkMap( this, false);
// MainDlg.h : interface of the CMainDlg class // ///////////////////////////////////////////////////////////////////////////// #pragma once #include <atlstr.h> #include "resource.h" class CMainDlg : public CAxDialogImpl<CMainDlg>, public IDispEventImpl<IDC_SHOCKWAVEFLASH_PLAYER,CMainDlg> { public: enum { IDD = IDD_MAINDLG }; BEGIN_MSG_MAP(CMainDlg) MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog) COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout) COMMAND_ID_HANDLER(IDOK, OnOK) COMMAND_ID_HANDLER(IDCANCEL, OnCancel) END_MSG_MAP() BEGIN_SINK_MAP(CMainDlg) SINK_ENTRY(IDC_SHOCKWAVEFLASH_PLAYER, 150, HandleFSCommand) SINK_ENTRY(IDC_SHOCKWAVEFLASH_PLAYER, 1958, OnFlashPlayerProgress) END_SINK_MAP() CAxWindow m_PlayerHostWnd; CComPtr<IShockwaveFlash> m_FlashPlayer; // Handler prototypes (uncomment arguments if needed): // LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) // LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) // LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/) LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) { // center the dialog on the screen CenterWindow(); // set icons HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR); SetIcon(hIcon, TRUE); HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR); SetIcon(hIconSmall, FALSE); // Load the flash player m_PlayerHostWnd = GetDlgItem( IDC_SHOCKWAVEFLASH_PLAYER ); LRESULT lResult = m_PlayerHostWnd.QueryControl( &m_FlashPlayer ); if ( FAILED( lResult ) ) { MessageBox(L"Can not Load the active X"); return FALSE; } TCHAR szBuffer[1024] = {0}; GetModuleFileName((HMODULE)&__ImageBase, szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])); TCHAR *pFind = _tcsrchr( szBuffer, '\\'); *(pFind + 1) = 0; CString strPath = szBuffer; strPath.Append( L"preview7us.swf" ); _bstr_t strMovieName( strPath.GetBuffer(strPath.GetLength()) ); strPath.ReleaseBuffer( -1 ); /*DispEventAdvise( m_FlashPlayer );*/ AtlAdviseSinkMap( this, true ); lResult = m_FlashPlayer->put_Movie( strMovieName ); if ( FAILED( lResult) ) { MessageBox(L"can not open the flash"); } m_FlashPlayer->Stop(); m_FlashPlayer->Play(); return TRUE; } LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) { CSimpleDialog<IDD_ABOUTBOX, FALSE> dlg; dlg.DoModal(); return 0; } LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) { if ( !!m_FlashPlayer ) { m_FlashPlayer->Stop(); } EndDialog(wID); return 0; } LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) { if ( !!m_FlashPlayer ) { m_FlashPlayer->Stop(); } EndDialog(wID); return 0; } void __stdcall HandleFSCommand(BSTR command, BSTR args) { } void __stdcall OnFlashPlayerProgress(long percentDone) { } };