شما اگه میخواین plug-in برای pocket PC بنویسین یا به منابع سیستمی که low level هستند دسترسی پیدا کنید و برایشان برنامه بنویسید تنها راه unmanaged code است (مثل طراحی برنامه برای صفحه ی Home یا Today)
اما برنمه هایی مثل ارسال sms یا ار با Camera را بهتر است با Net. بنویسید در زیر برنامه ی CeCamera به دو زبان C# و C++ آمده است:
کد:C++ :
//
// 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);
// 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
hReturn = SHCameraCapture(&shcc);
// 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;
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));
CBR(SetDlgItemText(hwndDlg, IDC_TITLE, CECAMERA_DEFAULT_TITLE));
CBR(SetDlgItemInt(hwndDlg, IDC_RESOLUTION_WIDTH, CECAMERA_DEFAULT_RESOLUTION_WIDTH, 0));
CBR(SetDlgItemInt(hwndDlg, IDC_RESOLUTION_HEIGHT, CECAMERA_DEFAULT_RESOLUTION_HEIGHT, 0));
CBR(SetDlgItemInt(hwndDlg, IDC_VIDEO_TIME_LIMIT, CECAMERA_DEFAULT_VIDEO_TIME_LIMIT, 0));
// Disable the edit controls since specify using default settings
EnableWindow(GetDlgItem(hwndDlg, IDC_INITIAL_DIR), FALSE);
EnableWindow(GetDlgItem(hwndDlg, IDC_DEFAULT_FILE_NAME), FALSE);
EnableWindow(GetDlgItem(hwndDlg, IDC_TITLE), FALSE);
EnableWindow(GetDlgItem(hwndDlg, IDC_RESOLUTION_WIDTH), FALSE);
EnableWindow(GetDlgItem(hwndDlg, IDC_RESOLUTION_HEIGHT), FALSE);
EnableWindow(GetDlgItem(hwndDlg, IDC_VIDEO_TIME_LIMIT), FALSE);
// Checks the menu item and makes it a radio item
CBR(CheckMenuRadioItem(GetSubMenu(g_hMainMenu, 0), IDM_MODE_STILL, IDM_MODE_VIDEOWITHAUDIO, IDM_MODE_STILL, MF_BYCOMMAND));
CBR(CheckMenuRadioItem(GetSubMenu(g_hMainMenu, 1), IDM_STILLQUALITY_DEFAULT, IDM_STILLQUALITY_HIGH, IDM_STILLQUALITY_DEFAULT, MF_BYCOMMAND));
CBR(CheckMenuRadioItem(GetSubMenu(g_hMainMenu, 2), IDM_VIDEOTYPES_ALL, IDM_VIDEOTYPES_MESSAGING, IDM_VIDEOTYPES_ALL, MF_BYCOMMAND));
// Set the global variables as default value
g_StillQuality = CECAMERA_DEFAULT_STILL_QUALITY;
g_VideoTypes = CECAMERA_DEFAULT_VIDEO_TYPES;
g_Mode = CECAMERA_DEFAULT_MODE;
Error:
return;
}
// ***************************************************************************
// Function Name: ChangeMode
//
// Purpose: Changes "Mode" parameter of SHCameraCapture()
//
// Arguments:
// WORD wMode - The identifier of the menu item
//
// Return Values: none
//
// Description:
// Changes "Mode" parameter of SHCameraCapture(),
// also checks the specified menu item of "Mode".
VOID ChangeMode(WORD wMode)
{
HRESULT hr;
// Update the menu item "Mode"
CBR(CheckMenuRadioItem(GetSubMenu(g_hMainMenu, 0), IDM_MODE_STILL, IDM_MODE_VIDEOWITHAUDIO, wMode, MF_BYCOMMAND));
// Set the global variables as user specified value
switch (wMode)
{
case IDM_MODE_STILL:
g_Mode = CAMERACAPTURE_MODE_STILL;
break;
case IDM_MODE_VIDEOONLY:
g_Mode = CAMERACAPTURE_MODE_VIDEOONLY;
break;
case IDM_MODE_VIDEOWITHAUDIO:
g_Mode = CAMERACAPTURE_MODE_VIDEOWITHAUDIO;
break;
default:
CBR(FALSE);
break;
}
Error:
return;
}
// ***************************************************************************
// Function Name: ChangeStillQuality
//
// Purpose: Changes "StillQuality" parameter of SHCameraCapture()
//
// Arguments:
// WORD wStillQuality - The identifier of the menu item
//
// Return Values: none
//
// Description:
// Changes "StillQuality" parameter of SHCameraCapture(),
// also checks the specified menu item of "StillQuality".
VOID ChangeStillQuality(WORD wStillQuality)
{
HRESULT hr;
// Update the menu item "StillQuality"
CBR(CheckMenuRadioItem(GetSubMenu(g_hMainMenu, 1), IDM_STILLQUALITY_DEFAULT, IDM_STILLQUALITY_HIGH, wStillQuality, MF_BYCOMMAND));
// Set the global variables as user specified value
switch (wStillQuality)
{
case IDM_STILLQUALITY_DEFAULT:
g_StillQuality = CAMERACAPTURE_STILLQUALITY_DEFAULT;
break;
case IDM_STILLQUALITY_LOW:
g_StillQuality = CAMERACAPTURE_STILLQUALITY_LOW;
break;
case IDM_STILLQUALITY_NORMAL:
g_StillQuality = CAMERACAPTURE_STILLQUALITY_NORMAL;
break;
case IDM_STILLQUALITY_HIGH:
g_StillQuality = CAMERACAPTURE_STILLQUALITY_HIGH;
break;
default:
CBR(FALSE);
break;
}
Error:
return;
}
// ***************************************************************************
// Function Name: ChangeVideoTypes
//
// Purpose: Changes "VideoTypes" parameter of SHCameraCapture()
//
// Arguments:
// WORD wVideoTypes - The identifier of the menu item
//
// Return Values: none
//
// Description:
// Changes "VideoTypes" parameter of SHCameraCapture(),
// also checks the specified menu item of "VideoTypes".
VOID ChangeVideoTypes(WORD wVideoTypes)
{
HRESULT hr;
// Update the menu item "VideoTypes"
CBR(CheckMenuRadioItem(GetSubMenu(g_hMainMenu, 2), IDM_VIDEOTYPES_ALL, IDM_VIDEOTYPES_MESSAGING, wVideoTypes, MF_BYCOMMAND));
// Set the global variables as user specified value
switch (wVideoTypes)
{
case IDM_VIDEOTYPES_ALL:
g_VideoTypes = CAMERACAPTURE_VIDEOTYPE_ALL;
break;
case IDM_VIDEOTYPES_STANDARD:
g_VideoTypes = CAMERACAPTURE_VIDEOTYPE_STANDARD;
break;
case IDM_VIDEOTYPES_MESSAGING:
g_VideoTypes = CAMERACAPTURE_VIDEOTYPE_MESSAGING;
break;
default:
CBR(FALSE);
break;
}
Error:
return;
}
// ***************************************************************************
// Function Name: ChangeOptions
//
// Purpose: Enable/Disable user input of the edit controls
//
// Arguments:
// HWND hwndDlg - Handle to the main dialog
// WORD wOptions - The identifier of the control
//
// Return Values: none
//
// Description:
// Enable/Disable user input of the edit controls for allowing/disallowing
// the user to specify the settings of the Camera API.
VOID ChangeOptions(HWND hwndDlg, WORD wOptions)
{
HRESULT hr;
LONG lCheckState;
BOOL bEnable;
// Get the check state of the check box
lCheckState = SendDlgItemMessage(hwndDlg, wOptions, BM_GETCHECK, 0, 0);
bEnable = (BST_UNCHECKED == lCheckState) ? FALSE : TRUE;
// Enable/Disable user input of the edit controls
switch (wOptions)
{
case IDC_CHECK_INITIAL_DIR:
EnableWindow(GetDlgItem(hwndDlg, IDC_INITIAL_DIR), bEnable);
break;
case IDC_CHECK_DEFAULT_FILE_NAME:
EnableWindow(GetDlgItem(hwndDlg, IDC_DEFAULT_FILE_NAME), bEnable);
break;
case IDC_CHECK_TITLE:
EnableWindow(GetDlgItem(hwndDlg, IDC_TITLE), bEnable);
break;
case IDC_CHECK_RESOLUTION:
EnableWindow(GetDlgItem(hwndDlg, IDC_RESOLUTION_WIDTH), bEnable);
EnableWindow(GetDlgItem(hwndDlg, IDC_RESOLUTION_HEIGHT), bEnable);
break;
case IDC_CHECK_VIDEO_TIME_LIMIT:
EnableWindow(GetDlgItem(hwndDlg, IDC_VIDEO_TIME_LIMIT), bEnable);
break;
default:
CBR(FALSE);
break;
}
Error:
return;
}
کد:
C#:
//
// 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.cs
//
// Take pictures and videos using Camera managed API.
//
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Globalization;
using Microsoft.WindowsMobile.Forms;
using System.IO;
namespace Microsoft.WindowsMobile.Samples.CECamera
{
/// <summary>
/// Main form for the CECamera application.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
// Default values for options
private const string cecameraDefaultInitialDirectory = null;
private const string cecameraDefaultPictureFileName = "picture1";
private const string cecameraDefaultPictureExtension = ".jpg";
private const string cecameraDefaultVideoFileName = "movie1";
private const string cecameraDefaultTitle = "Title";
private const string cecameraDefaultResolutionWidth = "100";
private const string cecameraDefaultResolutionHeight = "100";
private const string cecameraDefaultVideoTimeLimit = "25";
private System.Windows.Forms.MainMenu mainMenu;
private System.Windows.Forms.MenuItem menuStart;
private System.Windows.Forms.MenuItem menuMenu;
private System.Windows.Forms.MenuItem menuMode;
private System.Windows.Forms.MenuItem menuModeStill;
private System.Windows.Forms.MenuItem menuModeVideoOnly;
private System.Windows.Forms.MenuItem menuModeVideoWithAudio;
private System.Windows.Forms.MenuItem menuStillQuality;
private System.Windows.Forms.MenuItem menuStillQualityDefault;
private System.Windows.Forms.MenuItem menuStillQualityLow;
private System.Windows.Forms.MenuItem menuStillQualityNormal;
private System.Windows.Forms.MenuItem menuStillQualityHigh;
private System.Windows.Forms.MenuItem menuVideoTypes;
private System.Windows.Forms.MenuItem menuVideoTypesAll;
private System.Windows.Forms.MenuItem menuVideoTypesStandard;
private System.Windows.Forms.MenuItem menuVideoTypesMessaging;
private System.Windows.Forms.MenuItem menuSeparator1;
private System.Windows.Forms.MenuItem menuReset;
private System.Windows.Forms.MenuItem menuAbout;
private System.Windows.Forms.MenuItem menuSeparator2;
private System.Windows.Forms.MenuItem menuExit;
private System.Windows.Forms.CheckBox checkInitialDirectory;
private System.Windows.Forms.TextBox textInitialDirectory;
private System.Windows.Forms.CheckBox checkDefaultFileName;
private System.Windows.Forms.TextBox textDefaultFileName;
private System.Windows.Forms.CheckBox checkTitle;
private System.Windows.Forms.TextBox textTitle;
private System.Windows.Forms.CheckBox checkResolution;
private System.Windows.Forms.TextBox textResolutionWidth;
private System.Windows.Forms.Label labelX;
private System.Windows.Forms.TextBox textResolutionHeight;
private System.Windows.Forms.CheckBox checkVideoTimeLimit;
private System.Windows.Forms.TextBox textVideoTimeLimit;
/// <summary>
/// Constructor
/// </summary>
public MainForm()
{
//
// Required for Windows Form Designer support
//
this.InitializeComponent();
//
// Specify all options as default value
//
this.ResetOptions();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu = new System.Windows.Forms.MainMenu();
this.menuStart = new System.Windows.Forms.MenuItem();
this.menuMenu = new System.Windows.Forms.MenuItem();
this.menuMode = new System.Windows.Forms.MenuItem();
this.menuModeStill = new System.Windows.Forms.MenuItem();
this.menuModeVideoOnly = new System.Windows.Forms.MenuItem();
this.menuModeVideoWithAudio = new System.Windows.Forms.MenuItem();
this.menuStillQuality = new System.Windows.Forms.MenuItem();
this.menuStillQualityDefault = new System.Windows.Forms.MenuItem();
this.menuStillQualityLow = new System.Windows.Forms.MenuItem();
this.menuStillQualityNormal = new System.Windows.Forms.MenuItem();
this.menuStillQualityHigh = new System.Windows.Forms.MenuItem();
this.menuVideoTypes = new System.Windows.Forms.MenuItem();
this.menuVideoTypesAll = new System.Windows.Forms.MenuItem();
this.menuVideoTypesStandard = new System.Windows.Forms.MenuItem();
this.menuVideoTypesMessaging = new System.Windows.Forms.MenuItem();
this.menuSeparator1 = new System.Windows.Forms.MenuItem();
this.menuReset = new System.Windows.Forms.MenuItem();
this.menuAbout = new System.Windows.Forms.MenuItem();
this.menuSeparator2 = new System.Windows.Forms.MenuItem();
this.menuExit = new System.Windows.Forms.MenuItem();
this.checkInitialDirectory = new System.Windows.Forms.CheckBox();
this.textInitialDirectory = new System.Windows.Forms.TextBox();
this.checkDefaultFileName = new System.Windows.Forms.CheckBox();
this.textDefaultFileName = new System.Windows.Forms.TextBox();
this.checkTitle = new System.Windows.Forms.CheckBox();
this.textTitle = new System.Windows.Forms.TextBox();
this.checkResolution = new System.Windows.Forms.CheckBox();
this.textResolutionWidth = new System.Windows.Forms.TextBox();
this.labelX = new System.Windows.Forms.Label();
this.textResolutionHeight = new System.Windows.Forms.TextBox();
this.checkVideoTimeLimit = new System.Windows.Forms.CheckBox();
this.textVideoTimeLimit = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// mainMenu
//
this.mainMenu.MenuItems.Add(this.menuStart);
this.mainMenu.MenuItems.Add(this.menuMenu);
//
// menuStart
//
this.menuStart.Text = "Start";
this.menuStart.Click += new System.EventHandler(this.menuStart_Click);
//
// menuMenu
//
this.menuMenu.MenuItems.Add(this.menuMode);
this.menuMenu.MenuItems.Add(this.menuStillQuality);
this.menuMenu.MenuItems.Add(this.menuVideoTypes);
this.menuMenu.MenuItems.Add(this.menuSeparator1);
this.menuMenu.MenuItems.Add(this.menuReset);
this.menuMenu.MenuItems.Add(this.menuAbout);
this.menuMenu.MenuItems.Add(this.menuSeparator2);
this.menuMenu.MenuItems.Add(this.menuExit);
this.menuMenu.Text = "Menu";
//
// menuMode
//
this.menuMode.MenuItems.Add(this.menuModeStill);
this.menuMode.MenuItems.Add(this.menuModeVideoOnly);
this.menuMode.MenuItems.Add(this.menuModeVideoWithAudio);
this.menuMode.Text = "Mode";
//
// menuModeStill
//
this.menuModeStill.Text = "Still";
this.menuModeStill.Click += new System.EventHandler(this.menuModeStill_Click);
//
// menuModeVideoOnly
//
this.menuModeVideoOnly.Text = "Video Only";
this.menuModeVideoOnly.Click += new System.EventHandler(this.menuModeVideoOnly_Click);
//
// menuModeVideoWithAudio
//
this.menuModeVideoWithAudio.Text = "Video with Audio";
this.menuModeVideoWithAudio.Click += new System.EventHandler(this.menuModeVideoWithAudio_Click);
//
// menuStillQuality
//
this.menuStillQuality.MenuItems.Add(this.menuStillQualityDefault);
this.menuStillQuality.MenuItems.Add(this.menuStillQualityLow);
this.menuStillQuality.MenuItems.Add(this.menuStillQualityNormal);
this.menuStillQuality.MenuItems.Add(this.menuStillQualityHigh);
this.menuStillQuality.Text = "Still Quality";
//
// menuStillQualityDefault
//
this.menuStillQualityDefault.Text = "Default";
this.menuStillQualityDefault.Click += new System.EventHandler(this.menuStillQualityDefault_Click);
//
// menuStillQualityLow
//
this.menuStillQualityLow.Text = "Low";
this.menuStillQualityLow.Click += new System.EventHandler(this.menuStillQualityLow_Click);
//
// menuStillQualityNormal
//
this.menuStillQualityNormal.Text = "Normal";
this.menuStillQualityNormal.Click += new System.EventHandler(this.menuStillQualityNormal_Click);
//
// menuStillQualityHigh
//
this.menuStillQualityHigh.Text = "High";
this.menuStillQualityHigh.Click += new System.EventHandler(this.menuStillQualityHigh_Click);
//
// menuVideoTypes
//
this.menuVideoTypes.MenuItems.Add(this.menuVideoTypesAll);
this.menuVideoTypes.MenuItems.Add(this.menuVideoTypesStandard);
this.menuVideoTypes.MenuItems.Add(this.menuVideoTypesMessaging);
this.menuVideoTypes.Text = "Video Types";
//
// menuVideoTypesAll
//
this.menuVideoTypesAll.Text = "All";
this.menuVideoTypesAll.Click += new System.EventHandler(this.menuVideoTypesAll_Click);
//
// menuVideoTypesStandard
//
this.menuVideoTypesStandard.Text = "Standard";
this.menuVideoTypesStandard.Click += new System.EventHandler(this.menuVideoTypesStandard_Click);
//
// menuVideoTypesMessaging
//
this.menuVideoTypesMessaging.Text = "Messaging";
this.menuVideoTypesMessaging.Click += new System.EventHandler(this.menuVideoTypesMessaging_Click);
//
// menuSeparator1
//
this.menuSeparator1.Text = "-";
//
// menuReset
//
this.menuReset.Text = "Reset";
this.menuReset.Click += new System.EventHandler(this.menuReset_Click);
//
// menuAbout
//
this.menuAbout.Text = "About";
this.menuAbout.Click += new System.EventHandler(this.menuAbout_Click);
//
// menuSeparator2
//
this.menuSeparator2.Text = "-";
//
// menuExit
//
this.menuExit.Text = "Exit";
this.menuExit.Click += new System.EventHandler(this.menuExit_Click);
//
// checkInitialDirectory
//
this.checkInitialDirectory.Location = new System.Drawing.Point(5, 5);
this.checkInitialDirectory.Name = "checkInitialDirectory";
this.checkInitialDirectory.Size = new System.Drawing.Size(154, 16);
this.checkInitialDirectory.TabIndex = 0;
this.checkInitialDirectory.Text = "Initial Directory:";
this.checkInitialDirectory.CheckStateChanged += new System.EventHandler(this.checkInitialDirectory_CheckStateChanged);
//
// textInitialDirectory
//
this.textInitialDirectory.Location = new System.Drawing.Point(5, 25);
this.textInitialDirectory.MaxLength = 260;
this.textInitialDirectory.Name = "textInitialDirectory";
this.textInitialDirectory.Size = new System.Drawing.Size(141, 22);
this.textInitialDirectory.TabIndex = 1;
//
// checkDefaultFileName
//
this.checkDefaultFileName.Location = new System.Drawing.Point(5, 53);
this.checkDefaultFileName.Name = "checkDefaultFileName";
this.checkDefaultFileName.Size = new System.Drawing.Size(141, 16);
this.checkDefaultFileName.TabIndex = 2;
this.checkDefaultFileName.Text = "Default File Name:";
this.checkDefaultFileName.CheckStateChanged += new System.EventHandler(this.checkDefaultFileName_CheckStateChanged);
//
// textDefaultFileName
//
this.textDefaultFileName.Location = new System.Drawing.Point(5, 75);
this.textDefaultFileName.MaxLength = 260;
this.textDefaultFileName.Name = "textDefaultFileName";
this.textDefaultFileName.Size = new System.Drawing.Size(141, 22);
this.textDefaultFileName.TabIndex = 3;
//
// checkTitle
//
this.checkTitle.Location = new System.Drawing.Point(5, 102);
this.checkTitle.Name = "checkTitle";
this.checkTitle.Size = new System.Drawing.Size(108, 23);
this.checkTitle.TabIndex = 4;
this.checkTitle.Text = "Title:";
this.checkTitle.CheckStateChanged += new System.EventHandler(this.checkTitle_CheckStateChanged);
//
// textTitle
//
this.textTitle.Location = new System.Drawing.Point(5, 131);
this.textTitle.MaxLength = 64;
this.textTitle.Name = "textTitle";
this.textTitle.Size = new System.Drawing.Size(141, 22);
this.textTitle.TabIndex = 5;
//
// checkResolution
//
this.checkResolution.Location = new System.Drawing.Point(5, 158);
this.checkResolution.Name = "checkResolution";
this.checkResolution.Size = new System.Drawing.Size(154, 16);
this.checkResolution.TabIndex = 6;
this.checkResolution.Text = "Resolution (Width x Height):";
this.checkResolution.CheckStateChanged += new System.EventHandler(this.checkResolution_CheckStateChanged);
//
// textResolutionWidth
//
this.textResolutionWidth.Location = new System.Drawing.Point(5, 180);
this.textResolutionWidth.MaxLength = 10;
this.textResolutionWidth.Name = "textResolutionWidth";
this.textResolutionWidth.Size = new System.Drawing.Size(52, 22);
this.textResolutionWidth.TabIndex = 7;
//
// labelX
//
this.labelX.Location = new System.Drawing.Point(63, 180);
this.labelX.Name = "labelX";
this.labelX.Size = new System.Drawing.Size(24, 16);
this.labelX.Text = "x";
this.labelX.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// textResolutionHeight
//
this.textResolutionHeight.Location = new System.Drawing.Point(93, 180);
this.textResolutionHeight.MaxLength = 10;
this.textResolutionHeight.Name = "textResolutionHeight";
this.textResolutionHeight.Size = new System.Drawing.Size(53, 22);
this.textResolutionHeight.TabIndex = 9;
//
// checkVideoTimeLimit
//
this.checkVideoTimeLimit.Location = new System.Drawing.Point(5, 207);
this.checkVideoTimeLimit.Name = "checkVideoTimeLimit";
this.checkVideoTimeLimit.Size = new System.Drawing.Size(115, 19);
this.checkVideoTimeLimit.TabIndex = 10;
this.checkVideoTimeLimit.Text = "Video Time Limit:";
this.checkVideoTimeLimit.CheckStateChanged += new System.EventHandler(this.checkVideoTimeLimit_CheckStateChanged);
//
// textVideoTimeLimit
//
this.textVideoTimeLimit.Location = new System.Drawing.Point(5, 232);
this.textVideoTimeLimit.MaxLength = 10;
this.textVideoTimeLimit.Name = "textVideoTimeLimit";
this.textVideoTimeLimit.Size = new System.Drawing.Size(154, 22);
this.textVideoTimeLimit.TabIndex = 11;
//
// MainForm
//
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(240, 268);
this.Controls.Add(this.checkInitialDirectory);
this.Controls.Add(this.textInitialDirectory);
this.Controls.Add(this.checkDefaultFileName);
this.Controls.Add(this.textDefaultFileName);
this.Controls.Add(this.checkTitle);
this.Controls.Add(this.textTitle);
this.Controls.Add(this.checkResolution);
this.Controls.Add(this.textResolutionWidth);
this.Controls.Add(this.labelX);
this.Controls.Add(this.textResolutionHeight);
this.Controls.Add(this.checkVideoTimeLimit);
this.Controls.Add(this.textVideoTimeLimit);
this.Menu = this.mainMenu;
this.Name = "MainForm";
this.Text = "CECamera";
this.Closed += new System.EventHandler(this.MainForm_Closed);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new MainForm());
}
/// <summary>
/// Exit the application.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void MainForm_Closed(object sender, System.EventArgs e)
{
Application.Exit();
}
/// <summary>
/// Enable/Disable user input of the textInitialDirectory edit control
/// for allowing/disallowing the user to specify the initial directory
/// of the CameraCaptureDialog.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void checkInitialDirectory_CheckStateChanged(object sender, System.EventArgs e)
{
this.textInitialDirectory.Enabled = this.checkInitialDirectory.Checked;
}
/// <summary>
/// Enable/Disable user input of the textDefaultFileName edit control
/// for allowing/disallowing the user to specify the default file name
/// of the CameraCaptureDialog.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void checkDefaultFileName_CheckStateChanged(object sender, System.EventArgs e)
{
this.textDefaultFileName.Enabled = this.checkDefaultFileName.Checked;
validateFileNameInput();
}
/// <summary>
/// Enable/Disable user input of the textTitle edit control
/// for allowing/disallowing the user to specify the title
/// of the CameraCaptureDialog.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void checkTitle_CheckStateChanged(object sender, System.EventArgs e)
{
this.textTitle.Enabled = this.checkTitle.Checked;
}
/// <summary>
/// Enable/Disable user input of the textResolutionWidth and textResolutionHeight edit control
/// for allowing/disallowing the user to specify the resolution width and height
/// of the CameraCaptureDialog.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void checkResolution_CheckStateChanged(object sender, System.EventArgs e)
{
this.textResolutionWidth.Enabled = this.checkResolution.Checked;
this.textResolutionHeight.Enabled = this.checkResolution.Checked;
}
/// <summary>
/// Enable/Disable user input of the textVideoTimeLimit edit control
/// for allowing/disallowing the user to specify the video time limit
/// of the CameraCaptureDialog.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void checkVideoTimeLimit_CheckStateChanged(object sender, EventArgs e)
{
this.textVideoTimeLimit.Enabled = this.checkVideoTimeLimit.Checked;
}
/// <summary>
/// Launches Camera using CameraCaptureDialog with specified arguments.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuStart_Click(object sender, System.EventArgs e)
{
CameraCaptureDialog cameraCapture = new CameraCaptureDialog();
cameraCapture.Owner = this;
// Specify the options as user specified.
if (this.checkInitialDirectory.Checked)
{
cameraCapture.InitialDirectory = this.textInitialDirectory.Text;
}
if (this.checkDefaultFileName.Checked)
{
if (this.menuModeStill.Checked)
{
if (cameraCapture.DefaultFileName != null)
{
// It is necessary to end picture files with ".jpg".
// Otherwise the argument is invalid.
cameraCapture.DefaultFileName = cameraCapture.DefaultFileName + cecameraDefaultPictureExtension;
}
}
else
{
// If it is a video, pass null. This will return a filename with a
// correct extension. Later on we rename the file and keep the extension.
cameraCapture.DefaultFileName = null;
}
}
if (this.checkTitle.Checked)
{
cameraCapture.Title = this.textTitle.Text;
}
if (this.checkResolution.Checked)
{
int resolutionWidth = 0;
int resolutionHeight = 0;
if (!ConvertStringToInt(this.textResolutionWidth.Text, ref resolutionWidth))
{
MessageBox.Show("Please input a valid resolution width.", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
textResolutionWidth.Focus();
return;
}
if (!ConvertStringToInt(this.textResolutionHeight.Text, ref resolutionHeight))
{
MessageBox.Show("Please input a valid resolution height.", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
textResolutionHeight.Focus();
return;
}
cameraCapture.Resolution = new Size(resolutionWidth, resolutionHeight);
}
if (this.checkVideoTimeLimit.Checked)
{
int videoTimeLimit = 0;
if (!ConvertStringToInt(this.textVideoTimeLimit.Text, ref videoTimeLimit))
{
MessageBox.Show("Please input a valid video time limit.", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
textVideoTimeLimit.Focus();
return;
}
cameraCapture.VideoTimeLimit = new TimeSpan(0, 0, videoTimeLimit);
}
// Specify capture mode
if (this.menuModeStill.Checked)
{
cameraCapture.Mode = CameraCaptureMode.Still;
}
else if (this.menuModeVideoOnly.Checked)
{
cameraCapture.Mode = CameraCaptureMode.VideoOnly;
}
else if (this.menuModeVideoWithAudio.Checked)
{
cameraCapture.Mode = CameraCaptureMode.VideoWithAudio;
}
// Specify still quality
if (this.menuStillQualityDefault.Checked)
{
cameraCapture.StillQuality = CameraCaptureStillQuality.Default;
}
else if (this.menuStillQualityLow.Checked)
{
cameraCapture.StillQuality = CameraCaptureStillQuality.Low;
}
else if (this.menuStillQualityNormal.Checked)
{
cameraCapture.StillQuality = CameraCaptureStillQuality.Normal;
}
else if (this.menuStillQualityHigh.Checked)
{
cameraCapture.StillQuality = CameraCaptureStillQuality.High;
}
// Specify video types
if (this.menuVideoTypesAll.Checked)
{
cameraCapture.VideoTypes = CameraCaptureVideoTypes.All;
}
else if (this.menuVideoTypesStandard.Checked)
{
cameraCapture.VideoTypes = CameraCaptureVideoTypes.Standard;
}
else if (this.menuVideoTypesMessaging.Checked)
{
cameraCapture.VideoTypes = CameraCaptureVideoTypes.Messaging;
}
try
{
// Displays the "Camera Capture" dialog box
if (DialogResult.OK == cameraCapture.ShowDialog())
{
string fileName = cameraCapture.FileName;
// If it is a video we rename the file so that it has the user entered
// default filename and the correct extension.
if (cameraCapture.Mode != CameraCaptureMode.Still)
{
string extension = fileName.Substring(fileName.LastIndexOf("."));
string directory = "";
if (fileName.LastIndexOf("\\") != -1)
{
directory = fileName.Substring(0, fileName.LastIndexOf("\\") + 1);
}
fileName = directory + this.textDefaultFileName.Text + extension;
System.IO.File.Move(cameraCapture.FileName, fileName);
}
// The method completed successfully.
MessageBox.Show("The picture or video has been successfully captured and saved to:\n\n" + fileName,
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
}
}
catch (ArgumentException ex)
{
// An invalid argument was specified.
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
catch (OutOfMemoryException ex)
{
// There is not enough memory to save the image or video.
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK,
MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}
catch (InvalidOperationException ex)
{
// An unknown error occurred.
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK,
MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}
}
/// <summary>
/// Checks the selected menu item of "Mode".
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuModeStill_Click(object sender, System.EventArgs e)
{
for (int i = 0; i < this.menuMode.MenuItems.Count; i++)
{
this.menuMode.MenuItems[i].Checked = false;
}
this.menuModeStill.Checked = true;
validateFileNameInput();
this.checkResolution.Enabled = true;
this.checkVideoTimeLimit.Checked = false;
this.checkVideoTimeLimit.Enabled = false;
}
/// <summary>
/// Checks the selected menu item of "Mode".
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuModeVideoOnly_Click(object sender, System.EventArgs e)
{
for (int i = 0; i < this.menuMode.MenuItems.Count; i++)
{
this.menuMode.MenuItems[i].Checked = false;
}
this.menuModeVideoOnly.Checked = true;
validateFileNameInput();
this.checkResolution.Checked = false;
this.checkResolution.Enabled = false;
this.checkVideoTimeLimit.Enabled = true;
}
/// <summary>
/// Checks the selected menu item of "Mode".
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuModeVideoWithAudio_Click(object sender, System.EventArgs e)
{
for (int i = 0; i < this.menuMode.MenuItems.Count; i++)
{
this.menuMode.MenuItems[i].Checked = false;
}
this.menuModeVideoWithAudio.Checked = true;
validateFileNameInput();
this.checkResolution.Checked = false;
this.checkResolution.Enabled = false;
this.checkVideoTimeLimit.Enabled = true;
}
/// <summary>
/// Verifies the filename to be consistent with the camera mode.
/// </summary>
private void validateFileNameInput()
{
if (this.menuModeStill.Checked)
{
if (textDefaultFileName.Text == null || textDefaultFileName.Text.Length == 0 ||
textDefaultFileName.Text.Equals(cecameraDefaultVideoFileName))
{
this.textDefaultFileName.Text = cecameraDefaultPictureFileName;
}
}
else
{
if (textDefaultFileName.Text == null || textDefaultFileName.Text.Length == 0 ||
textDefaultFileName.Text.Equals(cecameraDefaultPictureFileName))
{
textDefaultFileName.Text = cecameraDefaultVideoFileName;
}
}
}
/// <summary>
/// Checks the selected menu item of "Still Quality".
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuStillQualityDefault_Click(object sender, System.EventArgs e)
{
for (int i = 0; i < this.menuStillQuality.MenuItems.Count; i++)
{
this.menuStillQuality.MenuItems[i].Checked = false;
}
this.menuStillQualityDefault.Checked = true;
}
/// <summary>
/// Checks the selected menu item of "Still Quality".
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuStillQualityLow_Click(object sender, System.EventArgs e)
{
for (int i = 0; i < this.menuStillQuality.MenuItems.Count; i++)
{
this.menuStillQuality.MenuItems[i].Checked = false;
}
this.menuStillQualityLow.Checked = true;
}
/// <summary>
/// Checks the selected menu item of "Still Quality".
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuStillQualityNormal_Click(object sender, System.EventArgs e)
{
for (int i = 0; i < this.menuStillQuality.MenuItems.Count; i++)
{
this.menuStillQuality.MenuItems[i].Checked = false;
}
this.menuStillQualityNormal.Checked = true;
}
/// <summary>
/// Checks the selected menu item of "Still Quality".
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuStillQualityHigh_Click(object sender, System.EventArgs e)
{
for (int i = 0; i < this.menuStillQuality.MenuItems.Count; i++)
{
this.menuStillQuality.MenuItems[i].Checked = false;
}
this.menuStillQualityHigh.Checked = true;
}
/// <summary>
/// Checks the selected menu item of "Video Types".
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuVideoTypesAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.menuVideoTypes.MenuItems.Count; i++)
{
this.menuVideoTypes.MenuItems[i].Checked = false;
}
this.menuVideoTypesAll.Checked = true;
}
/// <summary>
/// Checks the selected menu item of "Video Types".
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuVideoTypesStandard_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.menuVideoTypes.MenuItems.Count; i++)
{
this.menuVideoTypes.MenuItems[i].Checked = false;
}
this.menuVideoTypesStandard.Checked = true;
}
/// <summary>
/// Checks the selected menu item of "Video Types".
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuVideoTypesMessaging_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.menuVideoTypes.MenuItems.Count; i++)
{
this.menuVideoTypes.MenuItems[i].Checked = false;
}
this.menuVideoTypesMessaging.Checked = true;
}
/// <summary>
/// Resets all settings as the default value and updates UI.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuReset_Click(object sender, System.EventArgs e)
{
this.ResetOptions();
}
/// <summary>
/// Display a message box for program information.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuAbout_Click(object sender, System.EventArgs e)
{
MessageBox.Show("CECamera Version 1.0\n\nSimple sample for CameraCaptureDialog.", this.Text);
}
/// <summary>
/// Close self to exit the application.
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">Event arguments</param>
private void menuExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
/// <summary>
/// Resets all settings as the default value and updates UI.
/// </summary>
private void ResetOptions()
{
// Uncheck all checkboxs to specify the options using the default value
this.checkInitialDirectory.Checked = false;
this.checkDefaultFileName.Checked = false;
this.checkTitle.Checked = false;
this.checkResolution.Checked = false;
this.checkVideoTimeLimit.Checked = false;
this.checkVideoTimeLimit.Enabled = false;
// Set the default value for the options
this.textInitialDirectory.Text = cecameraDefaultInitialDirectory;
this.textDefaultFileName.Text = cecameraDefaultPictureFileName;
this.textTitle.Text = cecameraDefaultTitle;
this.textResolutionWidth.Text = cecameraDefaultResolutionWidth;
this.textResolutionHeight.Text = cecameraDefaultResolutionHeight;
this.textVideoTimeLimit.Text = cecameraDefaultVideoTimeLimit;
// Disable the edit controls since specify using default settings
this.textInitialDirectory.Enabled = false;
this.textDefaultFileName.Enabled = false;
this.textTitle.Enabled = false;
this.textResolutionWidth.Enabled = false;
this.textResolutionHeight.Enabled = false;
this.textVideoTimeLimit.Enabled = false;
// Checks the menu item of "Mode" as default item
for (int i = 0; i < this.menuMode.MenuItems.Count; i++)
{
this.menuMode.MenuItems[i].Checked = false;
}
this.menuModeStill.Checked = true;
// Checks the menu item of "Still Quality" as default item
for (int i = 0; i < this.menuStillQuality.MenuItems.Count; i++)
{
this.menuStillQuality.MenuItems[i].Checked = false;
}
this.menuStillQualityDefault.Checked = true;
// Checks the menu item of "Video Types" as default item
for (int i = 0; i < this.menuVideoTypes.MenuItems.Count; i++)
{
this.menuVideoTypes.MenuItems[i].Checked = false;
}
this.menuVideoTypesAll.Checked = true;
}
/// <summary>
/// Converts the string to an integer.
/// </summary>
/// <param name="value">A string to convert</param>
/// <param name="result">A integer of the converted result</param>
/// <returns>true if converts successfully; otherwise, false.</returns>
private static bool ConvertStringToInt(string value, ref int result)
{
try
{
result = Convert.ToInt32(value, NumberFormatInfo.CurrentInfo);
}
catch (FormatException)
{
return false;
}
catch (OverflowException)
{
return false;
}
return true;
}
}
}