CapPicture.CPP
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:10k
- #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,
- 0,
- 0,
- 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 *.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;
- }