⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cwmphost.cpp

📁 WINCE 下的Media Player 播放器控制源代码
💻 CPP
字号:
// CWMPHost.cpp : Implementation of the CWMPHost
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//

#include "stdafx.h"
#include "CWMPHost.h"
#include "dialogs.h"
#include "MediaLibraryDlg.h"

/////////////////////////////////////////////////////////////////////////////
// CWMPHost

void CWMPHost::OnFinalMessage(HWND /*hWnd*/)
{
    ::PostQuitMessage(0);
}

LRESULT CWMPHost::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    AtlAxWinInit();
    CComPtr<IAxWinHostWindow>           spHost;
    CComPtr<IConnectionPointContainer>  spConnectionContainer;
    CComWMPEventDispatch                *pEventListener = NULL;
    CComPtr<IWMPEvents>                 spEventListener;
    HRESULT                             hr;
    RECT                                rcClient;

    m_dwAdviseCookie = 0;

    SHMENUBARINFO mbi;
    ZeroMemory(&mbi, sizeof(SHMENUBARINFO));
    mbi.cbSize = sizeof(SHMENUBARINFO);
    mbi.hwndParent = m_hWnd;
    mbi.nToolBarId = IDR_MENU1;
    mbi.hInstRes = _Module.m_hInst;
    if (!SHCreateMenuBar(&mbi))
    {
       return -1;
    }

    // create window

    GetClientRect(&rcClient);
    m_wndView.Create(m_hWnd, rcClient, NULL, 
        WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
    if (NULL == m_wndView.m_hWnd)
        goto FAILURE;
    
    // load OCX in window

    hr = m_wndView.QueryHost(&spHost);
    if (FAILMSG(hr))
        goto FAILURE;

    hr = spHost->CreateControl(CComBSTR(_T("{6BF52A52-394A-11d3-B153-00C04F79FAA6}")), m_wndView, 0);
    if (FAILMSG(hr))
        goto FAILURE;

    hr = m_wndView.QueryControl(&m_spWMPPlayer);
    if (FAILMSG(hr))
        goto FAILURE;

    // start listening to events

    hr = CComWMPEventDispatch::CreateInstance(&pEventListener);
    spEventListener = pEventListener;
    if (FAILMSG(hr))
        goto FAILURE;

    hr = m_spWMPPlayer->QueryInterface(__uuidof(IConnectionPointContainer), (void**)&spConnectionContainer);
    if (FAILMSG(hr))
        goto FAILURE;

    // See if OCX supports the IWMPEvents interface
    hr = spConnectionContainer->FindConnectionPoint(__uuidof(IWMPEvents), &m_spConnectionPoint);
    if (FAILMSG(hr))
        goto FAILURE;

    hr = m_spConnectionPoint->Advise(spEventListener, &m_dwAdviseCookie);
    if (FAILMSG(hr))
        goto FAILURE;

    return 0;

FAILURE:
    ::PostQuitMessage(0);
    return 0;
}

LRESULT CWMPHost::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    // stop listening to events

    if (m_spConnectionPoint)
    {
        if (0 != m_dwAdviseCookie)
            m_spConnectionPoint->Unadvise(m_dwAdviseCookie);
        m_spConnectionPoint.Release();
    }

    // close the OCX

    if (m_spWMPPlayer)
    {
        m_spWMPPlayer->close();
        m_spWMPPlayer.Release();
    }

    bHandled = FALSE;
    return 0;
}

LRESULT CWMPHost::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& lResult)
{
    RECT rcClient;
    GetClientRect(&rcClient);

    HWND hwndMenu = SHFindMenuBar( m_hWnd );
    RECT rcMenu = { 0 };
    ::GetWindowRect( hwndMenu, &rcMenu );
    if (rcClient.bottom == rcMenu.top)
    {
        //leave room for menu
        rcClient.bottom -= rcMenu.bottom - rcMenu.top;
    }

    m_wndView.MoveWindow(rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
    
    return 0;
}

LRESULT CWMPHost::OnFileOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    CFileOpenDlg dlgOpen;
    HRESULT      hr;

    if (dlgOpen.DoModal(m_hWnd) == IDOK)
    {
        hr = m_spWMPPlayer->put_URL(dlgOpen.m_bstrName);
        if (FAILMSG(hr))
            return 0;
    }
    return 0;
}

LRESULT CWMPHost::OnFileExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    DestroyWindow();
    return 0;
}

LRESULT CWMPHost::OnMediaDlg(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    CMediaLibraryDlg dlg(m_spWMPPlayer);
    dlg.DoModal();

    return 0;
}

LRESULT CWMPHost::OnWMPCoreClose(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    HRESULT     hr;

    hr = m_spWMPPlayer->close();
    if (FAILMSG(hr))
        return 0;

    return 0;
}

LRESULT CWMPHost::OnWMPCoreURL(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    USES_CONVERSION;
    CComBSTR    m_bstrValue;
    HRESULT     hr;

    hr = m_spWMPPlayer->get_URL(&m_bstrValue);
    if (FAILMSG(hr))
        return 0;

    CStringDlg dlgString(_T("IWMPCore->URL"), OLE2T(m_bstrValue));

    if (dlgString.DoModal(m_hWnd) == IDOK)
    {
        hr = m_spWMPPlayer->put_URL(dlgString.m_bstrValue);
        if (FAILMSG(hr))
            return 0;
    }
    return 0;
}

LRESULT CWMPHost::OnWMPCoreOpenState(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    HRESULT      hr;
    WMPOpenState osValue;

    hr = m_spWMPPlayer->get_openState(&osValue);
    if (FAILMSG(hr))
        return 0;

    TCHAR   szValue[MAX_STRING];

    ::wsprintf(szValue, _T("Value = %d"), osValue);
    ::MessageBox(NULL, szValue, _T("IWMPCore->openState"), MB_OK);

    return 0;
}

LRESULT CWMPHost::OnWMPCorePlayState(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    HRESULT      hr;
    WMPPlayState psValue;

    hr = m_spWMPPlayer->get_playState(&psValue);
    if (FAILMSG(hr))
        return 0;

    TCHAR   szValue[MAX_STRING];

    ::wsprintf(szValue, _T("Value = %d"), psValue);
    ::MessageBox(NULL, szValue, _T("IWMPCore->playState"), MB_OK);

    return 0;
}

LRESULT CWMPHost::OnWMPCoreVersionInfo(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    USES_CONVERSION;
    CComBSTR    m_bstrValue;
    HRESULT     hr;

    hr = m_spWMPPlayer->get_versionInfo(&m_bstrValue);
    if (FAILMSG(hr))
        return 0;

    TCHAR   szValue[MAX_STRING];

    ::wsprintf(szValue, _T("Version = %s"), OLE2T(m_bstrValue));
    ::MessageBox(NULL, szValue, _T("IWMPCore->versionInfo"), MB_OK);

    return 0;
}

LRESULT CWMPHost::OnWMPCoreStatus(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    USES_CONVERSION;
    CComBSTR    m_bstrValue;
    HRESULT     hr;

    hr = m_spWMPPlayer->get_status(&m_bstrValue);
    if (FAILMSG(hr))
        return 0;

    TCHAR   szValue[MAX_STRING];

    ::wsprintf(szValue, _T("Status = %s"), OLE2T(m_bstrValue));
    ::MessageBox(NULL, szValue, _T("IWMPCore->status"), MB_OK);

    return 0;
}

LRESULT CWMPHost::OnWMPCoreInterface(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    HRESULT     hr;
    TCHAR       szName[MAX_STRING];
    void        *pUnknown;

    hr = E_FAIL;
    szName[0] = _T('\0');
    pUnknown = NULL;

    switch (wID)
    {
    case ID_WMPCORE_CONTROLS:
        {
            CComPtr<IWMPControls> spWMPControls;

            hr = m_spWMPPlayer->get_controls(&spWMPControls);
            if (spWMPControls)
            {
                spWMPControls->QueryInterface(__uuidof(IWMPControls), &pUnknown);
                lstrcpy(szName, _T("IWMPCore->controls"));
            }
        }
        break;
    case ID_WMPCORE_SETTINGS:
        {
            CComPtr<IWMPSettings> spWMPSettings;

            hr = m_spWMPPlayer->get_settings(&spWMPSettings);
            if (spWMPSettings)
            {
                spWMPSettings->QueryInterface(__uuidof(IWMPSettings), &pUnknown);
                lstrcpy(szName, _T("IWMPCore->settings"));
            }
        }
        break;
    case ID_WMPCORE_CURRENTMEDIA:
        {
            CComPtr<IWMPMedia> spWMPMedia;

            hr = m_spWMPPlayer->get_currentMedia(&spWMPMedia);
            if (spWMPMedia)
            {
                spWMPMedia->QueryInterface(__uuidof(IWMPMedia), &pUnknown);
                lstrcpy(szName, _T("IWMPCore->currentMedia"));
            }
        }
        break;
    case ID_WMPCORE_MEDIACOLLECTION:
        {
            CComPtr<IWMPMediaCollection> spWMPMediaCollection;

            hr = m_spWMPPlayer->get_mediaCollection(&spWMPMediaCollection);
            if (spWMPMediaCollection)
            {
                spWMPMediaCollection->QueryInterface(__uuidof(IWMPMediaCollection), &pUnknown);
                lstrcpy(szName, _T("IWMPCore->currentMediaCollection"));
            }
        }
        break;
    case ID_WMPCORE_PLAYLISTCOLLECTION:
        {
            CComPtr<IWMPPlaylistCollection> spWMPPlaylistCollection;

            hr = m_spWMPPlayer->get_playlistCollection(&spWMPPlaylistCollection);
            if (spWMPPlaylistCollection)
            {
                spWMPPlaylistCollection->QueryInterface(__uuidof(IWMPPlaylistCollection), &pUnknown);
                lstrcpy(szName, _T("IWMPCore->playlistCollection"));
            }
        }
        break;
    case ID_WMPCORE_NETWORK:
        {
            CComPtr<IWMPNetwork> spWMPNetwork;

            hr = m_spWMPPlayer->get_network(&spWMPNetwork);
            if (spWMPNetwork)
            {
                spWMPNetwork->QueryInterface(__uuidof(IWMPNetwork), &pUnknown);
                lstrcpy(szName, _T("IWMPCore->network"));
            }
        }
        break;
    case ID_WMPCORE_CURRENTPLAYLIST:
        {
            CComPtr<IWMPPlaylist> spWMPPlaylist;

            hr = m_spWMPPlayer->get_currentPlaylist(&spWMPPlaylist);
            if (spWMPPlaylist)
            {
                spWMPPlaylist->QueryInterface(__uuidof(IWMPPlaylist), &pUnknown);
                lstrcpy(szName, _T("IWMPCore->currentPlaylist"));
            }
        }
        break;
    case ID_WMPCORE_ERROR:
        {
            CComPtr<IWMPError> spWMPError;

            hr = m_spWMPPlayer->get_error(&spWMPError);
            if (spWMPError)
            {
                spWMPError->QueryInterface(__uuidof(IWMPError), &pUnknown);
                lstrcpy(szName, _T("IWMPCore->error"));
            }
        }
        break;
    default:
        case ID_WMPCORE_CLOSE:
        break;
    }

    if (FAILMSG(hr))
        return 0;

    if (!pUnknown)
    {
        FAILMSG(E_NOINTERFACE);
        return 0;
    }

    ((IUnknown *)pUnknown)->Release();

    ::MessageBox(NULL, _T("Got the expected interface"), szName, MB_OK);

    return 0;
}

LRESULT CWMPHost::OnWMPPlayerEnabled(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    HRESULT      hr;
    VARIANT_BOOL fValue;

    hr = m_spWMPPlayer->get_enabled(&fValue);
    if (FAILMSG(hr))
        return 0;

    CBooleanDlg dlgBoolean(_T("IWMPPlayer->enabled"), fValue);

    if (dlgBoolean.DoModal(m_hWnd) == IDOK)
    {
        hr = m_spWMPPlayer->put_enabled(dlgBoolean.m_fValue);
        if (FAILMSG(hr))
            return 0;
    }

    return 0;
}

LRESULT CWMPHost::OnWMPPlayerFullScreen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    HRESULT      hr;
    VARIANT_BOOL fValue;

    hr = m_spWMPPlayer->get_fullScreen(&fValue);
    if (FAILMSG(hr))
        return 0;

    CBooleanDlg dlgBoolean(_T("IWMPPlayer->fullScreen"), fValue);

    if (dlgBoolean.DoModal(m_hWnd) == IDOK)
    {
        hr = m_spWMPPlayer->put_fullScreen(dlgBoolean.m_fValue);
        if (FAILMSG(hr))
            return 0;
    }

    return 0;
}

LRESULT CWMPHost::OnWMPPlayerUIMode(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
    USES_CONVERSION;
    CComBSTR    m_bstrValue;
    HRESULT     hr;

    hr = m_spWMPPlayer->get_uiMode(&m_bstrValue);
    if (FAILMSG(hr))
        return 0;

    CStringDlg dlgString(_T("IWMPCore->uiMode"), OLE2T(m_bstrValue));

    if (dlgString.DoModal(m_hWnd) == IDOK)
    {
        hr = m_spWMPPlayer->put_uiMode(dlgString.m_bstrValue);
        if (FAILMSG(hr))
            return 0;
    }
    return 0;
}

LRESULT CWMPHost::FowardMsgToWMP(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    HRESULT hr;
    LRESULT llResult = 0;
    CComPtr<IOleInPlaceObjectWindowless> spSite = NULL;

    hr = m_spWMPPlayer->QueryInterface(__uuidof(IOleInPlaceObjectWindowless), (void**)&spSite);
    if( SUCCEEDED(hr) )
    {
        spSite->OnWindowMessage(uMsg, wParam, lParam, &llResult);
    }
    bHandled = TRUE;

    return llResult;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -