www.pudn.com > MpegDShow.rar > MpegImageDlg.cpp


// MpegImageDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "MpegImage.h" 
#include "MpegImageDlg.h" 
#include "PathDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#define SLIDER_TIMER   100 
#define SAVEIMAGE_TIMER 101 
///////////////////////////////////////////////////////////////////////////// 
// 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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMpegImageDlg dialog 
 
CMpegImageDlg::CMpegImageDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CMpegImageDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CMpegImageDlg) 
		// 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_FilterGraph = NULL; 
	m_SliderTimer = 0; 
	m_SaveImageTimer = 0; 
	m_strFilePath = ""; 
} 
 
CMpegImageDlg::~CMpegImageDlg() 
{ 
	if(m_FilterGraph) 
	{ 
		delete m_FilterGraph; 
		m_FilterGraph = NULL; 
	} 
} 
 
void CMpegImageDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CMpegImageDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	DDX_Control(pDX, IDC_SLIDER_GRAPH, m_SliderGraph); 
	DDX_Control(pDX, IDC_VIDEO_WINDOW, m_VideoWindow); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CMpegImageDlg, CDialog) 
	//{{AFX_MSG_MAP(CMpegImageDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(ID_OPEN_BUTTON, OnOpenButton) 
	ON_BN_CLICKED(ID_PLAY_BUTTON, OnPlayButton) 
	ON_WM_TIMER() 
	ON_BN_CLICKED(ID_IMAGE_BUTTON, OnImageButton) 
	ON_BN_CLICKED(IDC_STOP, OnStop) 
	ON_NOTIFY(NM_OUTOFMEMORY, IDC_SLIDER_GRAPH, OnOutofmemorySliderGraph) 
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_GRAPH, OnCustomdrawSliderGraph) 
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_GRAPH, OnReleasedcaptureSliderGraph) 
	ON_BN_CLICKED(IDC_PAUSE, OnPause) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMpegImageDlg message handlers 
 
BOOL CMpegImageDlg::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 
	 
	// TODO: Add extra initialization here 
	m_VideoWindow.ModifyStyle(0, WS_CLIPCHILDREN);	 
 
	m_SliderGraph.SetRange(0, 1000); 
	m_SliderGraph.SetPos(0); 
 
	GetDlgItem(ID_PLAY_BUTTON)->EnableWindow(FALSE); 
	GetDlgItem(ID_IMAGE_BUTTON)->EnableWindow(FALSE); 
	GetDlgItem(IDC_STOP)->EnableWindow(FALSE); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CMpegImageDlg::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 CMpegImageDlg::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 CMpegImageDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CMpegImageDlg::OnOpenButton()  
{ 
	// TODO: Add your control notification handler code here 
	CString strFilter =  
	"MPEG File (*.mpg;*.mpeg)|*.mpg;*.mpeg|" 
	"AVI File (*.avi)|*.avi|" 
	"Mp3 File (*.mp3)|*.mp3|" 
	"Wave File (*.wav)|*.wav|" 
	"All Files (*.*)|*.*|"; 
	CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,  
		strFilter, this); 
	if (IDOK == dlgOpen.DoModal())  
	{ 
		CString strSourceFile = dlgOpen.GetPathName(); 
		// Rebuild the file playback filter graph 
		CMpegImageDlg::CreateGraph(strSourceFile); 
		GetDlgItem(ID_OPEN_BUTTON)->EnableWindow(FALSE); 
		GetDlgItem(ID_PLAY_BUTTON)->EnableWindow(TRUE); 
	}	 
} 
 
 
void CMpegImageDlg::CreateGraph(const CString& strSourceFile) 
{ 
	CMpegImageDlg::DestroyGraph(); 
	 
	m_FilterGraph = new CDXGraph(); 
	if (m_FilterGraph->Create()) 
	{ 
		// Render the source clip 
		m_FilterGraph->RenderFile(strSourceFile); 
		// Set video window and notification window 
		m_FilterGraph->SetDisplayWindow(m_VideoWindow.GetSafeHwnd()); 
		m_FilterGraph->SetNotifyWindow(this->GetSafeHwnd()); 
		// Show the first frame 
		m_FilterGraph->Pause(); 
	} 
} 
 
 
void CMpegImageDlg::DestroyGraph(void) 
{ 
	if(m_FilterGraph) 
	{ 
		// Stop the filter graph first 
		m_FilterGraph->Stop(); 
		m_FilterGraph->SetNotifyWindow(NULL); 
		 
		delete m_FilterGraph; 
		m_FilterGraph = NULL; 
	} 
} 
 
void CMpegImageDlg::OnPlayButton()  
{ 
	// TODO: Add your control notification handler code here 
	if (m_FilterGraph) 
	{ 
		GetDlgItem(ID_PLAY_BUTTON)->EnableWindow(FALSE); 
		GetDlgItem(ID_IMAGE_BUTTON)->EnableWindow(TRUE); 
		GetDlgItem(IDC_STOP)->EnableWindow(TRUE); 
		m_FilterGraph->Run(); 
		// Start a timer 
		if (m_SliderTimer == 0) 
		{ 
			m_SliderTimer = SetTimer(SLIDER_TIMER, 100, NULL); 
		} 
	}	 
} 
 
void CMpegImageDlg::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if (nIDEvent == m_SliderTimer && m_FilterGraph) 
	{ 
		double pos = 0, duration = 1.; 
		m_FilterGraph->GetCurrentPosition(&pos); 
		m_FilterGraph->GetDuration(&duration); 
		// Get the new position, and update the slider 
		int newPos = int(pos * 1000 / duration); 
		if (m_SliderGraph.GetPos() != newPos) 
		{ 
			m_SliderGraph.SetPos(newPos); 
		} 
	} 
	else if(nIDEvent == m_SaveImageTimer && m_FilterGraph) 
	{ 
		CMpegImageDlg::SaveImage(m_strFilePath); 
	} 
	 
	CDialog::OnTimer(nIDEvent); 
} 
 
 
BOOL CMpegImageDlg::DestroyWindow()  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	if (m_SliderTimer) 
	{ 
		KillTimer(m_SliderTimer); 
		m_SliderTimer = 0; 
	} 
 
	if (m_SaveImageTimer) 
	{ 
		KillTimer(m_SaveImageTimer); 
		m_SaveImageTimer = 0; 
	} 
 
	return CDialog::DestroyWindow(); 
} 
 
 
void CMpegImageDlg::OnImageButton()  
{ 
	// TODO: Add your control notification handler code here 
	m_FilterGraph->Pause(); 
	// Start a timer 
	if (m_SliderTimer == 0) 
	{ 
		m_SliderTimer = SetTimer(SLIDER_TIMER, 100, NULL); 
	} 
 
	CString filepath = ""; 
	int iTimer = 1; 
	CPathDlg pathdlg; 
	if(pathdlg.DoModal() == IDOK) 
	{ 
		filepath = pathdlg.m_strFilePath; 
		iTimer = pathdlg.m_iTimer; 
	} 
	else  
	{ 
		m_FilterGraph->Run(); 
		// Start a timer 
		if (m_SliderTimer == 0) 
		{ 
			m_SliderTimer = SetTimer(SLIDER_TIMER, 100, NULL); 
		} 
		return; 
	} 
	 
	if(filepath == "") 
	{ 
		AfxMessageBox("ÇëÊäÈë±£´æÂ·¾¶£¡"); 
		m_FilterGraph->Run(); 
		// Start a timer 
		if (m_SliderTimer == 0) 
		{ 
			m_SliderTimer = SetTimer(SLIDER_TIMER, 100, NULL); 
		} 
		return; 
	} 
	 
	GetDlgItem(ID_IMAGE_BUTTON)->EnableWindow(false); 
 
	CMpegImageDlg::CreateDir(filepath); 
	m_strFilePath = filepath; 
	 
	m_FilterGraph->Run(); 
	// Start a timer 
	if (m_SliderTimer == 0) 
	{ 
		m_SliderTimer = SetTimer(SLIDER_TIMER, 100, NULL); 
	} 
 
	m_MyClientSocket.Init(); 
	m_SaveImageTimer = SetTimer(SAVEIMAGE_TIMER, iTimer*1000, NULL); 
} 
 
 
void CMpegImageDlg::CreateDir(CString strFilePath) 
{ 
	CString strTempPath = strFilePath; 
	if(strTempPath.IsEmpty()) 
		return; 
	 
	if(strTempPath.GetAt(strTempPath.GetLength() - 1) != '\\') 
	{ 
		strTempPath += '\\'; 
	} 
	int nCountTemp = 0; 
	CString strPath = ""; 
	while (1)  
	{ 
		int nCount = strTempPath.Find("\\");		 
		if (nCount != -1)  
		{ 
			strPath += strTempPath.Mid(nCountTemp, nCount + 1 - nCountTemp); 
			::CreateDirectory(strPath, NULL); 
			nCountTemp = nCount + 1; 
			strTempPath.SetAt(nCount, '|'); 
		}  
		else  
		{ 
			break; 
		} 
	} 
} 
 
 
void CMpegImageDlg::SaveImage(CString filepath) 
{ 
	if (m_FilterGraph) 
	{		 
			// Firstly grab a bitmap to a temp file 
		filepath += CMpegImageDlg::GetFileName(); 
		char* szTemp = filepath.GetBuffer(255);; 
		m_FilterGraph->SnapshotBitmap(szTemp);	 
		m_MyClientSocket.SendFile(filepath); 
		filepath.ReleaseBuffer(); 
	} 
} 
 
 
CString CMpegImageDlg::GetFileName() 
{ 
	CTime tTime = CTime::GetCurrentTime(); 
	int iMinuter = tTime.GetMinute(); 
	int iSecond = tTime.GetSecond(); 
	CString strFileName = ""; 
	CString strTemp = ""; 
	strTemp.Format("%d", iMinuter); 
	strFileName = strTemp; 
	strTemp = ""; 
	strTemp.Format("%d", iSecond); 
	strFileName += "_" + strTemp + ".bmp"; 
	return strFileName; 
} 
 
 
void CMpegImageDlg::OnStop()  
{ 
	this->DestroyGraph(); 
	m_SliderGraph.SetPos(0); 
 
	GetDlgItem(ID_PLAY_BUTTON)->EnableWindow(FALSE); 
	GetDlgItem(ID_IMAGE_BUTTON)->EnableWindow(FALSE); 
	GetDlgItem(IDC_STOP)->EnableWindow(FALSE); 
	GetDlgItem(ID_OPEN_BUTTON)->EnableWindow(TRUE); 
} 
 
void CMpegImageDlg::OnOutofmemorySliderGraph(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	TRACE("Slider 1\n"); 
	*pResult = 0; 
} 
 
void CMpegImageDlg::OnCustomdrawSliderGraph(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	// TODO: Add your control notification handler code here 
	TRACE("Slider 2\n"); 
	/*if(m_FilterGraph) 
	{ 
		int newPos = m_SliderGraph.GetPos(); 
		double pos = static_cast(newPos); 
		double duration = static_cast(pos * 1000 / pos); 
 
		m_FilterGraph->SetCurrentPosition(pos); 
		// Get the new position, and update the slider 
	} 
	*/ 
	*pResult = 0; 
} 
 
void CMpegImageDlg::OnReleasedcaptureSliderGraph(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	// TODO: Add your control notification handler code here 
	TRACE("Slider 3\n"); 
	*pResult = 0; 
} 
 
void CMpegImageDlg::OnPause()  
{ 
	if(m_FilterGraph) 
	{ 
		static int i = 1; 
		if(i & 1) 
		{ 
			m_FilterGraph->Pause(); 
			GetDlgItem(IDC_PAUSE)->SetWindowText("Resume"); 
			i++; 
		} 
		else 
		{ 
			m_FilterGraph->Run(); 
			GetDlgItem(IDC_PAUSE)->SetWindowText("Pause"); 
			i++; 
		} 
	} 
}