📄 camtest2.cpp
字号:
// DoSizeMain - Process WM_SIZE message for window.
//
LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
return 0;
}
//----------------------------------------------------------------------
// DoInitMenuMain - Process WM_INITMENUPOPUP message for window.
//
LRESULT DoInitMenuMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
HMENU hMenu = (HMENU)wParam;
int i;
TCHAR szName[128];
HMENU hSub = GetSubMenu (hMenu, 3);
// Delete the old menu items
for (i = 0; ; i++)
{
if (!DeleteMenu (hSub, ID_CAMERA_IMAGESIZE_1+i, MF_BYCOMMAND))
break;
}
// Set the resolutions on the screen size sub menu
for (i = 0; (i < nFmtCount); i++)
{
wsprintf (szName, TEXT("&%d %d x %d"), i+1, Formats[i].dwWidth, Formats[i].dwHeight);
AppendMenu (hSub, MF_STRING, ID_CAMERA_IMAGESIZE_1+i, szName);
// Check the resolution currently in use
if (i+1 == wFrame)
{
CheckMenuItem (hSub, ID_CAMERA_IMAGESIZE_1+i,
MF_BYCOMMAND | MF_CHECKED);
}
}
hSub = GetSubMenu (hMenu, 4);
// Delete the old menu items
for (i = 0; ; i++)
{
if (!DeleteMenu (hSub, ID_CAMERA_IMAGERATE_1+i, MF_BYCOMMAND))
break;
}
// Set the frame speed on the Frame rate sub menu
if (Formats[wFrame-1].nNumInterval)
{
for (i = 0; (i < Formats[wFrame-1].nNumInterval); i++)
{
wsprintf (szName, TEXT("%d fps"), 1000/(Formats[wFrame-1].dwInterval[i]/10000));
AppendMenu (hSub, MF_STRING, ID_CAMERA_IMAGERATE_1+i, szName);
if (i == wInterval)
{
// Check the resolution currently in use
CheckMenuItem (hMenu, ID_CAMERA_IMAGERATE_1+i,
MF_BYCOMMAND | MF_CHECKED );
}
}
}
if (fDraw)
CheckMenuItem (hMenu, ID_CAMERA_STOPDRAWING,
MF_BYCOMMAND | MF_UNCHECKED);
else
CheckMenuItem (hMenu, ID_CAMERA_STOPDRAWING,
MF_BYCOMMAND | MF_CHECKED);
return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
WORD idItem, wNotifyCode;
HWND hwndCtl;
int i;
// Parse the parameters.
idItem = (WORD) LOWORD (wParam);
wNotifyCode = (WORD) HIWORD (wParam);
hwndCtl = (HWND) lParam;
// Call routine to handle control message.
for (i = 0; i < dim(MainCommandItems); i++) {
if (idItem == MainCommandItems[i].Code)
return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
wNotifyCode);
}
return 0;
}
//----------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PAINTSTRUCT ps;
RECT rect;
HDC hdc;
// Adjust the size of the client rectangle to take into account
// the command bar height.
GetClientRect (hWnd, &rect);
rect.top += CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR));
hdc = BeginPaint (hWnd, &ps);
EndPaint (hWnd, &ps);
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
StopStreaming (dwContext);
ReleaseDC (hWnd, hdcMain);
PostQuitMessage (0);
return 0;
}
//======================================================================
// Menu handlers
//
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
SendMessage (hWnd, WM_CLOSE, 0, 0);
return 0;
}
//----------------------------------------------------------------------
// DoCommandImageSize - Process Image size menu items
//
LPARAM DoCommandImageSize (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
// Stop the current stream
StopStreaming (dwContext);
// Get frame index
wFrame = idItem - ID_CAMERA_IMAGESIZE_1 + 1;
// Restart the stream
StreamToClientWindow (hWnd);
return 0;
}
//----------------------------------------------------------------------
// DoCommandImageRate - Process Image frame rate menu items
//
LPARAM DoCommandImageRate (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
// Stop the current stream
StopStreaming (dwContext);
// Get frame index
wInterval = idItem - ID_CAMERA_IMAGERATE_1;
// Restart the stream
StreamToClientWindow (hWnd);
return 0;
}
//----------------------------------------------------------------------
// DoCommandStartStream - Process Start Stream command
//
LPARAM DoCommandStartStream (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
// Start the stream
StreamToClientWindow (hWnd);
return 0;
}
//----------------------------------------------------------------------
// DoCommandStopDrawing -
//
LPARAM DoCommandStopDrawing (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
// Stop the stream
if (fDraw)
fDraw = FALSE;
else
fDraw = TRUE;
SetDrawState (dwContext, fDraw);
return 0;
}
//----------------------------------------------------------------------
// DoCommandStopStream - Process Stop Stream command
//
LPARAM DoCommandStopStream (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
// Stop the stream
StopStreaming (dwContext);
return 0;
}
//----------------------------------------------------------------------
// DoCommandGetStillImage -
//
LPARAM DoCommandGetStillImage (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode)
{
LPBYTE pData = 0;
DWORD dwSize = 0;
FORMATPROPS fp;
STILLCAPDLGSTRUCT scStruct;
int rc;
// Display a dialog to get the format and file name
rc = DialogBoxParam (hInst, MAKEINTRESOURCE (IDD_STILLCAPDLG),
hWnd, StillCapDlgProc, (LPARAM)&scStruct);
if (rc == 0)
return 0;
// Set the busy cursor since this may take a bit
HCURSOR hOld = SetCursor (LoadCursor(NULL, IDC_WAIT));
// Go get that image!
memset (&fp, 0, sizeof (fp));
fp.wFormatIndex = scStruct.wFormat;
fp.wFrameIndex = scStruct.wFrame;
rc = GetStillImage (dwContext, 1, scStruct.wFrame, &fp, &pData, &dwSize);
if (rc == 0)
{
// Write the file
rc = WriteJPEG (scStruct.szFileName, pData, dwSize);
if (rc != 0)
printf ("Error writing still image to file rc %d\r\n", rc);
}
else
{
printf ("Error capturing still image rc %d\r\n", rc);
}
// Restore original cursor
SetCursor (hOld);
// Free the buffer
LocalFree (pData);
// Report to the user
TCHAR sz[256];
if (rc == 0)
lstrcpy (sz, TEXT("Image file created"));
else if (rc == 258)
wsprintf (sz, TEXT("Timeout Error waiting for still.\r\n"));
else
wsprintf (sz, TEXT("Error capturing still.\r\nError %d"), rc);
MessageBox (hWnd, sz, szAppName, MB_OK);
return 0;
}
//----------------------------------------------------------------------
// DoCommandSetSettings -
//
LPARAM DoCommandSetSettings (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode)
{
// Create dialog box only if not already created.
if (g_hwndMlDlg == 0)
// Use CreateDialog to create modeless dialog box.
g_hwndMlDlg = CreateDialog (hInst, MAKEINTRESOURCE (IDC_SETTINGS), hWnd,
SettingsDlgProc);
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandAbout - Process the Help | About menu command.
//
LPARAM DoMainCommandAbout(HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
// Use DialogBox to create modal dialog.
MessageBox (hWnd, TEXT("CamTest 0.3\r\nCopyright 2005 Douglas Boling"),
TEXT("About CamTest"), MB_OK);
return 0;
}
//----------------------------------------------------------------------
// Helper routine to reduce duplicated code. Simply starts streaming
// to a place 5 pixels from left and 5 down from cmd bar
//
int StreamToClientWindow (HWND hWnd)
{
int nHeight = 0;
#ifdef USECMDBAR
nHeight = CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR));
#endif
InvalidateRect (hWnd, NULL, NULL);
// Setting right and bot to 0 tells the code to use frame size
RECT rect;
SetRect (&rect, PIC_LEFT, nHeight + PIC_TOP, 0, 0);
// Find the frames per second value
DWORD dwFrameInterval = -1; // Assume slowest interval
if (wInterval < Formats[wFrame].nNumInterval)
dwFrameInterval = Formats[wFrame].dwInterval[wInterval];
StartStreaming (dwContext, hdcMain, &rect, wFmt, wFrame, dwFrameInterval);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -