www.pudn.com > MyVideoPlayer.rar > MyVideoPlayer1Dlg.cpp


// MyVideoPlayer1Dlg.cpp : implementation file 
// 
///////////////////////////////////////////////////////// 
// Writer:  Dennis  
// Jeffeson177@sohu.com 
///////////////////////////////////////////////////////// 
#include "stdafx.h" 
#include "MyVideoPlayer1.h" 
#include "MyVideoPlayer1Dlg.h" 
#include "stdlib.h" 
#include "string" 
using namespace std; 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyVideoPlayer1Dlg dialog 
 
 
CMyVideoPlayer1Dlg::CMyVideoPlayer1Dlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CMyVideoPlayer1Dlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CMyVideoPlayer1Dlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
 
	m_pFilterGraph = NULL; 
	m_sourceFile = ""; 
	m_sliderTimer = 0; 
} 
 
CMyVideoPlayer1Dlg::~CMyVideoPlayer1Dlg() 
{ 
	DestroyGraph(); 
} 
 
void CMyVideoPlayer1Dlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CMyVideoPlayer1Dlg) 
	DDX_Control(pDX, IDC_SLIDER1, m_sliderGraph); 
	DDX_Control(pDX, IDC_SHOW_WINDOW, m_videoWindow); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CMyVideoPlayer1Dlg, CDialog) 
	//{{AFX_MSG_MAP(CMyVideoPlayer1Dlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_ERASEBKGND() 
	ON_BN_CLICKED(IDC_BUTTONOPEN, OnButtonopen) 
	ON_BN_CLICKED(IDC_BUTTONPAUSE, OnButtonpause) 
	ON_BN_CLICKED(IDC_BUTTONFULLSCREEN, OnButtonfullscreen) 
	ON_BN_CLICKED(IDC_BUTTONPLAY, OnButtonplay) 
	ON_BN_CLICKED(IDC_BUTTONSTOP, OnButtonstop) 
	ON_BN_CLICKED(IDC_BUTTONTEST, OnButtontest) 
	ON_BN_CLICKED(IDC_BUTTONGRAB, OnButtongrab) 
	ON_WM_TIMER() 
	ON_WM_HSCROLL() 
	//}}AFX_MSG_MAP 
	ON_MESSAGE(WM_GRAPHNOTIFY, OnGraphNotify) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyVideoPlayer1Dlg message handlers 
 
BOOL CMyVideoPlayer1Dlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	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); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	m_videoWindow.ModifyStyle(0, WS_CLIPCHILDREN); 
	m_sliderGraph.SetRange(0, 1000); 
	m_sliderGraph.SetPos(0); 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CMyVideoPlayer1Dlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CMyVideoPlayer1Dlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		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; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CMyVideoPlayer1Dlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
LRESULT CMyVideoPlayer1Dlg::OnGraphNotify(WPARAM inWParam, LPARAM inLParam) 
{ 
	IMediaEventEx *pEvent = NULL; 
	if ((m_pFilterGraph!=NULL) && (pEvent = m_pFilterGraph->GetEventHandle())) 
	{ 
		LONG eventCode = 0; 
		LONG eventParam1  = 0; 
		LONG eventParam2 = 0; 
		while (SUCCEEDED(pEvent->GetEvent(&eventCode, &eventParam1, &eventParam2, 0))) 
		{ 
			pEvent->FreeEventParams(eventCode, eventParam1, eventParam2); 
			switch (eventCode) 
			{ 
			case EC_COMPLETE: 
				OnButtonpause(); 
				m_pFilterGraph->SetCurrentPosition(0); 
				break; 
			case EC_USERABORT: 
			case EC_ERRORABORT: 
				OnButtonstop(); 
				break; 
			default: 
				break; 
			} 
		} 
	} 
	return 0; 
} 
 
BOOL CMyVideoPlayer1Dlg::OnEraseBkgnd(CDC* pDC)  
{ 
	CRect rc; 
	m_videoWindow.GetWindowRect(&rc); 
	ScreenToClient(&rc); 
 
	pDC->ExcludeClipRect(&rc); 
	 
	return CDialog::OnEraseBkgnd(pDC); 
} 
 
void CMyVideoPlayer1Dlg::DestroyGraph() 
{ 
	if (m_pFilterGraph != NULL) 
	{ 
		m_pFilterGraph->Stop(); 
		m_pFilterGraph->SetNotifyWindow(NULL); 
 
		delete m_pFilterGraph; 
		m_pFilterGraph = NULL; 
	} 
} 
 
void CMyVideoPlayer1Dlg::CreateGraph() 
{ 
	DestroyGraph(); 
	m_pFilterGraph = new CDXGraph(); 
	if (m_pFilterGraph->Create()) 
	{ 
		m_pFilterGraph->RenderFile(m_sourceFile); 
		m_pFilterGraph->SetDisplayWindow(m_videoWindow.GetSafeHwnd()); 
		m_pFilterGraph->SetNotifyWindow(this->GetSafeHwnd()); 
 
		// show the first frame 
		m_pFilterGraph->Pause(); 
	} 
} 
 
void CMyVideoPlayer1Dlg::OnButtonopen()  
{ 
	CString strFilter = "AVI File (*.avi) | *.avi|"; 
	strFilter += "MPEG File (*.mpg; *.mpeg) | *.mpg; *.mpeg|"; 
	strFilter += "MP3 File (*.mp3) | *.mp3|"; 
	strFilter += "WMA File (*.wma) | *.wma|"; 
	strFilter += "All File (*.*) | *.*|"; 
 
	CFileDialog dlg(TRUE, NULL, NULL, OFN_PATHMUSTEXIST|OFN_HIDEREADONLY, strFilter, this); 
	if (dlg.DoModal() == IDOK) 
	{ 
		m_sourceFile = dlg.GetPathName(); 
		CreateGraph(); 
	} 
} 
 
void CMyVideoPlayer1Dlg::OnButtonpause()  
{ 
	if (m_pFilterGraph) 
	{ 
		m_pFilterGraph->Pause(); 
		if (m_sliderTimer == 0) 
		{ 
			m_sliderTimer = SetTimer(SLIDER_TIMER, 100, NULL); 
		} 
	}	 
} 
 
void CMyVideoPlayer1Dlg::OnButtonfullscreen()  
{ 
	if (m_pFilterGraph != NULL) 
	{ 
		m_pFilterGraph->SetFullScreen(TRUE); 
	} 
} 
 
void CMyVideoPlayer1Dlg::RestoreFromFullScreen() 
{ 
	if (m_pFilterGraph != NULL) 
	{ 
		if (m_pFilterGraph->GetFullScreen()) 
		{ 
			m_pFilterGraph->SetFullScreen(FALSE); 
		} 
	} 
} 
 
void CMyVideoPlayer1Dlg::OnButtonplay()  
{ 
	if (m_pFilterGraph) 
	{ 
		m_pFilterGraph->Run(); 
		if (m_sliderTimer == 0) 
		{ 
			m_sliderTimer = SetTimer(SLIDER_TIMER, 100, NULL); 
		} 
	} 
	 
} 
 
 
void CMyVideoPlayer1Dlg::OnButtonstop()  
{ 
	if (m_pFilterGraph != NULL) 
	{ 
		m_pFilterGraph->SetCurrentPosition(0); 
		m_pFilterGraph->Stop(); 
		if (m_sliderTimer != 0) 
		{ 
			KillTimer(m_sliderTimer); 
			m_sliderTimer = 0; 
		} 
	} 
} 
 
void CMyVideoPlayer1Dlg::OnButtontest()  
{ 
	ShowVRPropertyPage();	 
} 
 
void CMyVideoPlayer1Dlg::OnButtongrab()  
{ 
	static int c = 0; 
	CString str = "E:\\Grab Photo\\mysnapshot"; 
	char ch[8]; 
	itoa(c, ch, 10); 
	int len = str.GetLength(); 
	str.Insert(len, ch); 
	CString strTemp = ".bmp"; 
	str += strTemp; 
	c++; 
	char *p = str.GetBuffer(0); 
 
	if (m_pFilterGraph != NULL) 
	{ 
		// Firstly grab a bitmap to a temp file 
		if (m_pFilterGraph->SnapshotBitmap(p)) 
		{ 
			// User can browser for a new file here 
			CString   strFilter = "BMP File (*.bmp)|*.bmp|"; 
			CFileDialog dlgOpen(FALSE, ".bmp", NULL, OFN_HIDEREADONLY, strFilter, NULL); 
			if (IDOK == dlgOpen.DoModal())  
			{ 
				::CopyFile(p, dlgOpen.GetPathName(), FALSE); 
				::DeleteFile(p); 
			} 
		}	 
	} 
} 
 
void CMyVideoPlayer1Dlg::OnTimer(UINT nIDEvent)  
{ 
	if (nIDEvent == m_sliderTimer && m_pFilterGraph != NULL) 
	{ 
		double pos = 0; 
		double duration = 1.0; 
		m_pFilterGraph->GetCurrentPosition(&pos); 
		m_pFilterGraph->GetDuration(&duration); 
		int newPos = int(pos*1000/duration); 
		if (m_sliderGraph.GetPos() != newPos) 
		{ 
			m_sliderGraph.SetPos(newPos); 
		} 
	} 
	CDialog::OnTimer(nIDEvent); 
} 
 
void CMyVideoPlayer1Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
	if (pScrollBar->GetSafeHwnd() == m_sliderGraph.GetSafeHwnd()) 
	{ 
		if (m_pFilterGraph != NULL) 
		{ 
			double duration =1.0; 
			m_pFilterGraph->GetDuration(&duration); 
			double pos = duration * m_sliderGraph.GetPos()/1000.0; 
			m_pFilterGraph->SetCurrentPosition(pos); 
		} 
	} 
	else 
	{ 
		CDialog::OnHScroll(nSBCode, nPos, pScrollBar); 
	} 
} 
 
BOOL CMyVideoPlayer1Dlg::PreTranslateMessage(MSG* pMsg)  
{ 
	if (pMsg->message == WM_KEYDOWN) 
	{ 
		if (pMsg->wParam == VK_RETURN || VK_ESCAPE) 
		{ 
			RestoreFromFullScreen(); 
			return TRUE; 
		} 
	} 
	 
	return CDialog::PreTranslateMessage(pMsg); 
} 
 
void CMyVideoPlayer1Dlg::ShowVRPropertyPage() 
{ 
	IBaseFilter *pFilter = NULL; 
	if (FAILED(FindFilterByInterface(IID_IVideoWindow, &pFilter))) 
	{ 
		return; 
	} 
	pFilter->Release(); 
 
	ISpecifyPropertyPages *pProp = NULL; 
	HRESULT hr = pFilter->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pProp); 
	if (SUCCEEDED(hr))  
	{ 
		// Get the filter's name and IUnknown pointer. 
		FILTER_INFO FilterInfo; 
		hr = pFilter->QueryFilterInfo(&FilterInfo);  
		IUnknown *pFilterUnk; 
		pFilter->QueryInterface(IID_IUnknown, (void **)&pFilterUnk); 
 
		// Show the page.  
		CAUUID caGUID; 
		pProp->GetPages(&caGUID); 
		pProp->Release(); 
		OleCreatePropertyFrame( 
			this->GetSafeHwnd(),                   // Parent window 
			0, 0,                   // Reserved 
			FilterInfo.achName,     // Caption for the dialog box 
			1,                      // Number of objects (just the filter) 
			&pFilterUnk,            // Array of object pointers.  
			caGUID.cElems,          // Number of property pages 
			caGUID.pElems,          // Array of property page CLSIDs 
			0,                      // Locale identifier 
			0, NULL                 // Reserved 
		); 
 
		// Clean up. 
		pFilterUnk->Release(); 
		FilterInfo.pGraph->Release();  
		CoTaskMemFree(caGUID.pElems); 
	} 
} 
 
HRESULT CMyVideoPlayer1Dlg::FindFilterByInterface(REFIID riid, IBaseFilter **ppFilter) 
{ 
	*ppFilter = NULL; 
	if (m_pFilterGraph == NULL) 
	{ 
		return E_FAIL; 
	} 
 
    IEnumFilters* pEnum; 
    HRESULT hr = m_pFilterGraph->GetGraph()->EnumFilters(&pEnum); 
    if (FAILED(hr))  
	{ 
		return hr; 
    } 
 
    IBaseFilter* pFilter = NULL; 
    while (pEnum->Next(1, &pFilter, NULL) == S_OK)  
	{ 
		// Check for required interface 
		IUnknown* pUnk; 
		HRESULT hrQuery = pFilter->QueryInterface(riid, (void**)&pUnk); 
		if (SUCCEEDED(hrQuery))  
		{ 
			pUnk->Release(); 
			pEnum->Release(); 
			*ppFilter = pFilter; 
			return S_OK; 
		} 
		pFilter->Release(); 
    } 
    pEnum->Release(); 
 
    return E_FAIL; 
}