Using a custom window

The previous samples displayed a new window when playing MPEG. This page shows you how to display MPEG video within your custom window. Please note that error handling codes are omitted to keep the sample code simple.

Sample code

The following sample code shows how to use custom window.

To play a MPEG file within a custom window, you must create your own window. This sample uses MFC to create a custom window.


#include <afxwin.h>

#include <dshow.h>

class CMainWin : public CFrameWnd
{
public:
	CMainWin();
	~CMainWin();
	DECLARE_MESSAGE_MAP()
};

CMainWin::CMainWin()
{
 Create(NULL, "Sample");
}

CMainWin::~CMainWin()
{
}

class CApp : public CWinApp
{
public:
	BOOL InitInstance();
};

#define	FILENAME L"c:\\DXSDK\\Samples\\Media\\butterfly.mpg"

BOOL CApp::InitInstance()
{
 m_pMainWnd = new CMainWin;
 m_pMainWnd->ShowWindow(m_nCmdShow);
 m_pMainWnd->UpdateWindow();

 IGraphBuilder *pGraphBuilder;
 IMediaControl *pMediaControl;
 IMediaEvent *pMediaEvent;
 IVideoWindow *pVideoWindow;
 long eventCode;

 CoInitialize(NULL);

 CoCreateInstance(CLSID_FilterGraph,
	NULL,
	CLSCTX_INPROC,
	IID_IGraphBuilder,
	(LPVOID *)&pGraphBuilder);

 pGraphBuilder->QueryInterface(IID_IMediaControl,
	(LPVOID *)&pMediaControl);

 pGraphBuilder->QueryInterface(IID_IMediaEvent,
	(LPVOID *)&pMediaEvent);

 pMediaControl->RenderFile(FILENAME);

 pGraphBuilder->QueryInterface(IID_IVideoWindow,
	(LPVOID *)&pVideoWindow);
 pVideoWindow->put_Owner((OAHWND)m_pMainWnd->GetSafeHwnd());

 pVideoWindow->put_WindowStyle(WS_CHILD|WS_CLIPSIBLINGS);

 RECT rect;
 m_pMainWnd->GetClientRect(&rect);
 pVideoWindow->SetWindowPosition(0, 0,
	rect.right - rect.left, rect.bottom - rect.top);

 pVideoWindow->SetWindowForeground(OATRUE);
 pVideoWindow->put_Visible(OATRUE);

 pMediaControl->Run();

 pMediaEvent->WaitForCompletion(-1, &eventCode);

 pMediaEvent->Release();
 pVideoWindow->Release();
 pMediaControl->Release();
 pGraphBuilder->Release();
 CoUninitialize();

 return true;
}

BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)
END_MESSAGE_MAP()

CApp App;

Although this sample uses MFC, you can use other ways to create a window too.

To keep the sample simple, this sample does everything in InitInstance(). This is not a nice thing to do. This sample uses WaitForCompletion within InitInstance(), and InitInstance will not return until the playback ends. Please do not try to include everything in InitInstance when you write your own application.

  

Copyright (C) GeekPage.JP. All rights reserved.