Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
CapPicture.CPP
Package: videoCapture.rar [view]
Upload User: hzllkj
Upload Date: 2009-12-19
Package Size: 21k
Code Size: 18k
Category:
Video Capture
Development Platform:
Visual C++
- /********************************************************************
- * 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,
- 0,
- 500,
- 500,
- NULL,
- NULL,
- hInstance,
- NULL
- );
- ShowWindow( hwndMain, nCmdShow );
- //Set the main window to the region
- SetWindowRgn(hwndMain,hRegion1,1);
- while( GetMessage( &msg, NULL, 0, 0 ) ) {
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- 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);
- 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*.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"
- SetWindowText(hwndRecord, "Record Video");
- isRecording = false;
- break;
- }
- if(!isRecording ) // we're not recording, but a file's selected
- {
- 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;
- /**************************************************************
- * WM_CREATE: *
- **************************************************************/
- case WM_CREATE:
- RECT helpRect, minRect, exitRect;
- HRGN helpRgn, minRgn, exitRgn;
- // make the main region
- hRegion1 = CreateRoundRectRgn(0,0,500,400, 200, 200);
- // create video capture window
- hwndVideo = capCreateCaptureWindow(
- (LPSTR) "My Capture Window",
- WS_CHILD | WS_VISIBLE,
- 160, 120, 200, 148,
- (HWND) hwndMain,
- (int) 1);
- // Create the main window's buttons
- // create the close button
- hwndExit = CreateWindow (
- "button", // Builtin button class
- "x", // button text
- WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // styles
- 275, 10, BUTTONSIZE, BUTTONSIZE, // position and size
- hwndMain, // Parent is main window
- (HMENU) EXIT,// Control ID: EXIT
- (HINSTANCE)ghInstance,
- (LPVOID)NULL);
- // create the minimize button
- hwndMin = CreateWindow (
- "button", // Builtin button class
- "-", // button text
- WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // styles
- 250, 10, BUTTONSIZE, BUTTONSIZE, // position and size
- hwndMain, // Parent is main window
- (HMENU) MINIMIZE,// Control ID: MINIMIZE
- (HINSTANCE)ghInstance,
- (LPVOID)NULL);
- // create the help button
- hwndHelp = CreateWindow (
- "button", // Builtin button class
- "?", // button text
- WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // styles
- 225, 10, BUTTONSIZE, BUTTONSIZE, // position and size
- hwndMain, // Parent is main window
- (HMENU) HELP,// Control ID: HELP
- (HINSTANCE)ghInstance,
- (LPVOID)NULL);
- // create the record video button
- hwndRecord = CreateWindow (
- "button", // Builtin button class
- "Record Video", // button text
- WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // styles
- 200, 280, 90, 28, // position and size
- hwndMain, // Parent is main window
- (HMENU) RECORDVIDEO,// Control ID: RECORDVIDEO
- (HINSTANCE)ghInstance,
- (LPVOID)NULL);
- // get the button rectangles
- GetClientRect(hwndHelp, &helpRect);
- GetClientRect(hwndMin, &minRect);
- GetClientRect(hwndExit, &exitRect);
- // create the button regions
- helpRgn = CreateEllipticRgnIndirect(&helpRect);
- minRgn = CreateEllipticRgnIndirect(&minRect);
- exitRgn = CreateEllipticRgnIndirect(&exitRect);
- //Set the window to the region
- SetWindowRgn(hwndExit,exitRgn,1);
- SetWindowRgn(hwndMin,minRgn,1);
- SetWindowRgn(hwndHelp,helpRgn,1);
- // create the SelCapDrv dialog
- hwndSelCapDrvDlg = CreateDialog((HINSTANCE)ghInstance,
- MAKEINTRESOURCE( SELCAPDRVDLG ),
- 0, (DLGPROC)SelCapDrvProc);
- // get the handle to the list box
- hwndSelCapDrvDlg_LBox = GetDlgItem(hwndSelCapDrvDlg,
- SELCAPDRVDLG_LSTBOX);
- EnumCapDrv();
- break;
- /****************************************************************
- *WM_DESTROY: PostQuitMessage() is called and get rid of vfw stuff*
- ****************************************************************/
- case WM_DESTROY:
- capPreview(hwndVideo, FALSE); // end preview
- capDriverDisconnect(hwndVideo); // disconnect from driver
- PostQuitMessage( 0 );
- break;
- /**************************************************************
- * Let the default window proc handle all other messages *
- **************************************************************/
- default:
- return( DefWindowProc( hwndMain, msg, wParam, lParam ));
- }
- return 0;
- }
- LRESULT CALLBACK SelCapDrvProc( HWND hWnd, UINT msg, /*callback procedure */
- WPARAM wParam, LPARAM lParam )
- {
- switch(msg)
- {
- // dialog created
- case WM_INITDIALOG:
- return TRUE;
- // command
- case WM_COMMAND:
- switch ( wParam )
- {
- // user clicked the select driver button
- case SELCAPDRVDLG_BUTTON:
- int sel = 0;
- // get the selected driver
- SendMessage( hwndSelCapDrvDlg_LBox, LB_GETSELITEMS, 1, sel);
- // connect to the driver
- SendMessage( hwndVideo, WM_CAP_DRIVER_CONNECT, sel, 0L);
- // then close this dialog
- SendMessage( hwndSelCapDrvDlg, WM_CLOSE, 0, 0);
- // update the driver capabilities
- SendMessage( hwndVideo, WM_CAP_DRIVER_GET_CAPS,
- sizeof(CAPDRIVERCAPS), (LONG) (LPVOID) &CapDrvCaps);
- // set preview rate to 66 miliseconds
- capPreviewRate( hwndVideo, 66 );
- // start preview video
- capPreview( hwndVideo, TRUE );
- }
- return TRUE;
- // user wants to close dialog
- case WM_CLOSE:
- DestroyWindow(hwndSelCapDrvDlg);
- return TRUE;
- }
- return( 0L );
- }
- int EnumCapDrv() // enumerate the installed capture drivers
- {
- char szDeviceName[80]; // driver name
- char szDeviceVersion[80]; // driver version
- char item[161]; // concatinated string
- int i; // counter
- for (i=0; i<10; i++)
- {
- if ( capGetDriverDescription(i, szDeviceName, sizeof(szDeviceName),
- szDeviceVersion, sizeof(szDeviceVersion)) )
- {
- strcpy(item, szDeviceName);
- strcat(item, " ");
- strcat(item, szDeviceVersion);
- // add item to list box
- SendMessage(hwndSelCapDrvDlg_LBox, LB_ADDSTRING, 0,
- (LPARAM) item);
- SendMessage(hwndSelCapDrvDlg_LBox, LB_SETITEMDATA, i, (LPARAM) i);
- }
- }
- return 0;
- }
- /*int CreateWndButtons()
- {//why doesnt this work
- RECT rc; // window rectangle
- GetClientRect(hwndMain, &rc);
- // create the button
- hwndExit = CreateWindow (
- "button", // Builtin button class
- "X", // button text
- WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // styles
- 100, 100, BUTTONSIZE, BUTTONSIZE, // position and size
- hwndMain, // Parent is main window
- (HMENU) EXIT,// Control ID: EXIT
- (HINSTANCE)ghInstance,
- (LPVOID)NULL);
- return 0;
- }
- */
- VOID APIENTRY HandlePopupMenu(HWND hwnd, POINT pt)
- {
- HMENU hmenu; // menu template
- HMENU hmenuTrackPopup; // shortcut menu
- // Load the menu template containing the shortcut menu from the
- // application's resources.
- hmenu = LoadMenu((HINSTANCE)ghInstance, "PopupMenu");
- if (hmenu == NULL)
- return;
- // Get the first shortcut menu in the menu template. This is the
- // menu that TrackPopupMenu displays.
- hmenuTrackPopup = GetSubMenu(hmenu, 0);
- // TrackPopup uses screen coordinates, so convert the
- // coordinates of the mouse click to screen coordinates.
- ClientToScreen(hwnd, (LPPOINT) &pt);
- // Draw and track the shortcut menu.
- TrackPopupMenu(hmenuTrackPopup, TPM_LEFTALIGN | TPM_LEFTBUTTON,
- pt.x, pt.y, 0, hwnd, NULL);
- // Destroy the menu.
- DestroyMenu(hmenu);
- }
- DWORD WINAPI videoThreadProc(LPVOID lParam)
- {
- // make the record button say "Stop Recording"
- SetWindowText(hwndRecord, "Stop Recording");
- // capture the video
- capCaptureSequence(hwndVideo);
- isRecording = true;
- // don't exit the thread until the record button is pressed again
- while (!threadEnd)
- ;
- MessageBox(NULL, "Leaving Thread", "thread", NULL);
- return 0;
- }