📄 cecamera.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
// ***************************************************************************
// CECamera.cpp
//
// Take pictures and videos using Camera native API.
//
#include <aygshell.h>
#include <windowsx.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")
#define CECAMERA_CAMERAVIEW_CLASSNAME TEXT("Camera View")
#define WM_ACTIVATE_CAMERAVIEW WM_USER + 1
// Global variables
BOOL g_bSmartphone = FALSE;
HINSTANCE g_hInstance = NULL;
HMENU g_hMainMenu = NULL;
BOOL g_bCameraRunning = FALSE;
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);
BOOL IsOwned(HWND hwndOwner, HWND hwnd);
BOOL CALLBACK EnumLastActiveWindowProc(HWND hwnd, LPARAM lParam);
HWND GetLastActiveWindow(HWND hwndOwner);
// ***************************************************************************
// 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 hwndPrevInstance;
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
hwndPrevInstance = FindWindow(CECAMERA_MAINDLG_CLASSNAME, g_szCaption);
CPR(hwndPrevInstance);
SetForegroundWindow(GetLastActiveWindow(hwndPrevInstance));
}
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) && g_bCameraRunning)
{
PostMessage(hwndDlg, WM_ACTIVATE_CAMERAVIEW, 0, 0);
}
break;
case WM_ACTIVATE_CAMERAVIEW:
if (g_bCameraRunning)
{
// Reactivate the camera window if camera window is running
HWND hwndCameraView = FindWindow(CECAMERA_CAMERAVIEW_CLASSNAME, NULL);
if (NULL != hwndCameraView)
{
// Bring the last active window owned by camera window to the foreground
SetForegroundWindow(GetLastActiveWindow(hwndCameraView));
}
}
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
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -