www.pudn.com > ViewVideo1.rar > VideoWnd.cpp


// VideoWnd.cpp : 实现文件 
// 
 
#include "stdafx.h" 
#include "ViewVideo.h" 
#include "VideoWnd.h" 
 
 
// CVideoWnd 对话框 
COLORREF gBackgroundColor = RGB(10, 10, 10); 
 
//IMPLEMENT_DYNAMIC(CVideoWnd, CDialog) 
 
CVideoWnd::CVideoWnd(CWnd* pParent /*=NULL*/) 
	: CDialog(CVideoWnd::IDD, pParent) 
{ 
	m_hChannel=NULL; 
	m_strError.Empty(); 
	m_bPreviewStarted=FALSE; 
	m_iChannel=-1; 
	m_brush.CreateSolidBrush(gBackgroundColor); 
	m_bLBDown=FALSE; 
	m_lStandard = StandardNone; 
	m_bNotify = TRUE; 
	pWnd = pParent; 
} 
 
CVideoWnd::CVideoWnd(HANDLE handle, CWnd* pParent) 
{ 
	m_strError.Empty(); 
	m_bPreviewStarted=FALSE; 
	m_iChannel=-1; 
	m_brush.CreateSolidBrush(gBackgroundColor); 
	m_bLBDown=FALSE; 
	m_lStandard = StandardNone; 
	m_bNotify = TRUE; 
	m_hChannel = handle; 
} 
 
CVideoWnd::~CVideoWnd() 
{ 
	m_bNotify=FALSE; 
	this->CloseWindow(); 
} 
 
void CVideoWnd::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
} 
 
 
BEGIN_MESSAGE_MAP(CVideoWnd, CDialog) 
	ON_WM_CREATE() 
	ON_BN_CLICKED(IDOK, &CVideoWnd::OnBnClickedOk) 
	ON_BN_CLICKED(IDCANCEL, &CVideoWnd::OnBnClickedCancel) 
	ON_WM_CLOSE() 
//	ON_MESSAGE(WM_NOTIFYFLAG_Y, OnNotifyFlagYes) 
//	ON_MESSAGE(WM_NOTIFYFLAG_N, OnNotifyFlagNo) 
	ON_WM_LBUTTONDBLCLK() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_LBUTTONUP() 
	ON_WM_MOUSEMOVE() 
	ON_WM_CTLCOLOR() 
	ON_WM_SIZE() 
	ON_WM_MOVE() 
END_MESSAGE_MAP() 
 
 
// CVideoWnd 消息处理程序 
 
int CVideoWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{ 
	//lpCreateStruct->cx=0; 
	//lpCreateStruct->cy=0; 
 
	if (CDialog::OnCreate(lpCreateStruct) == -1) 
		return -1; 
 
	// TODO:  在此添加您专用的创建代码 
	if (m_hChannel!=NULL) 
	{ 
		//m_hChannel = ChannelOpen(m_iChannel); 
		//GetVideoPara(m_hChannel, &m_lStandard, &m_iBrightness, &m_iContrast,&m_iSaturation,&m_iHue); 
		//CString tmp; 
		//tmp.Format(_T("%X"), m_lStandard); 
		//AfxMessageBox(tmp); 
		//m_lStandard = StandardNone; 
		//SetVideoStandard(m_hChannel, StandardNTSC); 
		//SetDisplayStandard(m_iChannel, StandardNTSC); 
		//GetVideoPara(m_hChannel, &m_lStandard, &m_iBrightness, &m_iContrast,&m_iSaturation,&m_iHue); 
		//this->SetOwner(GetParent()); 
		//tmp.Format(_T("0x%07d"), m_lStandard); 
		//AfxMessageBox(tmp); 
	} 
	return 0; 
} 
 
BOOL CVideoWnd::Create(LPCTSTR lpszTemplateName, CWnd* pParentWnd) 
{ 
	// TODO: 在此添加专用代码和/或调用基类 
 
	return CDialog::Create(IDD, pParentWnd); 
} 
 
void CVideoWnd::OnBnClickedOk() 
{ 
	// TODO: 在此添加控件通知处理程序代码 
} 
 
