MainFrm.cpp
资源名称:VideoMove.rar [点击查看]
上传用户:czshopping
上传日期:2022-05-22
资源大小:5430k
文件大小:7k
源码类别:
视频捕捉/采集
开发平台:
Visual C++
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "VideoCapture.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_WM_CLOSE()
- ON_WM_ACTIVATE()
- ON_COMMAND(ID_APP_EXIT, OnAppExit)
- ON_COMMAND(ID_FORMAT, OnFormat)
- ON_COMMAND(ID_PREVIEW, OnPreview)
- ON_COMMAND(ID_SOURCE, OnSource)
- ON_COMMAND(ID_CAPTURE, OnCapture)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- LRESULT CALLBACK EXPORT ErrorCallbackProc(HWND hWnd,int nErrID,LPSTR lpErrorText)
- {
- if(nErrID==0)
- return true;
- AfxMessageBox(lpErrorText,MB_OK|NULL);
- return true;
- }
- LRESULT FAR PASCAL StatusCallbackProc(HWND hWnd,int nID,LPSTR lpStatusText)
- {
- static int CurrentID;
- if(nID==IDS_CAP_END)
- {
- if((CurrentID==IDS_CAP_STAT_VIDEOAUDIO)||(CurrentID==IDS_CAP_STAT_VIDEOONLY))
- return true;
- }
- CurrentID=nID;
- return (LRESULT)true;
- }
- LRESULT FAR PASCAL VideoCallbackProc(HWND hWnd,LPVIDEOHDR lpVHdr)
- {
- ((CMainFrame *)AfxGetMainWnd())->m_dibinfo.bitmapheader.biSizeImage =lpVHdr->dwBytesUsed ;
- memcpy(((CMainFrame *)AfxGetMainWnd())->m_dibinfo.buffer +((CMainFrame *)AfxGetMainWnd())->m_dibinfo.VideoFormatSize,lpVHdr->lpData,lpVHdr->dwBytesUsed);
- (((CMainFrame *)AfxGetMainWnd())->GetActiveView())->InvalidateRect(NULL,FALSE);
- return (LRESULT)true;
- }
- void CMainFrame::ChangeSize()
- {
- static UINT uiVideoX=0;
- static UINT uiVideoY=0;
- CAPSTATUS cs;
- RECT rc;
- m_wndSource.GetWindowRect(&rc);
- capGetStatus(m_WndCap,&cs,sizeof(cs));
- if(uiVideoX!=cs.uiImageWidth||uiVideoY!=cs.uiImageHeight)
- {
- m_wndSource.MoveWindow(rc.left,rc.top,cs.uiImageWidth+4,cs.uiImageHeight+28);
- uiVideoX=cs.uiImageWidth;
- uiVideoY=cs.uiImageHeight;
- }
- m_dibinfo.VideoFormatSize=capGetVideoFormatSize(m_WndCap);
- capGetVideoFormat(m_WndCap,&m_dibinfo.bitmapheader,m_dibinfo.VideoFormatSize);
- }
- CMainFrame::CMainFrame()
- {
- // TODO: add member initialization code here
- }
- CMainFrame::~CMainFrame()
- {
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
- | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
- // TODO: Delete these three lines if you don't want the toolbar to
- // be dockable
- m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
- EnableDocking(CBRS_ALIGN_ANY);
- DockControlBar(&m_wndToolBar);
- // 创建捕获窗口
- if(!m_wndSource.CreateEx(WS_EX_TOPMOST,NULL,"source",WS_CAPTION,CRect(100,100,150,180),NULL,0))
- return -1;
- m_wndSource.ShowWindow(SW_HIDE);
- m_WndCap=capCreateCaptureWindow((LPSTR)"Video Capture Window",WS_CHILD|WS_VISIBLE,0,0,300,240,m_wndSource.m_hWnd ,0);
- capSetCallbackOnError(m_WndCap,(FARPROC)ErrorCallbackProc);
- capSetCallbackOnStatus(m_WndCap,(FARPROC)StatusCallbackProc);
- capSetCallbackOnVideoStream(m_WndCap,(FARPROC)VideoCallbackProc);
- capDriverConnect(m_WndCap,0);
- capDriverGetCaps(m_WndCap,&m_caps,sizeof(CAPDRIVERCAPS));
- ChangeSize();
- return 0;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- if( !CFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
- void CMainFrame::OnClose()
- {
- // TODO: Add your message handler code here and/or call default
- OnAppExit();
- CFrameWnd::OnClose();
- }
- void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
- {
- CFrameWnd::OnActivate(nState, pWndOther, bMinimized);
- // TODO: Add your message handler code here
- }
- void CMainFrame::OnAppExit()
- {
- // TODO: Add your command handler code here
- capCaptureAbort(m_WndCap);
- //callback 是宏,如果出了问题就反馈
- capSetCallbackOnError(m_WndCap,NULL);
- capSetCallbackOnStatus(m_WndCap,NULL);
- capSetCallbackOnVideoStream(m_WndCap,NULL);
- capCaptureAbort(m_WndCap);
- capDriverDisconnect(m_WndCap);
- }
- void CMainFrame::OnFormat()
- {
- // TODO: Add your command handler code here
- if(m_caps.fHasDlgVideoFormat)
- {
- capDlgVideoFormat(m_WndCap);
- ChangeSize();
- }
- }
- void CMainFrame::OnPreview()
- {
- // TODO: Add your command handler code here
- static bool bPreview=FALSE;
- bPreview=!bPreview;
- capPreviewRate(m_WndCap,1000/10);
- capPreview(m_WndCap,bPreview);
- }
- void CMainFrame::OnSource()
- {
- // TODO: Add your command handler code here
- static BOOL bShow=false;
- bShow=!bShow;
- m_wndSource.ShowWindow(bShow?SW_SHOW:SW_HIDE);
- }
- void CMainFrame::OnCapture()
- {
- // TODO: Add your command handler code here
- static BOOL bCapture=false;
- CAPTUREPARMS CapParms;
- bCapture=!bCapture;
- if(!bCapture)
- {
- capCaptureGetSetup(m_WndCap,&CapParms,sizeof(CAPTUREPARMS));
- CapParms.dwIndexSize=324000;
- CapParms.fMakeUserHitOKToCapture=!CapParms.fMCIControl;
- CapParms.wPercentDropForError=100;
- CapParms.wNumVideoRequested=5;
- CapParms.wChunkGranularity=0;
- CapParms.fYield=true;
- CapParms.fCaptureAudio=false;
- CapParms.vKeyAbort=0;
- CapParms.fAbortLeftMouse=CapParms.fAbortRightMouse=false;
- CapParms.dwRequestMicroSecPerFrame=1000000/10;
- capSetCallbackOnYield(m_WndCap,NULL);
- capCaptureSetSetup(m_WndCap,&CapParms,sizeof(CAPTUREPARMS));
- capCaptureSequenceNoFile(m_WndCap);
- }
- else
- capCaptureAbort(m_WndCap);
- }