出自ProgWiki
用途
程式碼
HRESULT PlayMovie(LPTSTR lpszMovie, LPCTSTR lpWindowName)
{
// we will use several DirectShow interfaces
HRESULT hr;
OAHWND hWnd;
RECT rect;
LONG lMode;
static HWND hDrain=0;
IMediaControl *pMC = NULL;
IGraphBuilder *pGB = NULL;
IMediaEventEx *pME = NULL;
IVideoWindow *pVW = NULL;
long evCode; // something to hold a returned event code
// instantiate a filter graph as in-proc server
WCHAR wFile[MAX_PATH];
if (lpWindowName == NULL)
hWnd = (OAHWND)::GetActiveWindow();
else
hWnd = (OAHWND)::FindWindow(NULL,lpWindowName);
// UNICODE
MultiByteToWideChar( CP_ACP, 0, lpszMovie, -1, wFile, sizeof(wFile)/sizeof(wFile[0]));
CoInitialize(NULL);
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IMediaControl, (void **)&pMC);// we'll use this interface to build the graph
hr = pMC->QueryInterface(IID_IGraphBuilder, (void **)&pGB);// we'll want to wait for completion of the rendering, so we need a media event interface
hr = pMC->QueryInterface(IID_IMediaEventEx, (void **)&pME);// now we're ready to build the filter graph based on the source file data types
hr = pGB->RenderFile(wFile, NULL);// play the source file
hr = pMC->QueryInterface(IID_IVideoWindow, (void **)&pVW);
hr = pVW->put_Owner(hWnd);
hr = pVW->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
hr = pVW->HideCursor(OATRUE);
pVW->get_FullScreenMode(&lMode);
if (lMode == OAFALSE)
{
lMode = OAFALSE;
pVW->put_FullScreenMode(lMode);
// Undo change of message drain
pVW->put_MessageDrain((OAHWND) hDrain);
// Reset video window
pVW->SetWindowForeground(-1);
::GetClientRect((HWND)hWnd, &rect);
pVW->SetWindowPosition(rect.left, rect.top, rect.right, rect.bottom);
// Reclaim keyboard focus for player application
::UpdateWindow((HWND)hWnd);
::SetForegroundWindow((HWND)hWnd);
::SetFocus((HWND)hWnd);
hr = pMC->Run();// block application until video rendering operations finish
hr = pME->WaitForCompletion(INFINITE, &evCode);// release interfaces
}
else
{
// Switch back to windowed mode
lMode = OAFALSE;
pVW->put_FullScreenMode(lMode);
// Undo change of message drain
pVW->put_MessageDrain((OAHWND) hDrain);
// Reset video window
pVW->SetWindowForeground(-1);
::GetClientRect((HWND)hWnd, &rect);
pVW->SetWindowPosition(rect.left, rect.top, rect.right, rect.bottom);
// Reclaim keyboard focus for player application
::UpdateWindow((HWND)hWnd);
::SetForegroundWindow((HWND)hWnd);
::SetFocus((HWND)hWnd);
hr = pMC->Run();// block application until video rendering operations finish
hr = pME->WaitForCompletion(INFINITE, &evCode);// release interfaces
}
pVW->Release();
pME->Release();
pGB->Release();
pMC->Release();
CoUninitialize();
return (hr);
}