void CVideoWnd::OnBnClickedCancel() 
{ 
	// TODO: 在此添加控件通知处理程序代码 
} 
CString CVideoWnd::GetLastError(int errnumb) 
{ 
	if (HWERR_SUCCESS) 
		return m_strError; 
	switch(errnumb) 
	{ 
	case ERR_WAIT_TIMEOUT: 
		m_strError = _T("SDK操作超时。"); 
		break; 
	case ERR_INVALID_HANDLE: 
		m_strError = _T("非法句柄,在调用SDK函数使用了错误的句柄。"); 
		break; 
	case ERR_INVALID_ARGUMENT: 
		m_strError = _T("参数错误,输入的参数可能超出有效范围。"); 
		break; 
	case ERR_DDRAW_CREATE_FAILED: 
		m_strError = _T("DDRAW返回的错误,参见MSDN。"); 
		break; 
	case ERR_DDRAW_CAPS_FAULT: 
		m_strError = _T("DDRAW返回的错误,参见MSDN。"); 
		break; 
	case ERR_SET_COOPERATIVELEVEL_FAILED: 
		m_strError = _T("DDRAW返回的错误,参见MSDN。"); 
		break; 
	case ERR_PRIMARY_SURFACE_CREATE_FAILED: 
		m_strError = _T("DDRAW返回的错误,参见MSDN。"); 
		break; 
	case ERR_GET_OVERLAY_ADDRESS_FAILED: 
		m_strError = _T("DDRAW返回的错误,参见MSDN。"); 
		break; 
	case ERR_OVERLAY_SURFACE_CREATE_FAILED: 
		m_strError = _T("DDRAW返回的错误,参见MSDN。"); 
		break; 
	case ERR_OVERLAY_UPDATE_FAILED: 
		m_strError = _T("DDRAW返回的错误,参见MSDN。"); 
		break; 
	case ERR_TMMAN_FAILURE: 
		m_strError = _T("SDK内部错误。"); 
		break; 
	case ERR_CHANNELMAGIC_MISMATCH: 
		m_strError = _T("通道数据毁坏。"); 
		break; 
	case ERR_QUEUE_OVERFLOW: 
		m_strError = _T("数据流缓存溢出。"); 
		break; 
	case ERR_STREAM_THREAD_FAILURE: 
		m_strError = _T("无法启动流处理线程。"); 
		break; 
	case ERR_THREAD_STOP_ERROR: 
		m_strError = _T("流处理线程停止错误。"); 
		break; 
	case ERR_NOT_SUPPORT: 
		m_strError = _T("该功能尚不支持。"); 
		break; 
	case ERR_OUTOF_MEMORY: 
		m_strError = _T("系统内存不足。"); 
		break; 
	case ERR_DSP_BUSY: 
		m_strError = _T("DSP正忙。"); 
		break; 
	case ERR_DATA_ERROR: 
		m_strError = _T("严重数据错误,必须重新停启压缩!。"); 
		break; 
	default: 
		m_strError = _T("未知错误。"); 
		break; 
	} 
	return m_strError; 
} 
BOOL CVideoWnd::StartPreview() 
{ 
//#ifdef NONDEVICE_DEBUG 
//	return 1; 
//#endif 
 
	BOOL bret; 
	bret = FALSE; 
	if (m_hChannel!=NULL) 
	{ 
		CRect rc; 
		GetClientRect(&rc); 
		int iret; 
 		iret = StartVideoPreview(m_hChannel, this->GetSafeHwnd(), &rc, FALSE, vdfRGB16, 25); 
		if (iret!=0) 
			AfxMessageBox(GetLastError(iret)); 
		else 
		//if (iret == 0) 
		{ 
			bret = TRUE; 
			m_bPreviewStarted = TRUE; 
		} 
		//} 
	} 
	TRACE("\n\n\n%0x\n\n\n", this->GetSafeHwnd()); 
	return bret; 
} 
BOOL CVideoWnd::StopPreview() 
{ 
//#ifdef NONDEVICE_DEBUG 
//	return 1; 
//#endif 
	BOOL bret; 
	bret = FALSE; 
	int iret; 
	if (m_bPreviewStarted) 
	{ 
		iret = StopVideoPreview(m_hChannel); 
		if (iret!=0) 
			AfxMessageBox(GetLastError(iret)); 
		else 
		//if (iret==0) 
		{ 
			m_bPreviewStarted = FALSE; 
			bret = TRUE; 
		} 
	} 
	else 
		bret = TRUE; 
	return bret; 
} 
HBRUSH CVideoWnd::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{ 
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); 
 
	// TODO:  在此更改 DC 的任何属性 
	pDC->SetTextColor (RGB (255, 0 , 0)); 
	pDC->SetBkColor(gBackgroundColor); 
	hbr = (HBRUSH) m_brush; 
	// TODO:  如果默认的不是所需画笔,则返回另一个画笔 
	return hbr; 
} 
 
