Here is the solution to play the video within the application using windowless mode in MFC.
#pragma comment(lib,"Strmiids.lib")
#pragma once
#include <dshow.h>
class CPlaybackDlg : public CDialog
{
public:
CPlaybackDlg(CWnd* pParent = NULL);
enum { IDD = IDD_PLAYBACK_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedPlayBtn();
IGraphBuilder *pGraph;
IMediaControl *pControl;
IMediaEventEx *pEvent;
IVMRWindowlessControl *pWc;
RECT rcSrc, rcDest;
HWND hwnd;
HRESULT InitWindowlessVMR(
HWND hwndApp, IGraphBuilder* pGraph, IVMRWindowlessControl** ppWc ) ;
};
</dshow.h>
#include "stdafx.h"
#include "Playback.h"
#include "PlaybackDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
CPlaybackDlg::CPlaybackDlg(CWnd* pParent )
: CDialog(CPlaybackDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPlaybackDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CPlaybackDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_PLAY_BTN, &CPlaybackDlg::OnBnClickedPlayBtn)
END_MESSAGE_MAP()
BOOL CPlaybackDlg::OnInitDialog()
{
CDialog::OnInitDialog();
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE);
return TRUE; }
void CPlaybackDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CPlaybackDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); RECT rcClient;
GetClientRect(&rcClient);
if (pWc != NULL)
{
HRGN rgnClient = CreateRectRgnIndirect(&rcClient);
HRGN rgnVideo = CreateRectRgnIndirect(&rcDest);
CombineRgn(rgnClient, rgnClient, rgnVideo, RGN_DIFF);
HBRUSH hbr = GetSysColorBrush(COLOR_BTNFACE);
FillRgn(dc.m_hDC, rgnClient, hbr);
DeleteObject(hbr);
DeleteObject(rgnClient);
DeleteObject(rgnVideo);
HRESULT hr = pWc->RepaintVideo(hwnd, dc.m_hDC);
}
else {
FillRect(dc.m_hDC, &rcClient, (HBRUSH)(COLOR_BTNFACE + 1));
}
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<wparam>(dc.GetSafeHdc()), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CPlaybackDlg::OnQueryDragIcon()
{
return static_cast<hcursor>(m_hIcon);
}
void CPlaybackDlg::OnBnClickedPlayBtn()
{
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
MessageBox(L"ERROR - Could not initialize COM library");
return;
}
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
MessageBox(L"ERROR - Could not create the Filter Graph Manager.");
return;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
hr = InitWindowlessVMR(AfxGetMainWnd()->m_hWnd, pGraph, &pWc);
hr=pGraph->RenderFile(L"C:\\Documents and Settings\\BTC\\Desktop\\hai.mp4", NULL);
long lWidth, lHeight;
hr = pWc->GetNativeVideoSize(&lWidth, &lHeight, NULL, NULL);
if (SUCCEEDED(hr))
{
RECT rcSrc, rcDest;
SetRect(&rcSrc, 0, 0, lWidth, lHeight);
GetClientRect(&rcDest);
SetRect(&rcDest, 0, 0, rcDest.right, rcDest.bottom-40);
hr = pWc->SetVideoPosition(&rcSrc, &rcDest);
}
if (SUCCEEDED(hr))
{
hr = pControl->Run();
if (SUCCEEDED(hr))
{
long evCode;
pEvent->WaitForCompletion(11000, &evCode);
}
}
pWc->Release();
pControl->Release();
pGraph->Release();
pEvent->Release();
CoUninitialize();
}
HRESULT CPlaybackDlg::InitWindowlessVMR(
HWND hwndApp, IGraphBuilder* pGraph, IVMRWindowlessControl** ppWc )
{
if (!pGraph || !ppWc)
{
return E_POINTER;
}
IBaseFilter* pVmr = NULL;
IVMRWindowlessControl* pWc = NULL;
HRESULT hr = CoCreateInstance(CLSID_VideoMixingRenderer, NULL,
CLSCTX_INPROC, IID_IBaseFilter, (void**)&pVmr);
if (FAILED(hr))
{
return hr;
}
hr = pGraph->AddFilter(pVmr, L"Video Mixing Renderer");
if (FAILED(hr))
{
pVmr->Release();
return hr;
}
IVMRFilterConfig* pConfig;
hr = pVmr->QueryInterface(IID_IVMRFilterConfig, (void**)&pConfig);
if (SUCCEEDED(hr))
{
hr = pConfig->SetRenderingMode(VMRMode_Windowless);
pConfig->Release();
}
if (SUCCEEDED(hr))
{
hr = pVmr->QueryInterface(IID_IVMRWindowlessControl, (void**)&pWc);
if( SUCCEEDED(hr))
{
hr = pWc->SetVideoClippingWindow(hwndApp);
if (SUCCEEDED(hr))
{
*ppWc = pWc; }
else
{
pWc->Release();
}
}
}
pVmr->Release();
return hr;
}
</hcursor></wparam>