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

📄 cappicture.cpp

📁 基于vc++
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/********************************************************************\
*  CapPicture.c: Source code for generic                             *
*                                                                    *
*  Comments: Video capture and display with some processing          *
*                                                                    *
*  Functions:                                                        *
*     WinMain      - Application entry point                         *

*     MainWndProc  - main window procedure                           *
*                                                                    *
*                                                                    *
\********************************************************************/

/*********************  Header Files  *********************/

#include <windows.h> // windows GUI and services
#include <vfw.h> // video for windows library
#include <commdlg.h> // common dialogs
#include "CapPicture.h" // resource header

/*********************  Prototypes  ***********************/

// main window procedure
LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM ); 
// Select Capture Driver Procedure
LRESULT WINAPI SelCapDrvProc( HWND, UINT, WPARAM, LPARAM );
// Enumerate Capture Drivers
int EnumCapDrv();
// Create the buttons on the main window
//int CreateWndButtons(); this doesnt work
// handle the right click popup menu
VOID APIENTRY HandlePopupMenu(HWND, POINT);
// video thread procedure
DWORD WINAPI videoThreadProc(LPVOID lParam);
/*******************  Global Variables ********************/

HANDLE ghInstance; // application instance
HWND hwndMain; // main window handle
HWND hwndVideo; // video capture window
HWND hwndSelCapDrvDlg; // Select the capture driver dialog
HWND hwndSelCapDrvDlg_LBox; // list box for select capture driver dialog
HWND hwndExit; // exit button
HWND hwndMin; // minimize button
HWND hwndHelp; // help button
HWND hwndRecord; // record button
HANDLE hVideoThread; // thread to stop the hang when recording video
HRGN hRegion1; // region for window shaping
CAPDRIVERCAPS CapDrvCaps; // driver capabilities
bool isRecordFileOpen = false; // flag set if record file is open 
char recordFile[260]; // file to hold recording
bool isPicFileOpen = false; // flag set if snapshot file is open
char pictureFile[260]; // file to hold snapshot
bool isRecording = false; // are we recording?
bool threadEnd = false; // should the video thread end?
/********************************************************************\
*																	 *
* 						CLASSES, ENUMS, & STRUCTS					 *
* 																	 *
/********************************************************************/ 	
					
/********************************************************************\
*  Function: int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int)    *
*                                                                    *
*   Purpose: Initializes Application                                 *

*                                                                    *
*  Comments: Register window class, create and display the main      *
*            window, and enter message loop.                         *
*                                                                    *
*                                                                    *
\********************************************************************/

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(0, 64, 128));//用来着色窗口背景的刷子
      wc.lpszMenuName = "GenericAppMenu";//指向菜单资源名的指针

      wc.cbClsExtra = 0;  //窗口类附加数据
      wc.cbWndExtra = 0;  //窗口类附加数据

      RegisterClass( &wc );//注册窗口
   }

   ghInstance = hInstance;

   hwndMain = CreateWindow( "GenericAppClass",//创建的这个窗体类的名称
      "Super Video", //窗口标题
      WS_POPUP,//窗口风格,全部风格见后表
      0,//窗口位置x坐标
      0, //窗口位置y坐标
      500, //窗口高度
      500,//窗口高度
      NULL, //父窗口句柄
      NULL,//菜单句柄
      hInstance, //应用程序句柄
      NULL//最后一个参数是附加数据,一般都是0
   );

   ShowWindow( hwndMain, nCmdShow ); //显示窗口
   
   //Set the main window to the region
   SetWindowRgn(hwndMain,hRegion1,1);

   while( GetMessage( &msg, NULL, 0, 0 ) ) {
      TranslateMessage( &msg );          //得到消息,处理回调函数

      DispatchMessage( &msg ); //该函数调度一个消息给窗口程序。通常调度从GetMessage取得的消息。消息被调度到的窗口程序即是MainProc()函数。

   }

   return msg.wParam;
}

/********************************************************************\
* Function: LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM) *
*                                                                    *
*  Purpose: Processes Application Messages                           *
*                                                                    *
* Comments: The following messages are processed                     *

*                                                                    *
*           WM_PAINT                                                 *
*           WM_CREATE                                                *
*           WM_DESTROY                                               *
*                                                                    *
*                                                                    *
\********************************************************************/

LRESULT CALLBACK MainWndProc( HWND hwndMain, UINT msg, WPARAM wParam,
   LPARAM lParam )
{
   HDC hDC = GetDC(hwndMain);
   RECT rc;    // client area             
   POINT pt;   // location of mouse click    

   switch( msg ) {

/**************************************************************\
*     WM_LBUTTONDBLCLK:                                        *
\**************************************************************/

	  case WM_LBUTTONDBLCLK:
	  	    SetFocus(hwndMain);//调用SetFocus方法以后,用户输入将指向指定的窗体或控件
	  	    break;
/**************************************************************\
*     WM_LBUTTONDOWN:                                          *
\**************************************************************/

      case WM_RBUTTONDOWN: 
            // Get the bounding rectangle of the client area. 
 
            GetClientRect(hwndMain, (LPRECT) &rc); 
 
            // Get the client coordinates for the mouse click.  
 
            pt.x = LOWORD(lParam); 

            pt.y = HIWORD(lParam); 
 
            // If the mouse click took place inside the client 
            // area, execute the application-defined function 
            // that displays the shortcut menu. 
 
            if (PtInRect((LPRECT) &rc, pt))  //函数可以用来判断给定的点是否在所在的矩形区域之内
                HandlePopupMenu(hwndMain, pt); 
            break; 
            
/**************************************************************\
*     WM_PAINT:                                                *
\**************************************************************/

      case WM_PAINT:
      	 //Give the region a red border
         FrameRgn(hDC,hRegion1,CreateSolidBrush(RGB(0,0,0)),2,2); 
         
         // brung our dialog to the foreground
		 BringWindowToTop(hwndSelCapDrvDlg);
         return( DefWindowProc( hwndMain, msg, wParam, lParam ));

/**************************************************************\
*     WM_COMMAND:                                              *
\**************************************************************/
	  case WM_COMMAND:
	  	 CAPSTATUS CapStatus;
	  		
	  	 switch( wParam ) {
 		
		 	case SOURCE:
	  	 	   if(CapDrvCaps.fHasDlgVideoSource)
	  	 	   		capDlgVideoSource(hwndVideo);
	  	 	   break;
	  	 	case FORMAT://why doesnt this work
	  	 	   //if(CapDrvCaps.fHasDlgVideoFormat)
	  	 	   //{		
	  	 	   		capDlgVideoFormat(hwndMain);
	  	 	   		// Are there new image dimensions
	  	 	   		capGetStatus(hwndVideo, &CapStatus, sizeof(CAPSTATUS));
	  	 	   		SetWindowPos(hwndVideo, NULL, 0, 0, CapStatus.uiImageWidth,
	  	 	   					CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);//对话框(收缩,扩展)功能
	  	 	   //}
	  	 	   break;
	  	 	case DISPLAY://why doesnt this work
	  	 		//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; // open file name structure
			   		
			   		// initialize OPENFILENAME
			   		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;
					
					// Display the Save dialog box. 
					if(GetSaveFileName(&ofn) == TRUE)
					{
						strcpy(recordFile, ofn.lpstrFile);
						strcat(recordFile, ".avi");
						isRecordFileOpen = true; 
						
						// create the video capture thread				
						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) // we already have a file selected
				{
					if(isRecording) // we're already recording
					{
						threadEnd = true;
						// end the capture and save it
						capFileSaveAs(hwndVideo, recordFile);
					
						// make the record button say "Record Video"

⌨️ 快捷键说明

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