void CVideoWnd::OnMove(int x, int y) 
{ 
	CDialog::OnMove(x, y); 
//	if (m_bNotify) 
//	::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_CBTHOOK_MOVESIZE, (WPARAM)this->GetSafeHwnd(), 0); 
	//if (!m_bLBDown && m_bPreviewStarted) 
	if (m_bPreviewStarted) 
	{ 
		StopPreview(); 
		StartPreview(); 
		Invalidate(); 
	} 
	// TODO: 在此处添加消息处理程序代码 
} 
 
void CVideoWnd::OnSize(UINT nType, int cx, int cy) 
{ 
	CDialog::OnSize(nType, cx, cy); 
	//if (!m_bLBDown && m_bPreviewStarted) 
	if (m_bPreviewStarted) 
	{ 
		StopPreview(); 
		StartPreview(); 
		Invalidate(); 
	} 
//	if (m_bNotify) 
//	::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_CBTHOOK_MOVESIZE, (WPARAM)this->GetSafeHwnd(), 0); 
	// TODO: 在此处添加消息处理程序代码 
} 
 
void CVideoWnd::OnLButtonDown(UINT nFlags, CPoint point) 
{ 
	// TODO: 在此添加消息处理程序代码和/或调用默认值 
 
	CDialog::OnLButtonDown(nFlags, point); 
	::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_VIDEOWND_LBDOWN, (WPARAM)GetSafeHwnd(), 0); 
//	m_bLBDown = TRUE; 
//	m_ipx = point.x; 
//	m_ipy = point.y; 
//	SetCapture(); 
} 
 
void CVideoWnd::OnLButtonUp(UINT nFlags, CPoint point) 
{ 
	// TODO: 在此添加消息处理程序代码和/或调用默认值 
 
	CDialog::OnLButtonUp(nFlags, point); 
	::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_VIDEOWND_LBUP, (WPARAM)GetSafeHwnd(), 0); 
//	if (m_bLBDown) 
//	{ 
//		m_bLBDown = FALSE; 
//		StopPreview(); 
//		StartPreview(); 
//		Invalidate(); 
//	} 
	ReleaseCapture(); 
} 
 
void CVideoWnd::OnMouseMove(UINT nFlags, CPoint point) 
{ 
	// TODO: 在此添加消息处理程序代码和/或调用默认值 
 
	CDialog::OnMouseMove(nFlags, point); 
	::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_VIDEOWND_MOUSEMOVE, (WPARAM)GetSafeHwnd(), 0); 
//	if (m_bLBDown) 
//	{ 
//		CRect rc; 
//		GetWindowRect(&rc); 
//		int w,h; 
//		w=rc.Width(); 
//		h=rc.Height(); 
//		rc.left+=point.x-m_ipx; 
//		rc.top+=point.y-m_ipy; 
//		rc.right=rc.left+w; 
//		rc.bottom=rc.top+h; 
//		MoveWindow(rc); 
//	} 
} 
 
void CVideoWnd::OnClose() 
{ 
	// TODO: 在此添加消息处理程序代码和/或调用默认值 
 
	StopPreview(); 
	CloseWindow(); 
//	if (m_bNotify) 
//	::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_CBTHOOK_DESTROYWND, (WPARAM)GetSafeHwnd(), 0); 
} 
 
void CVideoWnd::OnLButtonDblClk(UINT nFlags, CPoint point) 
{ 
	// TODO: 在此添加消息处理程序代码和/或调用默认值 
	CDialog::OnLButtonDblClk(nFlags, point); 
	if (this->IsZoomed()) 
		this->ShowWindow(SW_RESTORE); 
	else 
		this->ShowWindow(SW_MAXIMIZE); 
	::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_VIDEOWND_LBDBCLICK, (WPARAM)GetSafeHwnd(), 0); 
} 
/* 
LRESULT CVideoWnd::OnNotifyFlagYes(WPARAM wParam, LPARAM lParam) 
{ 
	m_bNotify=TRUE; 
	return 1; 
} 
LRESULT CVideoWnd::OnNotifyFlagNo(WPARAM wParam, LPARAM lParam) 
{ 
	m_bNotify=FALSE; 
	return 1; 
} 
*/