win32用VFW预览和录像的代码

转载 2013年12月03日 15:37:05
#include <windows.h>
#include <vfw.h>
#include <commdlg.h>
#include "CapPicture.h"

LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM );
LRESULT WINAPI SelCapDrvProc( HWND, UINT, WPARAM, LPARAM );
int EnumCapDrv();
VOID APIENTRY HandlePopupMenu(HWND, POINT);
DWORD WINAPI videoThreadProc(LPVOID lParam);


HANDLE ghInstance;
HWND hwndMain;
HWND hwndVideo;
HWND hwndSelCapDrvDlg;
HWND hwndSelCapDrvDlg_LBox;
HWND hwndExit;
HWND hwndMin;
HWND hwndHelp;
HWND hwndRecord;
HANDLE hVideoThread;
HRGN hRegion1;
CAPDRIVERCAPS CapDrvCaps;
bool isRecordFileOpen = false;
char recordFile[260];
bool isPicFileOpen = false;
char pictureFile[260];
bool isRecording = false;
bool threadEnd = false;

int PASCAL WinMain( HINSTANCE hInstance,
    HINSTANCE hPrevInstance,

    LPSTR lpszCmdLine,
    int nCmdShow )
{
   WNDCLASS wc;
   MSG msg;

   if( !hPrevInstance )
   {
      wc.lpszClassName = "GenericAppClass";
      wc.lpfnWndProc = MainWndProc;
      wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
      wc.hInstance = hInstance;
      wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
      wc.hCursor = LoadCursor( NULL, IDC_ARROW );
      wc.hbrBackground = CreateSolidBrush (RGB(196, 200, 204));
      wc.lpszMenuName = "GenericAppMenu";

      wc.cbClsExtra = 0;
      wc.cbWndExtra = 0;

      RegisterClass( &wc );
   }

   ghInstance = hInstance;

   hwndMain = CreateWindow( "GenericAppClass",
      "Super Video",
      WS_POPUP,
      100,
      100,
      500,
      500,
      NULL,
      NULL,
      hInstance,
      NULL
   );

   ShowWindow( hwndMain, nCmdShow );
   SetWindowRgn(hwndMain,hRegion1,1);

   while( GetMessage( &msg, NULL, 0, 0 ) ) {
      TranslateMessage( &msg );

      DispatchMessage( &msg );
   }

   return msg.wParam;
}

LRESULT CALLBACK MainWndProc( HWND hwndMain, UINT msg, WPARAM wParam,
   LPARAM lParam )
{
   HDC hDC = GetDC(hwndMain);
   RECT rc;           
   POINT pt;  

   switch( msg ) {
      case WM_LBUTTONDBLCLK:
              SetFocus(hwndMain);
              break;
      case WM_RBUTTONDOWN:
            GetClientRect(hwndMain, (LPRECT) &rc);
            pt.x = LOWORD(lParam);
            pt.y = HIWORD(lParam);
            if (PtInRect((LPRECT) &rc, pt))
                HandlePopupMenu(hwndMain, pt);
            break;
      case WM_PAINT:
         FrameRgn(hDC,hRegion1,CreateSolidBrush(RGB(0,0,0)),2,2);
           BringWindowToTop(hwndSelCapDrvDlg);
         return( DefWindowProc( hwndMain, msg, wParam, lParam ));
      case WM_COMMAND:
           CAPSTATUS CapStatus;
           switch( wParam ) {
         
             case SOURCE:
                  if(CapDrvCaps.fHasDlgVideoSource)
                          capDlgVideoSource(hwndVideo);
                  break;
               case FORMAT:
                  if(CapDrvCaps.fHasDlgVideoFormat)
                  {        
                          capDlgVideoFormat(hwndMain);
                          capGetStatus(hwndVideo, &CapStatus, sizeof(CAPSTATUS));
                          SetWindowPos(hwndVideo, NULL, 0, 0, CapStatus.uiImageWidth,
                                      CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);
                  }
                  break;
               case DISPLAY:
                   if (CapDrvCaps.fHasDlgVideoDisplay)
                       capDlgVideoDisplay(hwndVideo);
                   break;
            case EXIT:
               SendMessage(hwndMain, WM_SYSCOMMAND, SC_CLOSE, 0);
               break;
            case MINIMIZE:
               SendMessage(hwndMain, WM_SYSCOMMAND, SC_MINIMIZE, 0);
               break;
            case HELP:
               SendMessage(hwndMain, WM_SYSCOMMAND, SC_CONTEXTHELP, 0);
               break;
            case RECORDVIDEO:
            if(HIWORD(wParam) == BN_CLICKED && (HWND) lParam == hwndRecord)
            {
               if (!isRecordFileOpen)
               {
                       OPENFILENAME ofn;
                       ZeroMemory(&ofn, sizeof(OPENFILENAME));
                    ofn.lStructSize = sizeof(OPENFILENAME);
                    ofn.hwndOwner = hwndMain;
                    ofn.lpstrFile = recordFile;
                    ofn.nMaxFile = sizeof(recordFile);
                    ofn.lpstrFilter = "Video\0*.avi";
                    ofn.nFilterIndex = 1;
                    ofn.lpstrFileTitle = NULL;
                    ofn.nMaxFileTitle = 0;
                    ofn.lpstrInitialDir = NULL;
                    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
                    if(GetSaveFileName(&ofn) == TRUE)
                    {
                        strcpy(recordFile, ofn.lpstrFile);
                        strcat(recordFile, ".avi");
                        isRecordFileOpen = true;
                        DWORD id;
                        SECURITY_ATTRIBUTES sa;
                        sa.nLength = sizeof(SECURITY_ATTRIBUTES);
                        sa.lpSecurityDescriptor = NULL;
                        sa.bInheritHandle = TRUE;
                        hVideoThread = CreateThread(&sa, (ULONG)0,
                                            videoThreadProc, (LPVOID)(ULONG)0, (ULONG)0, &id);        
                        if(hVideoThread == NULL)
                            MessageBox(NULL, "Creation of Record Thread failed!", "Thread", MB_OK | MB_ICONEXCLAMATION);
                        
                        break;
                    }
                }

                if (isRecordFileOpen)
                {
                    if(isRecording)
                    {
                        threadEnd = true;
                        capFileSaveAs(hwndVideo, recordFile);
                        SetWindowText(hwndRecord, "Record Video");
                        isRecording = false;
                        break;
                    }
                    
                    if(!isRecording )
                    {
                        int a = 0;
                        
                        MessageBox(hwndMain, "Do you want to write over the open file?",
                                    "File warning", MB_YESNO | MB_ICONWARNING);
                        if (a != IDYES)
                        {
                            isRecordFileOpen = false;
                            SendMessage(hwndMain, WM_COMMAND, MAKEWPARAM(RECORDVIDEO, BN_CLICKED), (LPARAM) hwndRecord);
                        }
                        
                        if (a == IDYES)
                        {
                            capCaptureSequence(hwndVideo);
                            isRecording = true;
                        }
                        
                    break;
                    }
                }
            }    
             break;
         }
      break;
     case WM_CREATE:
      
          RECT helpRect, minRect, exitRect;
          HRGN helpRgn, minRgn, exitRgn;
        hRegion1 = CreateRoundRectRgn(100,100,500, 500,20,20);   
          hwndVideo = capCreateCaptureWindow(
                          (LPSTR) "My Capture Window",
                          WS_CHILD | WS_VISIBLE,
                          150, 150, 300, 280,
                          (HWND) hwndMain,
                          (int) 1);
        hwndExit = CreateWindow (
                "button",
                "x",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER,
                470, 110, BUTTONSIZE, BUTTONSIZE,
                hwndMain,
                (HMENU) EXIT,
                (HINSTANCE)ghInstance,
                (LPVOID)NULL);
        hwndMin = CreateWindow (
                "button",
                "-",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER,
                450, 110, BUTTONSIZE, BUTTONSIZE,
                hwndMain,
                (HMENU) MINIMIZE,
                (HINSTANCE)ghInstance,
                (LPVOID)NULL);          
        hwndHelp = CreateWindow (
                "button",
                "?",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER,
                430, 110, BUTTONSIZE, BUTTONSIZE,
                hwndMain,
                (HMENU) HELP,
                (HINSTANCE)ghInstance,
                (LPVOID)NULL);
        hwndRecord = CreateWindow (
                "button",
                "录像",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER,
                250, 464, 90, 28,
                hwndMain,
                (HMENU) RECORDVIDEO,
                (HINSTANCE)ghInstance,
                (LPVOID)NULL);                
                
          GetClientRect(hwndHelp, &helpRect);
          GetClientRect(hwndMin, &minRect);
          GetClientRect(hwndExit, &exitRect);
                              
          helpRgn = CreateEllipticRgnIndirect(&helpRect);
        minRgn = CreateEllipticRgnIndirect(&minRect);
        exitRgn = CreateEllipticRgnIndirect(&exitRect);
                        
        SetWindowRgn(hwndExit,exitRgn,1);
        SetWindowRgn(hwndMin,minRgn,1);                                        
        SetWindowRgn(hwndHelp,helpRgn,1);                
    
        hwndSelCapDrvDlg = CreateDialog((HINSTANCE)ghInstance,
                                        MAKEINTRESOURCE( SELCAPDRVDLG  ),
                                        0, (DLGPROC)SelCapDrvProc);
        hwndSelCapDrvDlg_LBox = GetDlgItem(hwndSelCapDrvDlg,
                                              SELCAPDRVDLG_LSTBOX);
        EnumCapDrv();
      break;
      case WM_DESTROY:
           capPreview(hwndVideo, FALSE);
           capDriverDisconnect(hwndVideo);
         PostQuitMessage( 0 );
         break;
      default:
         return( DefWindowProc( hwndMain, msg, wParam, lParam ));

   }

   return 0;
}

