?? cecamera.cpp
字號:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
// ***************************************************************************
// CECamera.cpp
//
// Take pictures and videos using Camera native API.
//
#include <aygshell.h>
#include "Macros.h"
#include "resource.h"
// Macros
#define MAX_INITIAL_DIR MAX_PATH
#define MAX_FILE_NAME MAX_PATH
#define MAX_TITLE 64
#define MAX_RESOLUTION_WIDTH 10
#define MAX_RESOLUTION_HEIGHT 10
#define MAX_VIDEO_TIME_LIMIT 10
#define MAX_PLATFORM 64
#define MAX_CLASSNAME 64
#define MAX_MESSAGE MAX_PATH * 2
#define CECAMERA_DEFAULT_INITIAL_DIR NULL
#define CECAMERA_DEFAULT_FILE_NAME NULL
#define CECAMERA_DEFAULT_TITLE NULL
#define CECAMERA_DEFAULT_STILL_QUALITY CAMERACAPTURE_STILLQUALITY_DEFAULT
#define CECAMERA_DEFAULT_VIDEO_TYPES CAMERACAPTURE_VIDEOTYPE_ALL
#define CECAMERA_DEFAULT_RESOLUTION_WIDTH 0
#define CECAMERA_DEFAULT_RESOLUTION_HEIGHT 0
#define CECAMERA_DEFAULT_VIDEO_TIME_LIMIT 0
#define CECAMERA_DEFAULT_MODE CAMERACAPTURE_MODE_STILL
#define CECAMERA_MUTEX_NAME TEXT("__CECAMERA_MUTEX__")
#define CECAMERA_MAINDLG_CLASSNAME TEXT("Dialog")
// Global variables
BOOL g_bSmartphone = FALSE;
HINSTANCE g_hInstance = NULL;
HMENU g_hMainMenu = NULL;
LPCTSTR g_szCaption;
CAMERACAPTURE_STILLQUALITY g_StillQuality = CECAMERA_DEFAULT_STILL_QUALITY;
CAMERACAPTURE_VIDEOTYPES g_VideoTypes = CECAMERA_DEFAULT_VIDEO_TYPES;
CAMERACAPTURE_MODE g_Mode = CECAMERA_DEFAULT_MODE;
// Forward declarations of the functions
BOOL IsSmartphone();
BOOL IsFocusOnEditControl();
BOOL InitDialog(HWND hwndDlg);
VOID StartCamera(HWND hwndDlg);
VOID ShowAboutBox(HWND hwndDlg);
VOID ResetOptions(HWND hwndDlg);
VOID ChangeMode(WORD wMode);
VOID ChangeStillQuality(WORD wStillQuality);
VOID ChangeVideoTypes(WORD wVideoTypes);
VOID ChangeOptions(HWND hwndDlg, WORD wOptions);
BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
// ***************************************************************************
// Function Name: WinMain
//
// Purpose: Main entry point into the CECamera program
//
// Arguments: Standard WinMain arguments
//
// Return Values: 0
//
// Description:
// Checks to see if a previous instance of the program is running,
// and if it is not, it pops up a dialog box allowing the user to
// specify the parameters for SHCameraCapture() API to launch Camera app.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
{
HRESULT hr;
HWND hWndPreInstance;
HANDLE hMutex = NULL;
// Load the caption of the main dialog from resource
g_szCaption = (LPCTSTR)LoadString(hInstance, IDS_CAPTION, NULL, 0);
CPR(g_szCaption);
// Use a global named mutex to detect another instance of CECamera
hMutex = CreateMutex(NULL, FALSE, CECAMERA_MUTEX_NAME);
CPR(hMutex);
if (ERROR_ALREADY_EXISTS == GetLastError())
{
// Already an instance running - attempt to switch to it and then exit
hWndPreInstance = FindWindow(CECAMERA_MAINDLG_CLASSNAME, g_szCaption);
CPR(hWndPreInstance);
SetForegroundWindow(hWndPreInstance);
}
else
{
// Store the hInstance
g_hInstance = hInstance;
// Determine if platform is Smartphone
g_bSmartphone = IsSmartphone();
// Create the dialog box
DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAINDLG), NULL, (DLGPROC)DialogProc);
}
Error:
if (NULL != hMutex)
{
// Release the global named mutex
CloseHandle(hMutex);
}
return hr;
}
// ***************************************************************************
// Function Name: DialogProc
//
// Purpose: Message Handler for CECamera Dialog Box
//
// Arguments: Standard Dialog Procedure Arguments
//
// Return Values:
// Returns TRUE if it processed the message, or FALSE if it did not.
//
// Description:
// Dialog Procedure for the main CECamera Dialog.
BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
BOOL bHandled = TRUE;
switch (uMsg)
{
case WM_INITDIALOG:
// Make sure the dialog box was created
if (!InitDialog(hwndDlg))
{
PostMessage(hwndDlg, WM_CLOSE, 0, 0);
}
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_START:
StartCamera(hwndDlg);
break;
case IDM_ABOUT:
ShowAboutBox(hwndDlg);
break;
case IDM_RESET:
ResetOptions(hwndDlg);
break;
case IDM_MODE_STILL:
// Fall through
case IDM_MODE_VIDEOONLY:
// Fall through
case IDM_MODE_VIDEOWITHAUDIO:
ChangeMode(LOWORD(wParam));
break;
case IDM_STILLQUALITY_DEFAULT:
// Fall through
case IDM_STILLQUALITY_LOW:
// Fall through
case IDM_STILLQUALITY_NORMAL:
// Fall through
case IDM_STILLQUALITY_HIGH:
ChangeStillQuality(LOWORD(wParam));
break;
case IDM_VIDEOTYPES_ALL:
// Fall through
case IDM_VIDEOTYPES_STANDARD:
// Fall through
case IDM_VIDEOTYPES_MESSAGING:
ChangeVideoTypes(LOWORD(wParam));
break;
case IDC_CHECK_INITIAL_DIR:
// Fall through
case IDC_CHECK_DEFAULT_FILE_NAME:
// Fall through
case IDC_CHECK_TITLE:
// Fall through
case IDC_CHECK_RESOLUTION:
// Fall through
case IDC_CHECK_VIDEO_TIME_LIMIT:
ChangeOptions(hwndDlg, LOWORD(wParam));
break;
case IDCANCEL:
// Fall through
case IDM_EXIT:
PostMessage(hwndDlg, WM_CLOSE, 0, 0);
break;
}
break;
case WM_CLOSE:
EndDialog(hwndDlg, IDCANCEL);
break;
case WM_ACTIVATE:
if (WA_ACTIVE == LOWORD(wParam))
{
// Set the focus to the main dialog if we are activated
SetFocus(hwndDlg);
}
break;
case WM_HOTKEY:
if (g_bSmartphone && VK_TBACK == HIWORD(lParam))
{
if (IsFocusOnEditControl())
{
// The current control with focus is an edit control
if (MOD_KEYUP & LOWORD(lParam))
{
// Delete a character on each KEYUP
SendMessage(GetFocus(), WM_CHAR, VK_BACK, 0);
}
else if (MOD_HOLD & LOWORD(lParam))
{
// Clear content from edit controls while pressing and holding the Back key
SetWindowText(GetFocus(), TEXT(""));
}
}
else if (MOD_KEYUP & LOWORD(lParam))
{
// The current control with focus is not an edit control,
// close the dialog and then back to the previous application
PostMessage(hwndDlg, WM_CLOSE, 0, 0);
SHNavigateBack();
}
}
break;
default:
// Specify that we didn't process this message, let the default
// dialog box window procedure to process this message
bHandled = FALSE;
break;
}
return bHandled;
}
// **************************************************************************
// Function Name: IsSmartphone
//
// Purpose: Determine if platform is Smartphone
//
// Arguments: none
//
// Return Values:
// TRUE if the current platform is a Smartphone platform
// FALSE if the current platform is not a Smartphone platform
//
// Description:
// This function retrieves the current platforms type and then
// does a case insensitive string comparison.
BOOL IsSmartphone()
{
HRESULT hr;
TCHAR szPlatform[MAX_PLATFORM] = { 0 };
BOOL bResult = FALSE;
CBR(SystemParametersInfo(SPI_GETPLATFORMTYPE, ARRAYSIZE(szPlatform), szPlatform, 0));
if (0 == _tcsicmp(szPlatform, TEXT("Smartphone")))
{
bResult = TRUE;
}
Error:
return bResult;
}
// **************************************************************************
// Function Name: IsFocusOnEditControl
//
// Purpose: Determine if the current control with focus is an edit control
//
// Arguments: none
//
// Return Values:
// TRUE if the current control with focus is an edit control
// FALSE if the current control with focus is not an edit control
//
// Description:
// This function retrieves the window class name of the current control
// with focus and then does a case insensitive string comparison.
BOOL IsFocusOnEditControl()
{
HRESULT hr;
TCHAR szClassName[MAX_CLASSNAME] = { 0 };
BOOL bResult = FALSE;
CBR(0 != GetClassName(GetFocus(), szClassName, ARRAYSIZE(szClassName)));
if (0 == _tcsicmp(szClassName, TEXT("Edit")))
{
bResult = TRUE;
}
Error:
return bResult;
}
// ***************************************************************************
// Function Name: InitDialog
//
// Purpose: UI Initialization
//
// Arguments:
// HWND hwndDlg - Handle to the main dialog
//
// Return Values:
// TRUE if initialization was successful
// FALSE if initialization failed
//
// Description:
// The purpose of this function is to create a full-screen dialog box with
// the Cancel button and creates a menu bar at the bottom of the dialog.
BOOL InitDialog(HWND hwndDlg)
{
SHINITDLGINFO shidi;
SHMENUBARINFO shmbi;
BOOL bSuccess = FALSE;
// Specify that the dialog box should stretch full screen
ZeroMemory(&shidi, sizeof(shidi));
shidi.dwMask = SHIDIM_FLAGS;
shidi.hDlg = hwndDlg;
shidi.dwFlags = SHIDIF_SIZEDLGFULLSCREEN;
// Adds the Cancel button to close the dialog with IDCANCEL
// if the current platform is not a Smartphone platform
if (!g_bSmartphone)
{
shidi.dwFlags |= SHIDIF_CANCELBUTTON;
}
if (SHInitDialog(&shidi))
{
// Set up the menu bar
ZeroMemory(&shmbi, sizeof(shmbi));
shmbi.cbSize = sizeof(shmbi);
shmbi.hwndParent = hwndDlg;
shmbi.nToolBarId = IDR_MENUBAR;
shmbi.hInstRes = g_hInstance;
if (SHCreateMenuBar(&shmbi))
{
if (NULL != shmbi.hwndMB)
{
// Get the main menu and store it
g_hMainMenu = (HMENU)SendMessage(shmbi.hwndMB, SHCMBM_GETSUBMENU, 0, IDM_MENU);
if (NULL != g_hMainMenu)
{
// Set the window caption
SetWindowText(hwndDlg, g_szCaption);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -