This article walks through a simple C++ program designed to demonstrate one way to play movies in a particular playback window. It is based on the PlayMovieInWindow function code taken from the InWindow.cpp file, which is available in the InWindow sample in the Samples\Multimedia\DShow\Src\Player directory of the Microsoft® DirectX® Media SDK. This function is based on the Playfile sample, but has been expanded to show how an application can control the size and style of the video playback window.
See these related sections if you want to play back media files or display a property page:
Perform the following steps to play a video file in a particular window from within C/C++. You don't necessarily have to perform the steps in the order presented.
#include <windows.h> #include <mmsystem.h> #include <streams.h> #include "inwindow.h"
#define WM_GRAPHNOTIFY WM_USER+13 #define HELPER_RELEASE(x) { if (x) x->Release(); x = NULL; }
HWND ghApp; HINSTANCE ghInst; HRESULT hr; LONG evCode; LONG evParam1; LONG evParam2; RECT grc;
The ghApp variable is the handle of window to notify when the graph signals an event. The ghInst variable is the HINSTANCE of the window. The evCode variable will hold the event code, and the evParam1 and evParam2 variables will hold the event parameters. The grc variable will hold the coordinates of the parent window's client area.
IGraphBuilder *pigb = NULL; IMediaControl *pimc = NULL; IMediaEventEx *pimex = NULL; IVideoWindow *pivw = NULL;
void PlayMovieInWindow (LPCTSTR szFile) {
WCHAR wFile[MAX_PATH]; MultiByteToWideChar( CP_ACP, 0, szFile, -1, wFile, MAX_PATH );
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pigb);
pigb->QueryInterface(IID_IMediaControl, (void **)&pimc); pigb->QueryInterface(IID_IMediaEventEx, (void **)&pimex); pigb->QueryInterface(IID_IVideoWindow, (void **)&pivw);
hr = pigb->RenderFile(wFile, NULL);
pivw->put_Owner((OAHWND)ghApp);
pivw->put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
GetClientRect(ghApp, &grc);
pivw->SetWindowPosition(grc.left, grc.top, grc.right, grc.bottom);
hr = pimc->Run();
The InWindow sample uses the same GetClipFileName function to get the movie to be played and the same the WinMain function to create the window as the Playfile sample.
The InWindow WndMainProc callback function is similar to the Playfile WndMainProc used to handle the filter graph messages and release the interfaces when necessary, with one important difference. The WndMainProc function in InWindow calls the IVideoWindow::put_Owner method with a NULL value for its parameter. You must do this before releasing the IGraphBuilder interface and before the video window is destroyed. Otherwise, messages will continue to be sent to the video playback window but it will have no parent to forward the messages to, so errors will likely occur.
pivw->put_Owner(NULL);
Top of Page
© 2000 Microsoft and/or its suppliers. All rights reserved. Terms of Use.