📄 camapp.cpp
字号:
/*
*
* Copyright (C) 2004-2006, Freescale Semiconductor, Inc. All Rights Reserved.
* THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
* AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
*
* File: APPS/CAMAPP/Camapp.cpp
* Purpose: This file contians the WinMain, WinProc, initialization, and
* finalization functions for the Camapp application.
*/
/*********************************************************************
INCLUDE FILES
*********************************************************************/
#include <windows.h>
#include "CameraWindow.h"
/*********************************************************************
GLOBAL DEFINITIONS
*********************************************************************/
/*********************************************************************
GLOBAL OR STATIC VARIABLES
*********************************************************************/
WCHAR g_szCamAppName[] = L"CamApp";
CAMERAWINDOW *g_pCameraWindow = NULL;
HINSTANCE g_hInst = NULL;
BOOL g_bInit = FALSE;
/*********************************************************************
STATIC FUNCTION PROTOTYPES
*********************************************************************/
/*********************************************************************
EXPORTED FUNCTIONS
*********************************************************************/
/********************************************************************
*
* FUNCTION: WinProc
*
* DESCRIPTION: Handles the Window Messages for the main Camera Application window.
*
* PARAMETERS: HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam
*
* RETURNS: Return zero when successed
*
********************************************************************/
HRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hDC;
BOOL bResult = FALSE;
switch (msg)
{
case WM_CREATE:
if (NULL == g_pCameraWindow)
{
g_pCameraWindow = new CCameraWindow(hWnd, g_hInst);
}
break;
case WM_ACTIVATE:
if (!g_bInit && (NULL == g_pCameraWindow|| FALSE == g_pCameraWindow->Init()))
{
g_bInit = TRUE;
::MessageBox(hWnd, TEXT("Unable to initialize CamApp!"), TEXT("Error"),
MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND | MB_APPLMODAL);
delete g_pCameraWindow;
g_pCameraWindow = NULL;
::PostQuitMessage(0);
break;
}
g_bInit = TRUE;
if (LOWORD(wParam) == WA_INACTIVE)
{
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinProc: received WA_INACTIVE \r\n")));
g_pCameraWindow->PausePreviewThread();
}
else
{
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinProc: received WA_ACTIVE \r\n")));
g_pCameraWindow->RevertPreviewThread();
}
break;
case WM_COMMAND:
{
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinProc: received WM_COMMAND \r\n")));
bResult = g_pCameraWindow->OnCommand(LOWORD(wParam), lParam);
if (bResult == FALSE)
{
::MessageBox(hWnd, TEXT("Something wrong when running this command!"), TEXT("Error"),
MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND | MB_APPLMODAL);
// ::PostQuitMessage(0);
}
}
break;
case WM_HOTKEY:
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinProc: received WM_HOTKEY \r\n")));
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinProc: hotkey id= %x, LOWORD(lParam)= %x, HIWORD(lParam)= %x\r\n"), wParam, LOWORD(lParam), HIWORD(lParam)));
if ((LOWORD(lParam) == MOD_ALT) && (HIWORD(lParam) == 'C'))
{
g_pCameraWindow->OnCommand(CamApp_BTN_ID_CAPTURE, lParam);
}
else if ((LOWORD(lParam) == MOD_ALT) && (HIWORD(lParam) == 'I'))
{
g_pCameraWindow->OnCommand(CamApp_BTN_ID_VIEW, lParam);
}
else if ((LOWORD(lParam) == MOD_ALT) && (HIWORD(lParam) == 'R'))
{
g_pCameraWindow->OnCommand(CamApp_BTN_ID_ROTATE, lParam);
}
break;
case WM_CLOSE:
if (NULL != g_pCameraWindow)
{
delete g_pCameraWindow;
g_pCameraWindow = NULL;
}
::PostQuitMessage(0);
return DefWindowProc(hWnd, msg, wParam, lParam);
case WM_PAINT:
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinProc: received WM_PAINT \r\n")));
if (!wParam)
{
hDC = BeginPaint(hWnd, &ps);
}
else
{
hDC = (HDC)wParam;
}
if (NULL != g_pCameraWindow)
{
g_pCameraWindow->OnPaint(hDC, &(ps.rcPaint));
}
if (!wParam)
{
EndPaint(hWnd, &ps);
}
break;
case PD_CLOSED:
if (NULL != g_pCameraWindow)
{
g_pCameraWindow->PicBrowserDlgClosed();
}
break;
case WM_TIMER:
if (NULL != g_pCameraWindow)
{
g_pCameraWindow->OnTimer((UINT)wParam);
}
break;
case WM_ENTERMENULOOP:
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinProc:WM_ENTERMENULOOP \r\n")));
g_pCameraWindow->PausePreviewThread();
break;
case WM_EXITMENULOOP:
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinProc:WM_EXITMENULOOP \r\n")));
g_pCameraWindow->RevertPreviewThread();
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
/********************************************************************
*
* FUNCTION: InitCamApp
*
* DESCRIPTION: Initialize the window class for the CamApp application
*
* PARAMETERS: HINSTANCE hInstance
*
* RETURNS: Return TRUE when success, otherwise return FALSE
*
********************************************************************/
BOOL InitCamApp(HINSTANCE hInstance)
{
WNDCLASS wc;
BOOL bResult = TRUE;
// create and register a new window class
wc.style = 0;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(DWORD);
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ICON);
wc.hbrBackground = reinterpret_cast<HBRUSH>(GetStockObject(LTGRAY_BRUSH));
wc.lpszMenuName = NULL,
wc.lpszClassName = g_szCamAppName;
if (FALSE == RegisterClass(&wc))
bResult = FALSE;
return bResult;
}
/********************************************************************
*
* FUNCTION: FiniCamApp
*
* DESCRIPTION: Shutdown the main window
*
* PARAMETERS: HINSTANCE hInstance
*
* RETURNS: Return TRUE when success, otherwise return FALSE
*
********************************************************************/
BOOL FiniCamApp(HINSTANCE hInstance)
{
BOOL bResult = TRUE;
if (NULL != g_pCameraWindow)
{
delete g_pCameraWindow;
g_pCameraWindow = NULL;
}
UnregisterClass(g_szCamAppName, hInstance);
return bResult;
}
/********************************************************************
*
* FUNCTION: WinMain
*
* DESCRIPTION: The entry point for the CAMAPP application
*
* PARAMETERS: HINSTANCE hInstance,
* HINSTANCE hPrev,
* LPTSTR szCmdLine,
* int iCmdShow
*
* RETURNS: Message's wParam parameter indicates success
*
********************************************************************/
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrev,
LPTSTR szCmdLine,
int iCmdShow
)
{
HWND hWnd;
MSG msg;
HKEY hkResult = NULL;
BOOL bOpenMultiple = FALSE;
if (FALSE == InitCamApp(hInstance))
{
MessageBox(NULL, TEXT("Unable to initialize CamApp!"), TEXT("Error"),
MB_OK | MB_ICONEXCLAMATION);
FiniCamApp(hInstance);
return 0;
}
//Save Instance
g_hInst = hInstance;
if( !bOpenMultiple )
{
hWnd = FindWindow(g_szCamAppName, NULL);
if (hWnd)
{
//If it is already running, then focus on the window
SetForegroundWindow (hWnd);
return 0;
}
}
// create a window for the player
DWORD dwExStyle, dwStyle;
int iXPos, iYPos;
int iWidth, iHeight;
RECT rcWorkArea;
// Initialize the window position to be the upper left corner of the screen
iXPos = 0;
iYPos = 0;
dwExStyle = 0;
dwStyle = WS_VISIBLE;
if (::SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWorkArea, 0))
{
iWidth = rcWorkArea.right - rcWorkArea.left;
iHeight = rcWorkArea.bottom - rcWorkArea.top;
}
else
{
HDC hdc = ::GetDC(NULL);
iWidth = ::GetDeviceCaps(hdc, HORZRES);
iHeight = ::GetDeviceCaps(hdc, VERTRES) - ::GetSystemMetrics(SM_CYMENU);
::ReleaseDC(NULL, hdc);
}
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinMain: iWidth = %d, iHeight = %d\r\n"),iWidth, iHeight));
hWnd = ::CreateWindowEx(dwExStyle,
g_szCamAppName,
TEXT("Camera Application"),
dwStyle,
iXPos,
iYPos,
iWidth,
iHeight,
NULL,
NULL,
hInstance,
NULL);
if (hWnd == 0)
{
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinMain: CreateWindowEx Failed. Last error code is %d \r\n"), GetLastError()));
FiniCamApp(hInstance);
return 0;
}
// Register hot keys for the window
if (!RegisterHotKey(hWnd, CAMAPP_CAPTURE_HOTKEY_ID, MOD_ALT, 'C'))
{
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinMain: RegisterHotKey Failed (Capture). Last error code is %d \r\n"), GetLastError()));
FiniCamApp(hInstance);
return 0;
}
// Register hot keys for the window
if (!RegisterHotKey(hWnd, CAMAPP_VIEW_HOTKEY_ID, MOD_ALT, 'I'))
{
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinMain: RegisterHotKey Failed (View). Last error code is %d \r\n"), GetLastError()));
FiniCamApp(hInstance);
return 0;
}
// Register hot keys for the window
if (!RegisterHotKey(hWnd, CAMAPP_ROTATE_HOTKEY_ID, MOD_ALT, 'R'))
{
RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("WinMain: RegisterHotKey Failed (Rotation). Last error code is %d \r\n"), GetLastError()));
FiniCamApp(hInstance);
return 0;
}
if (NULL != g_pCameraWindow)
{
::ShowWindow(hWnd, iCmdShow);
::UpdateWindow(hWnd);
}
// Event loop... wait for a close
while (::GetMessage(&msg, NULL, 0, 0))
{
if (NULL == g_pCameraWindow || !g_pCameraWindow->DialogMessage(&msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
UnregisterHotKey(hWnd, CAMAPP_CAPTURE_HOTKEY_ID);
UnregisterHotKey(hWnd, CAMAPP_VIEW_HOTKEY_ID);
UnregisterHotKey(hWnd, CAMAPP_ROTATE_HOTKEY_ID);
// Perform shutdown on the player
FiniCamApp(hInstance);
::DestroyWindow(hWnd);
return msg.wParam;
}
/*********************************************************************
END OF FILE
*********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -