【译】用于多媒体应用的无窗口ATL ActiveX控件容器

原文链接:ATL Windowless ActiveX Media Container

      这个ATL activeX框架适用于作为Windows Media Player,Flash以及Sliverlight动画的承载容器。整个框架分布在Windowless文件夹下,共有6个文件。架构如图所示:

使用Adobe Flash Player作为子控件

 

主要代码如下:

class CMainDlg : public CAxWindowlessHost<CMainDlg>


LRESULT CMainDlg::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);

    TCHAR szPath[MAX_PATH] 
= { 0 };
    
if ( ::GetModuleFileName(NULL, szPath, MAX_PATH) > 0 )
    {
//定位.swf文件
        LPTSTR pszSep = _tcsrchr(szPath, TCHAR('\\'));
        pszSep
++;
        
*pszSep = 0;
        ::StringCchCat(szPath, MAX_PATH, _T(
"Construction.swf"));
    }

    
// Initialize Flash Player (Shockwave .swf)
    HRESULT hr;
    ActiveXSite
* pSite;
    pSite 
= CAxWindowlessHost<CMainDlg>::GetControlSite(IDC_SHOCKWAVEFLASH);//获取flash player子控件
    if ( pSite != NULL )
    {
        CComQIPtr
<IShockwaveFlash> spFlash = pSite->ActiveXControl();
        hr 
= spFlash->put_WMode( CComBSTR("Transparent") );//设置“透明”
        hr = spFlash->put_Movie( CComBSTR(szPath) );//设置影片源文件
    }
    
return TRUE;
}

使用Windows Meida Player作为子控件

 

主要代码如下:

LRESULT CMainDlg::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);

    
// Initialize Windows Media Player
    HRESULT hr;
    ActiveXSite
* pSite;
    pSite 
= CAxWindowlessHost<CMainDlg>::GetControlSite(IDC_WMP11);//获取Meida Player子控件
    if ( pSite != NULL )
    {
        pSite
->SetAllowResize(false);//不允许自动调整大小
        CComQIPtr<IWMPCore> wmp = pSite->ActiveXControl();
        CComQIPtr
<IWMPPlayer4> wmp4 = pSite->ActiveXControl();
        
// this can be done manually as well
        if ( wmp4 ) 
        {
            hr 
= wmp4->put_windowlessVideo( VARIANT_TRUE );//无窗口模式
            
//hr = wmp4->put_uiMode( CComBSTR("Full") );//填充模式
        }
        
//hr = wmp->put_URL( CComBSTR("c:\\temp\\videofile.wmv") );//多媒体文件地址
    }

    
return TRUE;
}

LRESULT CMainDlg::OnBnClickedBtnBrowse(WORD 
/*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
//选择多媒体文件
    static LPCTSTR pszFilter = _T("Video Files (*.avi;*.mpg;*.wmv)\0*.avi;*.mpg;*.wmv\0\0");
    CFileDialog fileOpen(TRUE, _T(
"avi"), NULL,
        OFN_HIDEREADONLY 
| OFN_OVERWRITEPROMPT, pszFilter/*, m_hWnd*/);
    
if ( IDOK == fileOpen.DoModal() )
    {
        ActiveXSite
* pSite;
        pSite 
= CAxWindowlessHost<CMainDlg>::GetControlSite(IDC_WMP11);
        SetDlgItemText(IDC_TXT_FILENAME,  fileOpen.m_szFileName);
        CComQIPtr
<IWMPPlayer4> wmp4 = pSite->ActiveXControl();
        wmp4
->close();
        wmp4
->put_URL( CComBSTR( fileOpen.m_szFileName ) );
    }

    
return 0;
}

 使用Sliverlight作为子控件

 

主要代码如下:

LRESULT CMainDlg::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);

    
// Initialize Silverlight Control (this is not windowless but cool anyway)
    HRESULT hr;
    ActiveXSite
* pSite;
    pSite 
= CAxWindowlessHost<CMainDlg>::GetControlSite(IDC_AGCONTROL1);//获取SliverLight子控件
    if ( pSite != NULL )
    {
        
// disable right-click!
        pSite->SetAllowRClick(false);//禁止掉右键
        
// load from URL
        
//CComBSTR bstrUrl("file:///c:\\temp\\SortTheFootbars.xap");
        CComBSTR bstrUrl("http://www.andybeaulieu.com/silverlight/2.0/sortthefoobars/ClientBin/SortTheFoobars.xap");
        pSite
->SetUrl(bstrUrl);//设置多媒体源文件
        CComQIPtr<IXcpControl2> slight = pSite->ActiveXControl();
        hr 
= slight->put_Source(bstrUrl);
    }
    
return TRUE;
}

 

posted on 2009-03-04 14:20  Phinecos(洞庭散人)  阅读(3701)  评论(0编辑  收藏  举报

导航