WTL對 Flash 控件的使用

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)
	{

	}
};


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