コード例 #1
0
/**\brief Window procedure for the control dialog
 * \ingroup dialogs
 * \param hWnd The dialog window handle
 * \param message The message to process
 * \param wParam the window parameter
 * \param lParam the (often unused) generic long parameter
 * 
 * Duties:
 *  - menu items
 *     - load/save view settings to from/to registry
 *     - close the window
 *     - view menu toggles panes on and off
 *  - messages
 *     - paint: nothing (for now)
 *     - destroy: localfree the window extension structure
 *     - hscroll: scroll the panes back and forth
 */
LRESULT CALLBACK ControlDialogWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	int i;
	PAINTSTRUCT ps;
	HDC hdc;
	HKEY  hKey,hCSK;
	DWORD dwRet,dwDisposition,dwFoo,dwSize = sizeof(DWORD),dwType = REG_DWORD;
	SCROLLINFO si;
	PCONTROL_WINDOW_EXTENSION pExt = (PCONTROL_WINDOW_EXTENSION) GetWindowLongPtr(hWnd,GWLP_USERDATA);
	PCONTROL_PANE_EXTENSION pPaneExt;
	C1394Camera *pCam;
	C1394CameraControl *pControl;
	char buf[256];
	LRESULT lRetval = 0;
	HWND hWndChild;
	MSG msg;
	LARGE_INTEGER UniqueID;
	
	DllTrace(DLL_TRACE_ENTER,"ControlDialogProc(%08x,%08x,%08x,%08x)\n",
		hWnd,
		message,
		wParam,
		lParam);
	
	/* to maintain reasonable encapsulation, we need to translate our own accelerator messages */
	if(pExt)
	{
		msg.hwnd = hWnd;
		msg.message = message;
		msg.wParam = wParam;
		msg.lParam = lParam;
		TranslateAccelerator(hWnd,pExt->hAccel,&msg);
		message = msg.message;
		lParam = msg.lParam;
		wParam = msg.wParam;
	}
	
	switch (message) 
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		// Parse the menu selections:
		switch (wmId)
		{
		case ID_CONTROL_POLLALL:
			for(i=0; i<pExt->nPanes; i++)
			{
				if(pExt->PaneState[i])
					SendDlgItemMessage(hWnd,ID_FIRST_CONTROL_PANE + i,WM_COMMAND,MAKELONG(IDC_BUT_POLL,0),0);
			}
			break;
		case ID_FILE_LOADDEFAULTVIEW:
			pCam = pExt->pCamera;
			if(pCam)
			{
				if((hCSK = OpenCameraSettingsKey(NULL,0,KEY_ALL_ACCESS)) != NULL)
				{
					pCam->GetCameraUniqueID(&UniqueID);
					StringCbPrintf(buf,sizeof(buf),"%08x%08x\\ControlPanes\\DefaultView",UniqueID.HighPart,UniqueID.LowPart);
					dwRet = RegCreateKeyEx(hCSK,buf,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisposition);
					if(dwRet == ERROR_SUCCESS)
					{
						if(dwDisposition == REG_CREATED_NEW_KEY)
						{
							// we made a new key, so we need to write the initial settings
							for(i=0; i<pExt->nPanes; i++)
							{
								hWndChild = GetDlgItem(hWnd,ID_FIRST_CONTROL_PANE + i);
								pPaneExt = (PCONTROL_PANE_EXTENSION) GetWindowLongPtr(hWndChild,GWLP_USERDATA);
								dwFoo = 1;
								dwRet = RegSetValueEx(hKey,pPaneExt->pane_name,0,REG_DWORD,(LPBYTE)&dwFoo,dwSize);
								if(dwRet != ERROR_SUCCESS)
									DllTrace(DLL_TRACE_ERROR,
									"ControlDialogProc: Load Default View: error %d setting registry key for %s\n",
									dwRet,pPaneExt->pane_name);
							}
						}
						
						
						for(i=0; i<pExt->nPanes; i++)
						{
							hWndChild = GetDlgItem(hWnd,ID_FIRST_CONTROL_PANE + i);
							pPaneExt = (PCONTROL_PANE_EXTENSION) GetWindowLongPtr(hWndChild,GWLP_USERDATA);
							dwFoo = pExt->PaneState[i];
							dwRet = RegQueryValueEx(hKey,pPaneExt->pane_name,0,&dwType,(LPBYTE)&(dwFoo),&dwSize);
							
							if(dwRet != ERROR_SUCCESS)
								DllTrace(DLL_TRACE_ERROR,
								"ControlDialogProc: Load Default View: error %d setting registry key for %s\n",
								dwRet,pPaneExt->pane_name);
							
							CheckMenuItem(
								GetMenu(hWnd),
								ID_VIEW_CONTROL_START + i,
								MF_BYCOMMAND | (dwFoo ? MF_CHECKED : MF_UNCHECKED));
							
							pExt->PaneState[i] = (BOOL)dwFoo;
							
						}
						
						ShowControlPanes(hWnd,TRUE);
						
						RegCloseKey(hKey);
					} else {
						DllTrace(DLL_TRACE_ERROR,"ControlPaneDlgProc: Load Default View: Error %08x on RegCreateKeyEx\n",dwRet);
					}
					RegCloseKey(hCSK);
				} else {
					DllTrace(DLL_TRACE_ERROR,"ControlPaneDlgProc: Failed to open camera settings key: %s",StrLastError());
				}
			} else {
				// no camera: nothing to do
			}
			break;
			
		case ID_FILE_SAVEDEFAULTVIEW:
			pCam = pExt->pCamera;
			if(pCam)
			{
				if((hCSK = OpenCameraSettingsKey(NULL,0,KEY_ALL_ACCESS)) != NULL)
				{
					pCam->GetCameraUniqueID(&UniqueID);
					StringCbPrintf(buf,sizeof(buf),"%08x%08x\\ControlPanes\\DefaultView",UniqueID.HighPart,UniqueID.LowPart);
					dwRet = RegCreateKeyEx(hCSK,buf,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisposition);
					if(dwRet == ERROR_SUCCESS)
					{
						for(i=0; i<pExt->nPanes; i++)
						{
							hWndChild = GetDlgItem(hWnd,ID_FIRST_CONTROL_PANE + i);
							pPaneExt = (PCONTROL_PANE_EXTENSION) GetWindowLongPtr(hWndChild,GWLP_USERDATA);
							dwFoo = pExt->PaneState[i];
							dwRet = RegSetValueEx(hKey,pPaneExt->pane_name,0,REG_DWORD,(LPBYTE)&dwFoo,dwSize);
							if(dwRet != ERROR_SUCCESS)
								DllTrace(DLL_TRACE_ERROR,
								"ControlDialogProc: Save Default View: error %d setting registry key for %s\n",
								dwRet,pPaneExt->pane_name);
						}
						RegCloseKey(hKey);
					} else {
						DllTrace(DLL_TRACE_ERROR,"ControlDialogProc: Save Default View: Error %d opening key %s\n",dwRet,buf);
					}
					RegCloseKey(hCSK);
				} else {
					DllTrace(DLL_TRACE_ERROR,"ControlPaneDlgProc: Failed to open camera settings key: %s",StrLastError());
				}			
			} else {
				// no camera, nothing to do
			}
			break;
			
		case ID_FILE_CLOSE:
			DestroyWindow(hWnd);
			break;
		case ID_VIEW_ALLCONTROLS:
			for(i=0; i<pExt->nPanes; i++)
			{
				pExt->PaneState[i] = 1;
				CheckMenuItem(
					GetMenu(hWnd),
					ID_VIEW_CONTROL_START + i,
					MF_BYCOMMAND | MF_CHECKED);
			}
			ShowControlPanes(hWnd,TRUE);
			break;
		case ID_VIEW_STRICT_PRESENT:
			for(i=0; i<pExt->nPanes; i++)
			{
				HWND hWndPanel = GetDlgItem(hWnd,ID_FIRST_CONTROL_PANE + i);
				PCONTROL_PANE_EXTENSION pCPExt;
				pCPExt = (PCONTROL_PANE_EXTENSION)GetWindowLongPtr(hWndPanel,GWLP_USERDATA);
				pControl = pCPExt->pControl;
				pExt->PaneState[i] = 
					(((pCPExt->flags & PIF_STROBE) != 0) || pExt->pCamera->HasFeature(pControl->GetFeatureID()) &&
					pControl->HasPresence() &&
					pControl->StatusPresence());
				CheckMenuItem(
					GetMenu(hWnd),
					ID_VIEW_CONTROL_START + i,
					MF_BYCOMMAND | (pExt->PaneState[i] ? MF_CHECKED : MF_UNCHECKED));
			}
			ShowControlPanes(hWnd,TRUE);
			break;
		case ID_VIEW_LOOSE_PRESENT:
			for(i=0; i<pExt->nPanes; i++)
			{
				HWND hWndPanel = GetDlgItem(hWnd,ID_FIRST_CONTROL_PANE + i);
				PCONTROL_PANE_EXTENSION pCPExt;
				pCPExt = (PCONTROL_PANE_EXTENSION)GetWindowLongPtr(hWndPanel,GWLP_USERDATA);
				pControl = pCPExt->pControl;
				pExt->PaneState[i] = (pExt->pCamera->HasFeature(pControl->GetFeatureID()) ||
					pControl->HasPresence() ||
					pControl->StatusPresence());
				CheckMenuItem(
					GetMenu(hWnd),
					ID_VIEW_CONTROL_START + i,
					MF_BYCOMMAND | (pExt->PaneState[i] ? MF_CHECKED : MF_UNCHECKED));
			}
			ShowControlPanes(hWnd,TRUE);
			break;
		default:
			if(wmId >= ID_VIEW_CONTROL_START && wmId < ID_VIEW_CONTROL_END)
			{
				// toggle a pane
				i = wmId - ID_VIEW_CONTROL_START;
				pExt->PaneState[i] ^= 1;
				CheckMenuItem(
					GetMenu(hWnd),
					wmId,
					MF_BYCOMMAND | (pExt->PaneState[i] ? MF_CHECKED : MF_UNCHECKED));
				ShowControlPanes(hWnd,TRUE);
			} else {
				lRetval = DefWindowProc(hWnd, message, wParam, lParam);
			}
			
		} // switch(wmId)
		break;
		
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
		
	case WM_DESTROY:
		DllTrace(DLL_TRACE_CHECK,"ControlDialogWndProc: WM_DESTROY: Freeing %08x\n",pExt);
		DestroyAcceleratorTable(pExt->hAccel);
		SetFocus(pExt->hWndParent);
		LocalFree(pExt);
		break;
		
	case WM_HSCROLL:
		si.cbSize = sizeof(SCROLLINFO);
		si.fMask = SIF_ALL;
		GetScrollInfo(hWnd,SB_HORZ,&si);
		switch(LOWORD(wParam))
		{
		case SB_THUMBPOSITION:
			si.nPos = si.nTrackPos;
			break;
		case SB_LEFT:
		case SB_LINELEFT:
			si.nPos -= 10;
			break;
		case SB_RIGHT:
		case SB_LINERIGHT:
			si.nPos += 10;
			break;
		case SB_PAGELEFT:
			si.nPos -= si.nPage;
			break;
		case SB_PAGERIGHT:
			si.nPos += si.nPage;
			break;
		}
		si.fMask = SIF_POS;
		pExt->trackpos = (LOWORD(wParam) == SB_THUMBTRACK ? si.nTrackPos : si.nPos);
		if(pExt->trackpos < 0)
			pExt->trackpos = 0;
		if(pExt->trackpos >= (si.nMax - (int)si.nPage))
			pExt->trackpos = si.nMax - si.nPage - 1;
		SetScrollInfo(hWnd,SB_HORZ,&si,TRUE);
		if(LOWORD(wParam) != SB_THUMBPOSITION)
			ShowControlPanes(hWnd,FALSE);
		break;
		
		default:
			lRetval = DefWindowProc(hWnd, message, wParam, lParam);
   }
   
   DllTrace(DLL_TRACE_EXIT,"EXIT ControlDialogWndProc (%d)\n",lRetval);
   return lRetval;
}
コード例 #2
0
BOOL C1394CameraDemoApp::OnIdle(LONG lCount)
{
	CMainFrame* pWnd = (CMainFrame *) theApp.GetMainWnd();
  HANDLE hFrameEvent;
  DWORD dwRet;
  BOOL GotFrame = FALSE;
  ULONG Timeout = 1000;
  ULONG Dropped = 0;
  unsigned long t;
  float fps;
  float belief;
  char buf[256];
  if(this->m_showCamera)
  {
    do {
      hFrameEvent = theCamera.GetFrameEvent();
      
      if(hFrameEvent == NULL)
      {
        // no frame is attached, push it on
        OutputDebugString("Pushing\n");
        if(theCamera.AcquireImageEx(FALSE,NULL) != CAM_ERROR_FRAME_TIMEOUT)
        {
          AfxMessageBox("WTF?");
        }
        hFrameEvent = theCamera.GetFrameEvent();
      }
      dwRet = MsgWaitForMultipleObjects(1,&hFrameEvent,FALSE,Timeout,QS_ALLINPUT);
      switch(dwRet)
      {
      case WAIT_OBJECT_0:
        // got a frame
        if(Timeout == 0)
        {
          m_dropped++;
          OutputDebugString(" -> Drop\n");
        } else {
          t = clock();
          if(m_frames >= 32)
            m_timesum -= m_times[m_frames & 31];
          m_times[m_frames & 31] = t - m_lastclock;
          m_lastclock = t;
          m_timesum += m_times[m_frames & 31];
          m_frames++;
          if(m_timesum > 0)
          {
            fps = (float)(m_frames > 32 ? 32 : m_frames);
            fps /= m_timesum;
            fps *= 1000.0;
          } else {
            fps = 0.0;
          }
          // simple kalman on fps to provide some visual stability
          belief = ((float)(m_frames > 32 ? 32 : m_frames)) / 40.0f;
          m_fps = belief * m_fps + (1.0f - belief) * fps;
          sprintf(buf,"Displaying: %.1f fps, %.1f%% dropped\n",
            m_fps,100.0f * (float)(m_dropped)/(float)(m_frames));
          ((CMainFrame*)m_pMainWnd)->SetStatus(buf);
          GotFrame = TRUE;
          Timeout = 0;
        }
        if(theCamera.AcquireImageEx(FALSE,NULL) != CAM_SUCCESS)
        {
          sprintf(buf,"Error \"%s\" while Acquiring Images, Terminate Acguitision?",StrLastError());
          if(AfxMessageBox(buf,MB_YESNO,0) == IDYES)
            this->On1394StopCamera();
        }
        break;
      case WAIT_OBJECT_0 + 1:
        // got a message
        break;
      case WAIT_TIMEOUT:
        // timeout
        if(Timeout == 1000)
        {
          m_timeouts++;
          m_timeouts &= 0x3;
          sprintf(buf,"Timeout.%c%c%c",
            m_timeouts > 0 ? '.' : ' ',
            m_timeouts > 1 ? '.' : ' ',
            m_timeouts > 2 ? '.' : ' ');
          ((CMainFrame*)m_pMainWnd)->SetStatus(buf);
        }
        break;
      default:
        // error
        sprintf(buf,"Error \"%s\" while Acquiring Images, Terminate Acquitision?",StrLastError());
        if(AfxMessageBox(buf,MB_YESNO,0) == IDYES)
          this->On1394StopCamera();
        break;
      }
    } while(dwRet == WAIT_OBJECT_0);
    if(GotFrame)
    {
    	BITMAPINFO bmi;
      unsigned long wd,ht;
      theCamera.GetVideoFrameDimensions(&wd,&ht);
	    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
      bmi.bmiHeader.biWidth = (long) wd;
      bmi.bmiHeader.biHeight = (long) ht;
	    bmi.bmiHeader.biPlanes = 1;
	    bmi.bmiHeader.biBitCount = 24;
	    bmi.bmiHeader.biCompression = BI_RGB;
	    bmi.bmiHeader.biSizeImage = 0;
	    bmi.bmiHeader.biXPelsPerMeter = 1000;
	    bmi.bmiHeader.biYPelsPerMeter = 1000;
	    bmi.bmiHeader.biClrUsed = 0;
	    bmi.bmiHeader.biClrImportant = 0;
	    RECT rect;
	    int x,y,w,h,i=0, timeouts = 0;
	    unsigned long sum = 0, t=clock(),totaldropped = 0;
      int drop = 0;
      CDC *pDC = pWnd->GetViewDC();
      HDC hDC = pDC->m_hDC;
      BOOL DropStuff = TRUE;
		  pWnd->GetWindowRect(&rect);
		  h = rect.bottom - rect.top - theApp.m_borderHeight;
		  w = rect.right - rect.left - theApp.m_borderWidth;

		  x = w - wd;
		  x >>= 1;
		  if(x < 0) x = 0;

		  y = h - ht;
		  y >>= 1;
		  if(y < 0) y = 0;

      theCamera.getDIB(this->m_pBitmap,this->m_pBitmapLength);
		  SetDIBitsToDevice(hDC, x, y, wd, ht, 0, 0, 0, ht, theApp.m_pBitmap, &bmi, DIB_RGB_COLORS);
      pWnd->ReleaseViewDC(pDC);
    } 
    return TRUE;
  }
  return FALSE;
}