📄 cecamera.cpp
字号:
// 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);
// Limits the amount of text the user can enter into the edit controls
SendDlgItemMessage(hwndDlg, IDC_INITIAL_DIR, EM_LIMITTEXT, MAX_INITIAL_DIR, 0);
SendDlgItemMessage(hwndDlg, IDC_DEFAULT_FILE_NAME, EM_LIMITTEXT, MAX_FILE_NAME, 0);
SendDlgItemMessage(hwndDlg, IDC_TITLE, EM_LIMITTEXT, MAX_TITLE, 0);
SendDlgItemMessage(hwndDlg, IDC_RESOLUTION_WIDTH, EM_LIMITTEXT, MAX_RESOLUTION_WIDTH, 0);
SendDlgItemMessage(hwndDlg, IDC_RESOLUTION_HEIGHT, EM_LIMITTEXT, MAX_RESOLUTION_HEIGHT, 0);
SendDlgItemMessage(hwndDlg, IDC_VIDEO_TIME_LIMIT, EM_LIMITTEXT, MAX_VIDEO_TIME_LIMIT, 0);
// Specify all options as default value
ResetOptions(hwndDlg);
// In order to make Back work properly, it's necessary to
// override it and then call the appropriate SH API
if (g_bSmartphone)
{
SendMessage(shmbi.hwndMB, SHCMBM_OVERRIDEKEY, VK_TBACK,
MAKELPARAM(SHMBOF_NODEFAULT | SHMBOF_NOTIFY,
SHMBOF_NODEFAULT | SHMBOF_NOTIFY));
}
bSuccess = TRUE;
}
}
}
}
return bSuccess;
}
// ***************************************************************************
// Function Name: StartCamera
//
// Purpose: Launches Camera using Camera API with specified arguments
//
// Arguments:
// HWND hwndDlg - Handle to the main dialog
//
// Return Values: none
//
// Description:
// Calling SHCameraCapture() function to start Camera for
// taking the pictures or videos.
VOID StartCamera(HWND hwndDlg)
{
HRESULT hr;
HRESULT hReturn;
SHCAMERACAPTURE shcc;
LONG lCheckStateInitialDir;
LONG lCheckStateDefaultFileName;
LONG lCheckStateTitle;
LONG lCheckStateResolution;
LONG lCheckStateVideoTimeLimit;
TCHAR szInitialDir[MAX_INITIAL_DIR] = { 0 };
TCHAR szDefaultFileName[MAX_FILE_NAME] = { 0 };
TCHAR szTitle[MAX_TITLE] = { 0 };
DWORD dwResolutionWidth;
DWORD dwResolutionHeight;
DWORD dwVideoTimeLimit;
LPCTSTR szFormat;
TCHAR szMessage[MAX_MESSAGE] = { 0 };
// Get the state of the checkboxs
lCheckStateInitialDir = SendDlgItemMessage(hwndDlg, IDC_CHECK_INITIAL_DIR, BM_GETCHECK, 0, 0);
lCheckStateDefaultFileName = SendDlgItemMessage(hwndDlg, IDC_CHECK_DEFAULT_FILE_NAME, BM_GETCHECK, 0, 0);
lCheckStateTitle = SendDlgItemMessage(hwndDlg, IDC_CHECK_TITLE, BM_GETCHECK, 0, 0);
lCheckStateResolution = SendDlgItemMessage(hwndDlg, IDC_CHECK_RESOLUTION, BM_GETCHECK, 0, 0);
lCheckStateVideoTimeLimit = SendDlgItemMessage(hwndDlg, IDC_CHECK_VIDEO_TIME_LIMIT, BM_GETCHECK, 0, 0);
// Get the user inputs of the edit controls
GetDlgItemText(hwndDlg, IDC_INITIAL_DIR, szInitialDir, ARRAYSIZE(szInitialDir));
GetDlgItemText(hwndDlg, IDC_DEFAULT_FILE_NAME, szDefaultFileName, ARRAYSIZE(szDefaultFileName));
GetDlgItemText(hwndDlg, IDC_TITLE, szTitle, ARRAYSIZE(szTitle));
dwResolutionWidth = GetDlgItemInt(hwndDlg, IDC_RESOLUTION_WIDTH, NULL, FALSE);
dwResolutionHeight = GetDlgItemInt(hwndDlg, IDC_RESOLUTION_HEIGHT, NULL, FALSE);
dwVideoTimeLimit = GetDlgItemInt(hwndDlg, IDC_VIDEO_TIME_LIMIT, NULL, FALSE);
// Specify the arguments of SHCAMERACAPTURE
ZeroMemory(&shcc, sizeof(shcc));
shcc.cbSize = sizeof(shcc);
shcc.hwndOwner = hwndDlg;
shcc.pszInitialDir = (BST_UNCHECKED == lCheckStateInitialDir) ? CECAMERA_DEFAULT_INITIAL_DIR : szInitialDir;
shcc.pszDefaultFileName = (BST_UNCHECKED == lCheckStateDefaultFileName) ? CECAMERA_DEFAULT_FILE_NAME : szDefaultFileName;
shcc.pszTitle = (BST_UNCHECKED == lCheckStateTitle) ? CECAMERA_DEFAULT_TITLE : szTitle;
shcc.StillQuality = g_StillQuality;
shcc.VideoTypes = g_VideoTypes;
shcc.nResolutionWidth = (BST_UNCHECKED == lCheckStateResolution) ? CECAMERA_DEFAULT_RESOLUTION_WIDTH : dwResolutionWidth;
shcc.nResolutionHeight = (BST_UNCHECKED == lCheckStateResolution) ? CECAMERA_DEFAULT_RESOLUTION_HEIGHT : dwResolutionHeight;
shcc.nVideoTimeLimit = (BST_UNCHECKED == lCheckStateVideoTimeLimit) ? CECAMERA_DEFAULT_VIDEO_TIME_LIMIT : dwVideoTimeLimit;
shcc.Mode = g_Mode;
// Call SHCameraCapture() function
g_bCameraRunning = TRUE;
hReturn = SHCameraCapture(&shcc);
g_bCameraRunning = FALSE;
// Check the return codes of the SHCameraCapture() function
switch (hReturn)
{
case S_OK:
// The method completed successfully.
szFormat = (LPCTSTR)LoadString(g_hInstance, IDS_NOERROR, NULL, 0);
CPR(szFormat);
CHR(StringCchPrintf(szMessage, ARRAYSIZE(szMessage), szFormat, shcc.szFile));
MessageBox(hwndDlg, szMessage, g_szCaption, MB_OK | MB_ICONINFORMATION);
break;
case S_FALSE:
// The user canceled the Camera Capture dialog box.
break;
case E_INVALIDARG:
// An invalid argument was specified.
szFormat = (LPCTSTR)LoadString(g_hInstance, IDS_ERROR_INVALIDARG, NULL, 0);
CPR(szFormat);
MessageBox(hwndDlg, szFormat, g_szCaption, MB_OK | MB_ICONEXCLAMATION);
break;
case E_OUTOFMEMORY:
// There is not enough memory to save the image or video.
szFormat = (LPCTSTR)LoadString(g_hInstance, IDS_ERROR_OUTOFMEMORY, NULL, 0);
CPR(szFormat);
MessageBox(hwndDlg, szFormat, g_szCaption, MB_OK | MB_ICONSTOP);
break;
case HRESULT_FROM_WIN32(ERROR_RESOURCE_DISABLED):
// The camera is disabled.
szFormat = (LPCTSTR)LoadString(g_hInstance, IDS_ERROR_CAMERADISABLED, NULL, 0);
CPR(szFormat);
MessageBox(hwndDlg, szFormat, g_szCaption, MB_OK | MB_ICONSTOP);
break;
default:
// An unknown error occurred.
szFormat = (LPCTSTR)LoadString(g_hInstance, IDS_ERROR_UNKNOWN, NULL, 0);
CPR(szFormat);
CHR(StringCchPrintf(szMessage, ARRAYSIZE(szMessage), szFormat, hReturn));
MessageBox(hwndDlg, szMessage, g_szCaption, MB_OK | MB_ICONSTOP);
break;
}
Error:
return;
}
// ***************************************************************************
// Function Name: ShowAboutBox
//
// Purpose: Show program information
//
// Arguments:
// HWND hwndDlg - Handle to the main dialog
//
// Return Values: none
//
// Description:
// Display a message box for program information.
VOID ShowAboutBox(HWND hwndDlg)
{
HRESULT hr;
LPCTSTR szAbout;
// Load the about string from resource and display it
szAbout = (LPCTSTR)LoadString(g_hInstance, IDS_ABOUT, NULL, 0);
CPR(szAbout);
MessageBox(hwndDlg, szAbout, g_szCaption, MB_OK);
Error:
return;
}
// ***************************************************************************
// Function Name: ResetOptions
//
// Purpose: Resets all settings as the default value
//
// Arguments:
// HWND hwndDlg - Handle to the main dialog
//
// Return Values: none
//
// Description:
// Resets all settings as the default value and updates UI.
VOID ResetOptions(HWND hwndDlg)
{
HRESULT hr;
// Uncheck all checkboxs to specify the options using the default value
SendDlgItemMessage(hwndDlg, IDC_CHECK_INITIAL_DIR, BM_SETCHECK, BST_UNCHECKED, 0);
SendDlgItemMessage(hwndDlg, IDC_CHECK_DEFAULT_FILE_NAME, BM_SETCHECK, BST_UNCHECKED, 0);
SendDlgItemMessage(hwndDlg, IDC_CHECK_TITLE, BM_SETCHECK, BST_UNCHECKED, 0);
SendDlgItemMessage(hwndDlg, IDC_CHECK_RESOLUTION, BM_SETCHECK, BST_UNCHECKED, 0);
SendDlgItemMessage(hwndDlg, IDC_CHECK_VIDEO_TIME_LIMIT, BM_SETCHECK, BST_UNCHECKED, 0);
// Set the default value for the options
CBR(SetDlgItemText(hwndDlg, IDC_INITIAL_DIR, CECAMERA_DEFAULT_INITIAL_DIR));
CBR(SetDlgItemText(hwndDlg, IDC_DEFAULT_FILE_NAME, CECAMERA_DEFAULT_FILE_NAME));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -