PCBMatchDlg.cpp
资源名称:PCBMatch.rar [点击查看]
上传用户:bxq2008bxq
上传日期:2022-07-18
资源大小:6138k
文件大小:5k
源码类别:
DirextX编程
开发平台:
Visual C++
- // PCBMatchDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "PCBMatch.h"
- #include "PCBMatchDlg.h"
- #include "SelectDevice.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // 对话框数据
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- // 实现
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- END_MESSAGE_MAP()
- // CPCBMatchDlg 对话框
- CPCBMatchDlg::CPCBMatchDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CPCBMatchDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CPCBMatchDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CPCBMatchDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- // CPCBMatchDlg 消息处理程序
- BOOL CPCBMatchDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 将“关于...”菜单项添加到系统菜单中。
- // IDM_ABOUTBOX 必须在系统命令范围内。
- 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);
- }
- }
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- // TODO: 在此添加额外的初始化代码
- InitVideo();
- return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
- }
- void CPCBMatchDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CPCBMatchDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // 使图标在工作矩形中居中
- 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;
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标显示。
- //
- HCURSOR CPCBMatchDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CPCBMatchDlg::InitVideo(void)
- {
- HRESULT hr = m_EnumDevice.EnumDevice();
- int index = 0;
- if(hr != S_OK)
- {
- this->MessageBox(_T("没有找到视频采集设备!"));
- exit(1);
- }
- if(m_EnumDevice.outlist.GetCount() > 1)
- {
- CSelectDevice SelectDeviceDlg;
- SelectDeviceDlg.outlist.Append(m_EnumDevice.outlist);
- if(IDOK == SelectDeviceDlg.DoModal())
- {
- index = SelectDeviceDlg.DeviceIndex;
- }
- else
- {
- exit(1);
- }
- }
- hr = m_BuildGraph.InitCaptureVideo(m_EnumDevice.GetCaptureDevice(index));
- if(hr != S_OK)
- {
- this->MessageBox(_T("视频采集设备初始化失败!"));
- exit(1);
- }
- else
- {
- AfxBeginThread(ThreadVideo,this, THREAD_PRIORITY_NORMAL);
- }
- }
- UINT ThreadVideo(LPVOID param)
- {
- CPCBMatchDlg *pDlg = (CPCBMatchDlg *)param;
- pDlg->CaptureVideo(IDC_STATIC_VFW);
- return 0;
- }
- HRESULT CPCBMatchDlg::CaptureVideo(int nID)
- {
- AM_MEDIA_TYPE mt;
- HRESULT hr = m_BuildGraph.pGrabber->GetConnectedMediaType(&mt);
- if (FAILED(hr))
- {
- // Return error code.
- return E_FAIL;
- }
- // Examine the format block.
- VIDEOINFOHEADER *pVih;
- if ((mt.formattype == FORMAT_VideoInfo) &&
- (mt.cbFormat >= sizeof(VIDEOINFOHEADER)) &&
- (mt.pbFormat != NULL) )
- {
- pVih = (VIDEOINFOHEADER*)mt.pbFormat;
- }
- else
- {
- // Wrong format. Free the format block and return an error.
- FreeMediaType(mt);
- return VFW_E_INVALIDMEDIATYPE;
- }
- byte *pBuffer = new byte[m_BuildGraph.cbBuffer];
- if (!pBuffer)
- {
- // Out of memory. Return an error code.
- return E_FAIL;
- }
- HDC hdc = this->GetDlgItem(nID)->GetDC()->GetSafeHdc();
- this->GetDlgItem(nID)->MoveWindow(10,10,pVih->bmiHeader.biWidth,pVih->bmiHeader.biHeight);
- while(1)
- {
- hr = m_BuildGraph.pGrabber->GetCurrentBuffer(&m_BuildGraph.cbBuffer, (long*)pBuffer);
- if(hr == S_OK)
- {
- SetDIBitsToDevice(
- hdc, 0, 0,
- pVih->bmiHeader.biWidth,
- pVih->bmiHeader.biHeight,
- 0, 0,
- 0,
- pVih->bmiHeader.biHeight,
- pBuffer,
- (BITMAPINFO*)&pVih->bmiHeader,
- DIB_RGB_COLORS
- );
- }
- Sleep(70);
- }
- // Free the format block when you are done:
- FreeMediaType(mt);
- }