⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.h

📁 BCAM 1394 Driver
💻 H
字号:
//-----------------------------------------------------------------------------
//  (c) 2002 by Basler Vision Technologies
//  Section:  Vision Components
//  Project:  BCAM
//  $Header: mainfrm.h, 5, 02.10.2002 11:40:50, Nebelung, H.$
//-----------------------------------------------------------------------------
/**
  \file     mainfrm.h
  \brief    interface of the CMainFrame class
*/

#pragma once

/// Main window of the SDI application. Handles commands
class CMainFrame : 
  public CFrameWindowImpl<CMainFrame>, 
  public CUpdateUI<CMainFrame>,
  public CMessageFilter, 
  public CIdleHandler
{
public:
  DECLARE_FRAME_WND_CLASS(NULL, IDR_MAINFRAME)

  /// Called before the message handlers
  virtual BOOL PreTranslateMessage(MSG* pMsg)
  {
    return CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg);
  }
  /// Idle handler
  virtual BOOL OnIdle()
  {
    return FALSE;
  }
  
  BEGIN_MSG_MAP(CMainFrame)
    MESSAGE_HANDLER(WM_CREATE, OnCreate)
    
    // Step 2a (optional)
    // Add a handler function for WM_DEVICECHANGE messages
    // These messages are sent to the window you have registered with
    // the CBcam class by the plug&play manager (see step 1)
    MESSAGE_HANDLER(WM_DEVICECHANGE, OnDeviceChange)
    
    COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
    CHAIN_MSG_MAP(CUpdateUI<CMainFrame>)
    CHAIN_MSG_MAP(CFrameWindowImpl<CMainFrame>)
  END_MSG_MAP()
    
  BEGIN_UPDATE_UI_MAP(CMainFrame)
  END_UPDATE_UI_MAP()
    
  /// Creates the window.  
  LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  {
    // register object for message filtering and idle updates
    CMessageLoop* pLoop = _Module.GetMessageLoop();
    ATLASSERT(pLoop != NULL);
    pLoop->AddMessageFilter(this);
    pLoop->AddIdleHandler(this);
    
    // Step 1
    // Register a window with the CBcam class
    // The window is used to receive notification messages from 
    // the plug&play manager (see step 2)
    CBcam::RegisterClient(m_hWnd);
    
    return 0;
  }
  
  /// Fired if the application should exit.
  LRESULT OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
  {
    
    CBcam::UnregisterClient();  // Optional
    
    PostMessage(WM_CLOSE);
    return 0;
  }
  
  // Step 2b (optional)
  /// Handler for notifications sent by the plug&play manager
  LRESULT OnDeviceChange(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
  {
    
    // Step 3 (optional)
    // Handle the notification messages yourself
    switch (wParam) 
    {
    case DBT_DEVICEARRIVAL: 
      ATLTRACE("Something has arrived.\n");
      Beep(4000,500);
      
      // Check if it's a BCAM managed 1394 camera
      if( reinterpret_cast<PDEV_BROADCAST_HDR>(lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
      {
        string DeviceName = reinterpret_cast<PDEV_BROADCAST_DEVICEINTERFACE>(lParam)->dbcc_name;
        ATLTRACE("  It's a 1394 camera : Devicename = %s.\n", DeviceName.c_str() );
        
        // Step 4a
        // Create a camera object for the new device and store it in a map
        CBcam *pBcam = new CBcam();
        pBcam->Open(DeviceName.c_str());
        // The open function has registered our window also for device notifications related to the
        // file handle of the Bcam object.

        // Register callback functions fireing in case the opened device is removed
        pBcam->SetOnRemoveRequestCallback(&CMainFrame::OnRemoveRequestCallback);
        pBcam->SetOnRemoveCompleteCallback(&CMainFrame::OnRemoveCompleteCallback);
        m_Cameras[DeviceName] = pBcam;
        
      }
      break;
      
    case DBT_DEVICEREMOVECOMPLETE: 
      // We will recieve this kind of message two times for each opened removed device. 
      // The first message tells us that a device with a specific file handle has been removed (i.e, the file handle 
      // related device notification).
      // The second message will inform us, that a devcie of a specific type has been removed (i.e, the device interface
      // related notification).
      ATLTRACE("Something has been removed.\n");
      Beep(2000,500);
      
      if ( reinterpret_cast<PDEV_BROADCAST_HDR>(lParam)->dbch_devicetype == DBT_DEVTYP_HANDLE )
      {
        // We got a file handle related notification.
        // Before we get this message, the Bcam API has performed the following steps:
        //   1) Call the SetOnRemoveRequestCallback() function
        //   2) Close the Bcam object which encapsulates the file handle
        //   3) Call the SetOnRemoveCompleteCallback function
        HANDLE hFile = reinterpret_cast<PDEV_BROADCAST_HANDLE>(lParam)->dbch_handle;
        ATLTRACE("  It's a device assoiciated with the file handle %x\n", hFile);
      }

      if( reinterpret_cast<PDEV_BROADCAST_HDR>(lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
      {
        // We got a device interface related notification, i.e. a BCAM managed camera has been removed.
        string DeviceName = reinterpret_cast<PDEV_BROADCAST_DEVICEINTERFACE>(lParam)->dbcc_name;
        ATLTRACE("  It's a 1394 camera : Devicename = %s.\n", DeviceName.c_str() );
        
        // Step 4b
        // Purge the camera object from the map if the camera is removed
        m_Cameras.erase(DeviceName);
      }
      break;
    }
    
    return TRUE;
  }
  
protected:
  
  // Step 4c
  /// This map holds the dynamically allocated driver objects
  /// The key is the devicename - the value is a pointer to the Bcam object
  map<string, CBcam*> m_Cameras;
  
  // Step 4d
  /// This function is called if a device is going to be removed
  /// In this function you can call WaitForCompletion and get your buffers back
  /// since the kernel mode driver's handle is still open (the device object
  /// is already gone)
  static void OnRemoveRequestCallback(CBcam& rBcam, void* pContext)
  {
    ATLTRACE("OnRemoveRequestCallback : Devicename = %s\n", rBcam.Info.DeviceName());
    Beep(1000,500);
  }
  
  // Step 4e
  /// This function is called if a device has been removed
  /// In this function you cannot call WaitForCompletion because
  /// now the kernel mode driver has been closed.
  static void OnRemoveCompleteCallback(CBcam& rBcam, void* pContext)
  {
    ATLTRACE("SetOnRemoveCompleteCallback : Devicename = %s\n", rBcam.Info.DeviceName());
    Beep(1000,500);
  }
  
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -