player.cpp

来自「一个摄像机的程序」· C++ 代码 · 共 490 行 · 第 1/2 页

CPP
490
字号
//Player.cpp

#define INC_OLE2
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include <commdlg.h>
#include <string.h>
#include <stdlib.h>
#include <direct.h>
#include <digitalv.h>
#include <vfw.h>
#include "player.h"

#include "resource.h"


// Globals
HWND hWndMCI;                 /* window handle of the movie */
BOOL bIsOpenMovie = FALSE;        /* Open flag: TRUE == movie open, FALSE = none */
HMENU hMenuBar = NULL;          /* menu bar handle */
char szAppName [] = "Player";
char playfiledir[300];	
//char seps[] = "*\t\n";
char seps[] = "*";
#define WM_USER_PLAY 0x00401


// function declarations 
long FAR PASCAL WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
void OpenMCIMovieFile(HWND hWnd);
void OpenMCIMovieFileInit(HWND hWnd);
void UpdateMenubar(HWND hWnd);
void UpdateTitle(HWND hWnd, LPSTR lpstrMovie);
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);



/********************************************************************
************************** FUNCTIONS ********************************
********************************************************************/


//InitWindows - initialize stuff
HWND InitWindows(HINSTANCE hInstance, HINSTANCE hPrevInstance, int nCmdShow)
{
        HWND            hWnd;   /* window handle to return */
        int             iWinHeight;
        WORD    wVer;

        /* first let's make sure we are running on 1.1 */
        wVer = HIWORD(VideoForWindowsVersion());
        if (wVer < 0x010a){
                /* oops, we are too old, blow out of here */
                MessageBeep(MB_ICONHAND);
                MessageBox(NULL, "Video for Windows version is too old",
                          "Error", MB_OK|MB_ICONSTOP);
                return FALSE;
        }

        if (!hPrevInstance){
                WNDCLASS    wndclass;

                wndclass.style         = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
                wndclass.lpfnWndProc   = WndProc;
                wndclass.cbClsExtra    = 0;
                wndclass.cbWndExtra    = 0;
                wndclass.hInstance     = hInstance;
                wndclass.hIcon         = LoadIcon (hInstance, "AppIcon");
                wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
                wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
                wndclass.lpszMenuName  = szAppName;
                wndclass.lpszClassName = szAppName;

                if (!RegisterClass(&wndclass)){
                        MessageBox(NULL, "RegisterClass failure", szAppName, MB_OK);
                        return NULL;
                }
        }

        iWinHeight = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYMENU) +
                        (GetSystemMetrics(SM_CYFRAME) * 2);

        /* create the main window for the app */
        hWnd = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW |
                WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 180, iWinHeight,
                NULL, NULL, hInstance, NULL);

		 


        if (hWnd == NULL){
                MessageBox(NULL, "CreateWindow failure", szAppName, MB_OK);
                return NULL;
        }

        hMenuBar = GetMenu(hWnd);       /* get the menu bar handle */
        UpdateMenubar(hWnd);            /* update menu bar to disable Movie menu */

        /* Show the main window */
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);

        /* create the movie window using MCIWnd that has no file open initially */
        hWndMCI = MCIWndCreate(hWnd, hInstance, WS_CHILD |WS_VISIBLE | MCIWNDF_NOOPEN |
                                MCIWNDF_NOERRORDLG | MCIWNDF_NOTIFYSIZE | MCIWNDF_SHOWMODE , NULL);
		
        if (!hWndMCI){
                /* we didn't get the movie window, destroy the app's window and bail out */
                DestroyWindow(hWnd);
                return NULL;
        }		
		
		MCIWndSetInactiveTimer(hWndMCI, 20);
		MCIWndSetActiveTimer(hWndMCI, 20);
		MCIWndSetRepeat( hWndMCI, TRUE );

		ShowWindow(hWndMCI, SW_SHOW);		

        return hWnd;
}




//WinMain - main routine.                                       |
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                LPSTR lpszCmdParam, int nCmdShow)
{
        HWND        hWnd;
        MSG         msg;		


        if ((hWnd = InitWindows(hInstance, hPrevInstance,nCmdShow)) == NULL)
                return 0;       /* died initializing, bail out */

		if (strlen(lpszCmdParam)!=0) {
			/////////////////////////
			/*
			token=strtok(lpszCmdParam,seps);
			if (token==NULL) {
				//MessageBox(NULL,"Error in reading parameters. AVI Aborted","Error",MB_OK);
				exit(1);
			}
			strcpy(playfiledir,token);		
			//playfiledir[strlen(playfiledir)]=0;
			PostMessage(hWnd,WM_USER_PLAY,0,0);
			*/

			strcpy(playfiledir,lpszCmdParam);
			PostMessage(hWnd,WM_USER_PLAY,0,0);
		}

		

        while (GetMessage(&msg, NULL, 0, 0)){
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }
        return msg.wParam;
}



//WndProc - window proc for the app                            
long FAR PASCAL WndProc (HWND hWnd, UINT message, WPARAM wParam,
                                                LPARAM lParam)
{
        PAINTSTRUCT ps;
        WORD w;
        WORD    wMenu;
        RECT    rc;

        switch (message){
				case WM_USER_PLAY :
					//MessageBox(NULL,playfiledir,"note",MB_OK);
					OpenMCIMovieFileInit(hWnd);
					return 0;

                case WM_CREATE:
                        return 0;

                case WM_INITMENUPOPUP:
                        /* be sure this isn't the system menu */
                        if (HIWORD(lParam))
                                return DefWindowProc(hWnd, WM_INITMENUPOPUP,
                                                wParam, lParam);

                        wMenu = LOWORD(lParam);
                        switch (wMenu){
                                case 0:   /* file menu */
                                        /* turn on/off CLOSE & PLAY */
                                        if (bIsOpenMovie) w = MF_ENABLED|MF_BYCOMMAND;
                                        else            w = MF_GRAYED|MF_BYCOMMAND;
                                        EnableMenuItem((HMENU)wParam, IDM_CLOSE, w);
                                        break;
                        } /* switch */
                        break;

                case WM_COMMAND:
                        switch (wParam) {
                                /* File Menu */
							    case ID_PLAY:
                                        MCIWndPlay(hWndMCI);
                                        break;
								case ID_STOP:
                                        MCIWndStop(hWndMCI);										
                                        break;
                                case IDM_OPEN:
                                        OpenMCIMovieFile(hWnd);
                                        break;
                                case IDM_CLOSE:
                                        bIsOpenMovie = FALSE;
                                        MCIWndClose(hWndMCI);         // close the movie
                                        ShowWindow(hWndMCI, SW_HIDE); //hide the window
                                        UpdateMenubar(hWnd);
                                        UpdateTitle(hWnd, NULL);     // title bar back to plain
                                        break;
                                case IDM_EXIT:
                                        PostMessage(hWnd, WM_CLOSE, 0, 0L);
                                        break;

                                
                                case IDM_ABOUT:
                                        DialogBox(GetWindowInstance(hWnd),
                                                  MAKEINTRESOURCE(IDD_ABOUT),
                                                  hWnd,
                                                  //(int (__stdcall *)(void)) AboutDlgProc);  //VC++ 5
												  AboutDlgProc); //VC++ 6
                                        break;

                        }
                        return 0;

                case WM_PAINT:
                        BeginPaint(hWnd, &ps);
                        EndPaint(hWnd, &ps);
                        return 0;

                case WM_SIZE:
                        if (!IsIconic(hWnd) && hWndMCI && bIsOpenMovie)
                                MoveWindow(hWndMCI,0,0,LOWORD(lParam),HIWORD(lParam),TRUE);
                        break;

                case WM_DESTROY:

⌨️ 快捷键说明

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