LRESULT CALLBACK SelCapDrvProc( HWND hWnd, UINT msg,
                                WPARAM wParam, LPARAM lParam )
{

    switch(msg)
    {
        case WM_INITDIALOG:
            return TRUE;
        case WM_COMMAND:
            switch ( wParam )
            {
                case SELCAPDRVDLG_BUTTON:
                    int sel = 0;
                    SendMessage( hwndSelCapDrvDlg_LBox, LB_GETSELITEMS, 1, sel);
                    SendMessage( hwndVideo, WM_CAP_DRIVER_CONNECT, sel, 0L);
                    SendMessage( hwndSelCapDrvDlg, WM_CLOSE, 0, 0);
                    SendMessage( hwndVideo, WM_CAP_DRIVER_GET_CAPS,
                                 sizeof(CAPDRIVERCAPS), (LONG) (LPVOID) &CapDrvCaps);
                    capPreviewRate( hwndVideo, 66 );
                    capPreview( hwndVideo, TRUE );
            }
            return TRUE;
        case WM_CLOSE:
            DestroyWindow(hwndSelCapDrvDlg);
            return TRUE;
    }

    return( 0L );
}

int EnumCapDrv()
{
    char szDeviceName[80];
    char szDeviceVersion[80];
    char item[161];
    int i;

    for (i=0; i<10; i++)
    {
        if ( capGetDriverDescription(i, szDeviceName, sizeof(szDeviceName),
                                    szDeviceVersion, sizeof(szDeviceVersion)) )
        {
                strcpy(item, szDeviceName);
                strcat(item, " ");
                strcat(item, szDeviceVersion);
                SendMessage(hwndSelCapDrvDlg_LBox, LB_ADDSTRING, 0,
                           (LPARAM) item);
                SendMessage(hwndSelCapDrvDlg_LBox, LB_SETITEMDATA, i, (LPARAM) i);
        }
    }
    
    return 0;
}

VOID APIENTRY HandlePopupMenu(HWND hwnd, POINT pt)
{
    HMENU hmenu;    
    HMENU hmenuTrackPopup;
    hmenu = LoadMenu((HINSTANCE)ghInstance, "PopupMenu");
    if (hmenu == NULL)
        return;
    hmenuTrackPopup = GetSubMenu(hmenu, 0);
    ClientToScreen(hwnd, (LPPOINT) &pt);
    TrackPopupMenu(hmenuTrackPopup, TPM_LEFTALIGN | TPM_LEFTBUTTON,
        pt.x, pt.y, 0, hwnd, NULL);
    DestroyMenu(hmenu);
}

DWORD WINAPI videoThreadProc(LPVOID lParam)
{
    SetWindowText(hwndRecord, "停止");
    capCaptureSequence(hwndVideo);
    isRecording = true;
    while (!threadEnd)
        ;
    MessageBox(NULL, "Leaving Thread", "thread", NULL);
    return 0;
}



原帖出自点击打开链接

c# 获取 摄像头 实现录像

c# 获取 摄像头 实现录像
  • wyq1153
  • wyq1153
  • 2016年10月18日 17:19
  • 3853

c# 摄像头录像 及视频保存压缩等

来自于 网络: using System; using System.Collections.Generic; using System.Linq; using System.T...

ubuntu平台,VLC/LibVLC交叉编译,生成win32版本,以及LibVLC增加录像接口

这几天要用LibVLC播放视频,还需要增加一个在播放过程中录制的功能,于是把VLC完整编译了一遍,也添加了录像的接口。 原先是想在windows平台用mingw+msys交叉编译的,不过vlc官网一再...
  • avsuper
  • avsuper
  • 2017年08月02日 18:19
  • 240

C、C++、MFC、Win32读取txt文本信息的代码

1.C语言中文件操作。 2.C++语言中的文件操作。 3.Win32 API函数文件操作。 4.MFC CFile类文件操作。 5.MFC CFileDialog类的文件操作。 6.注册表文...

最新代码vlc 2.2.4的win32编译

最新vlc代码的Windows编译过程

win32窗口编程实例代码,自己敲出来的,仅供新人参考

// mydemo1.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "mydemo1.h" #define WM_MYMSG ...

Win32 鼠标绘图代码研究

http://download.csdn.net/detail/u012313945/9534690 下面摘录绘图部分代码以供研究;winmain和相关窗口过程函数参阅相关资料; #i...

win32 20子窗口控件的代码

汇编代码: ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ; Sample code for ; ...

gnu编译C++11,最好与win32公用代码

看makefile: #jingz's first file to test makeFile allTarget:stdafx_target 11_12_target g++ stdafx.o ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:win32用VFW预览和录像的代码
举报原因:
原因补充:

(最多只允许输入30个字)