⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mfcview.cpp

📁 利用opencv在mfc下实现视频的播放
💻 CPP
字号:
// mfcView.cpp : implementation of the CMfcView class
//

#include "stdafx.h"
#include "mfc.h"
#include "highgui.h"
#include "mfcDoc.h"
#include "mfcView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMfcView

IMPLEMENT_DYNCREATE(CMfcView, CView)

BEGIN_MESSAGE_MAP(CMfcView, CView)
	//{{AFX_MSG_MAP(CMfcView)
	ON_WM_SIZE()
	ON_WM_SHOWWINDOW()
	ON_WM_MOVE()
	ON_WM_TIMER()
	ON_WM_CREATE()
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMfcView construction/destruction

CMfcView::CMfcView()
{
	// TODO: add construction code here
	a=1;
	i=0;
}

CMfcView::~CMfcView()
{
}

BOOL CMfcView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs	

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMfcView drawing

void CMfcView::OnDraw(CDC* pDC)
{
	pDoc= GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here		
	if (a==1)
    {
	filePath1=pDoc->filePath;
	capture=cvCaptureFromFile(filePath1);
	}
	if (a==cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT))
	{
    //加这个是为了循环播放
    if (filePath1!=pDoc->filePath)
	    replay();
	}
	m_frame=cvQueryFrame(capture);
    HDC hDC= pDC->GetSafeHdc(); 
    CRect rect; 
    GetClientRect(&rect); 
    cimg.CopyOf(m_frame); 
	//CRect rect(0,0,cimg.Width(),cimg.Height());          
	cimg.DrawToHDC(hDC,&rect);	
}  
/////////////////////////////////////////////////////////////////////////////
// CMfcView printing

BOOL CMfcView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMfcView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMfcView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMfcView diagnostics

#ifdef _DEBUG
void CMfcView::AssertValid() const
{
	CView::AssertValid();
}

void CMfcView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMfcDoc* CMfcView::GetDocument() // non-debug version is inline
{

	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMfcDoc)));
	return (CMfcDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////



void CMfcView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
	Invalidate();
	UpdateWindow();
	
}

void CMfcView::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CView::OnShowWindow(bShow, nStatus);

	// TODO: Add your message handler code here
    Invalidate();
	UpdateWindow();	
}

void CMfcView::OnMove(int x, int y) 
{
	//CView::OnMove(x, y);
    Invalidate();
	UpdateWindow();
	// TODO: Add your message handler code here
	
}

void CMfcView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	//capture得到以后,帧数才开始偏移
	//&&a<cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT)
	if(capture&&a<cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT))
	{
    a++;
	cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,a);	
	}
	//InvalidateRect(&rect);
	Invalidate();
	UpdateWindow();
	//CView::OnTimer(nIDEvent);
}

int CMfcView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	// TODO: Add your specialized creation code here
	SetTimer(1,40,NULL);
	    return 0;
}

BOOL CMfcView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	if (capture)    
	return 1;
	else
	return CView::OnEraseBkgnd(pDC);	
}

void CMfcView::replay()
{	
	pDoc= GetDocument();
	ASSERT_VALID(pDoc);
	capture=cvCaptureFromFile(pDoc->filePath);
	filePath1=pDoc->filePath;
	a=1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -