📄 mpcontainer.cpp
字号:
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::ReleaseDC()
// Desc: This function releases a device context previously acquired by a
// call to GetDC().
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::ReleaseDC(HDC hDC)
{
::ReleaseDC(m_hWnd, hDC);
return S_OK;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::InvalidateRect()
// Desc: This function causes a portion of the window to be redrawn.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::InvalidateRect(LPCRECT pRect, BOOL fErase)
{
::InvalidateRect(m_hWnd, pRect, fErase);
return S_OK;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::InvalidateRgn()
// Desc: Not implemented.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::InvalidateRgn(HRGN, BOOL)
{
return E_NOTIMPL;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::ScrollRect()
// Desc: Not implemented.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::ScrollRect(INT, INT, LPCRECT, LPCRECT)
{
return E_NOTIMPL;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::AdjustRect()
// Desc: Not implemented.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::AdjustRect(LPRECT)
{
return E_NOTIMPL;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::OnDefWindowMessage()
// Desc: Not implemented.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::OnDefWindowMessage(UINT, WPARAM, LPARAM, LRESULT *)
{
return E_NOTIMPL;
}
//////
// Methods defined in CMPContainer
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::CreateControl()
// Desc: This function attempts to create a MediaPlayer control as an
// in-place object.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::CreateControl(REFIID rclsid)
{
HRESULT hResult;
IOleObject *pIOleObject = NULL;
// Translate the IID to a CLSID
hResult = CoCreateInstance(rclsid, NULL,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
IID_IUnknown, reinterpret_cast<LPVOID*>(&m_pUnk));
if (FAILED(hResult))
{
return hResult;
}
hResult = m_pUnk->QueryInterface(IID_IOleObject, reinterpret_cast<LPVOID*>(&pIOleObject));
if (SUCCEEDED(hResult))
{
pIOleObject->SetClientSite(this);
// Tell the control to show itself
pIOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, this, 0, m_hWnd, &m_rcPos);
pIOleObject->DoVerb(OLEIVERB_SHOW, NULL, this, 0, m_hWnd, &m_rcPos);
pIOleObject->Release();
#ifndef UNDER_CE
hResult = E_FAIL;
m_pIPOW = NULL;
#else
hResult = m_pUnk->QueryInterface(IID_IOleInPlaceObjectWindowless, reinterpret_cast<void**>(&m_pIPOW));
#endif // !under_ce
if (FAILED(hResult))
{
// Not windowless, so try this
hResult = m_pUnk->QueryInterface(IID_IOleInPlaceObject, reinterpret_cast<void**>(&m_pIPO));
}
// cache our favorite interface
hResult = m_pUnk->QueryInterface(IID_IMediaPlayer, reinterpret_cast<LPVOID*>(&m_pMP));
}
if( SUCCEEDED( hResult ) )
{
// Put the appropriate tool-bars and controls onto the player
UpdateDisplay();
// Set the autostart property to TRUE
m_pMP->put_AutoStart( TRUE );
m_pMP->put_AutoSize( TRUE );
m_pMP->put_AutoRewind( TRUE );
// Set up the player-control for play-forever
g_pPlayerWindow->ApplyPlayCount();
// Setup the player-control for desired zoom-level
g_pPlayerWindow->ApplyZoomLevel();
}
return hResult;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::DestroyControl()
// Desc: This function releases and all holds on the MediaPlayer control
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::DestroyControl ()
{
HRESULT hr = S_OK;
if (NULL != m_pUnk)
{
IOleObject *pIOleObject = NULL;
m_pUnk->QueryInterface(IID_IOleObject, reinterpret_cast<LPVOID*>(&pIOleObject));
if (pIOleObject)
{
pIOleObject->DoVerb(OLEIVERB_HIDE, NULL, this, 0, m_hWnd, &m_rcPos);
pIOleObject->SetClientSite(NULL);
pIOleObject->Release();
}
m_pUnk->Release();
m_pUnk = NULL;
}
if (NULL != m_pMP)
{
m_pMP->Release();
m_pMP = NULL;
}
if (NULL != m_pIPO)
{
m_pIPO->Release();
m_pIPO = NULL;
}
if (NULL != m_pIPOW)
{
m_pIPOW->Release();
m_pIPOW = NULL;
}
return hr;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::SetWindow()
// Desc: This function stores the window associated with the container.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::SetWindow(HWND hWnd)
{
m_hWnd = hWnd;
if (GetClientRect(m_hWnd, &m_rcPos) == 0)
{
SetRectEmpty(&m_rcPos);
}
return S_OK;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::Paint()
// Desc: This function redraws the MediaPlayer control.
///////////////////////////////////////////////////////////////////////////////
void CMPContainer::Paint(HDC hDC, RECT *pRect)
{
IViewObjectEx *pVOEx = NULL;
if (NULL != m_pUnk)
{
m_pUnk->QueryInterface(IID_IViewObjectEx, reinterpret_cast<void**>(&pVOEx));
}
if (NULL != pVOEx)
{
RECT *prcPos = pRect;
if (NULL == m_pIPOW)
{
prcPos = &m_rcPos;
}
pVOEx->Draw(DVASPECT_CONTENT, -1, NULL, NULL, NULL, hDC,
reinterpret_cast<RECTL*>(prcPos),
reinterpret_cast<RECTL*>(prcPos), NULL, NULL);
pVOEx->Release();
}
return;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::UpdateDisplay()
// Desc: This function makes sure that the various components of the
// MediaPlayer control are being displayed based on what the user
// wishes to see.
///////////////////////////////////////////////////////////////////////////////
void CMPContainer::UpdateDisplay()
{
RECT rect;
VARIANT_BOOL fTracker;
VARIANT_BOOL fControls;
VARIANT_BOOL fAudioControls;
VARIANT_BOOL fPositionControls;
VARIANT_BOOL fStatusBar;
switch( m_dwLevelOfControls )
{
case 0: // minimal
fTracker = VARIANT_FALSE;
fControls = VARIANT_TRUE;
fAudioControls = VARIANT_TRUE;
fPositionControls = VARIANT_TRUE;
fStatusBar = VARIANT_FALSE;
break;
case 1: // compact
case 2: // standard (same as compact for now...)
default:
fTracker = VARIANT_TRUE;
fControls = VARIANT_TRUE;
fAudioControls = VARIANT_TRUE;
fPositionControls = VARIANT_TRUE;
fStatusBar = VARIANT_TRUE;
break;
case 10: // skin
fTracker = VARIANT_FALSE;
fControls = VARIANT_FALSE;
fAudioControls = VARIANT_FALSE;
fPositionControls = VARIANT_FALSE;
fStatusBar = VARIANT_TRUE;
break;
}
m_pMP->put_ShowTracker(fTracker);
m_pMP->put_EnableTracker(fTracker);
m_pMP->put_ShowControls(fControls);
m_pMP->put_ShowAudioControls(fAudioControls);
m_pMP->put_ShowPositionControls(fPositionControls);
m_pMP->put_EnablePositionControls(fPositionControls);
m_pMP->put_ShowStatusBar(fStatusBar);
GetWindowRect( m_hWnd, &rect );
g_pPlayerWindow->SetMinimumRect( rect );
return;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::OpenFile()
// Desc: This function attempts to open the file given by szFilename.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::OpenFile(TCHAR *szFilename)
{
HRESULT hr = S_FALSE;
BSTR bstrFilename = NULL;
if( m_pMP )
{
m_pMP->put_SendErrorEvents(TRUE);
m_pMP->put_SendWarningEvents(TRUE);
// put_FileName takes a BSTR, and we shouldn't just pass it szFilename
if( bstrFilename = SysAllocString( szFilename ) )
{
hr = m_pMP->put_FileName(bstrFilename);
SysFreeString( bstrFilename );
}
else
{
hr = E_INVALIDARG;
}
}
return hr;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::Close()
// Desc: This function attempts to close a file.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::Close()
{
if (NULL == m_pMP)
{
return S_FALSE;
}
return m_pMP->put_FileName(NULL);
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::Play()
// Desc: This function plays the currently open media clip at the normal rate.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::Play()
{
HRESULT hr;
m_pMP->put_Rate(1.0);
hr = m_pMP->Play();
return hr;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::Pause()
// Desc: This function pauses playback.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::Pause()
{
return m_pMP->Pause();
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::Stop()
// Desc: This function stops playback and resets the position back to the
// begining of the stream.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::Stop()
{
double dDuration = 0.0;
HRESULT hr;
// Stop playback
hr = m_pMP->Stop();
// check to see if the current position needs to be reset
VARIANT_BOOL fDurationValid;
m_pMP->get_Duration(&dDuration);
m_pMP->get_IsDurationValid(&fDurationValid);
if (!fDurationValid) dDuration = 0.0;
// reset to the beginning of the stream
if (SUCCEEDED(hr) && dDuration > 0.0)
{
hr = m_pMP->put_CurrentPosition(0.0);
}
return hr;
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::Mute()
// Desc: This function toggles the mute status of the MediaPlayer.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::Mute()
{
HRESULT hr;
VARIANT_BOOL fMuted;
hr = m_pMP->get_Mute(&fMuted);
if (FAILED(hr))
{
return S_FALSE;
}
// Toggle the mute state
if(VARIANT_FALSE == fMuted)
{
fMuted = VARIANT_TRUE;
}
else
{
fMuted = VARIANT_FALSE;
}
return m_pMP->put_Mute(fMuted);
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::Repeat()
// Desc: This function switchs between repeating a media clip and playing it
// only once.
///////////////////////////////////////////////////////////////////////////////
HRESULT CMPContainer::Repeat()
{
LONG lCount;
HRESULT hr;
hr = m_pMP->get_PlayCount(&lCount);
if (FAILED(hr))
{
return S_FALSE;
}
// This app only allows play once and infinite repeat
if (0 == lCount)
{
lCount = 1;
}
else
{
lCount = 0;
}
return m_pMP->put_PlayCount(lCount);
}
///////////////////////////////////////////////////////////////////////////////
// Name: CMPContainer::IsRepeating()
// Desc: This function tells us if the media player control is set to
// repeat forever (returns true) or play once (returns false).
///////////////////////////////////////////////////////////////////////////////
bool CMPContainer::IsRepeating()
{
long lCount;
HRESULT hr = m_pMP->get_PlayCount(&lCount);
ASSERT(SUCCEEDED(hr));
return (lCount == 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -