home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / ba / usevideo / videomfc / vdmfcdlg.cpp < prev    next >
C/C++ Source or Header  |  1997-08-29  |  8KB  |  327 lines

  1. //
  2. // VdMFCDlg.cpp: Implements the CVdMFCDlg class, which is
  3. //               the application's main dialog. 
  4. //
  5. // Copyright (C) 1997 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Broadcast Architecture Programmer's Reference.
  10. // For detailed information regarding Broadcast
  11. // Architecture, see the reference.
  12. //
  13.  
  14. #include "stdafx.h"
  15. #include <initguid.h>
  16. #include "VdMFC.h"
  17. #include "VdMFCDlg.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CVdMFCDlg dialog
  27.  
  28. CVdMFCDlg::CVdMFCDlg(CWnd* pParent /*=NULL*/)
  29.     : CDialog(CVdMFCDlg::IDD, pParent)
  30. {
  31.     //{{AFX_DATA_INIT(CVdMFCDlg)
  32.     m_channel = 0;
  33.     m_filename = _T("");
  34.     //}}AFX_DATA_INIT
  35.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  36.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  37. }
  38.  
  39. void CVdMFCDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41.     CDialog::DoDataExchange(pDX);
  42.     //{{AFX_DATA_MAP(CVdMFCDlg)
  43.     DDX_Control(pDX, IDC_VIDCNTRL, m_CVid);
  44.     DDX_Text(pDX, IDC_EDITCHANNEL, m_channel);
  45.     DDX_Text(pDX, IDC_EDITFILENAME, m_filename);
  46.     //}}AFX_DATA_MAP
  47. }
  48.  
  49. BEGIN_MESSAGE_MAP(CVdMFCDlg, CDialog)
  50.     //{{AFX_MSG_MAP(CVdMFCDlg)
  51.     ON_WM_PAINT()
  52.     ON_WM_QUERYDRAGICON()
  53.     ON_BN_CLICKED(IDC_SETINPUT, OnSetInput)
  54.     ON_BN_CLICKED(IDC_SETOUTPUT, OnSetOutput)
  55.     ON_BN_CLICKED(IDC_SETCHANNEL, OnSetChannel)
  56.     ON_BN_CLICKED(IDC_SETFILENAME, OnSetFile)
  57.     ON_BN_CLICKED(IDC_PLAY, OnPlay)
  58.     ON_BN_CLICKED(IDC_PAUSE, OnPause)
  59.     ON_BN_CLICKED(IDC_STOP, OnStop)
  60.     ON_BN_CLICKED(IDC_VIDEOON, OnVideoOn)
  61.     //}}AFX_MSG_MAP
  62. END_MESSAGE_MAP()
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CVdMFCDlg message handlers
  66.  
  67. BOOL CVdMFCDlg::OnInitDialog()
  68. {
  69.     CDialog::OnInitDialog();
  70.  
  71.     // Set the icon for this dialog.  The framework does this automatically
  72.     //  when the application's main window is not a dialog
  73.     SetIcon(m_hIcon, TRUE);            // Set big icon
  74.     SetIcon(m_hIcon, FALSE);        // Set small icon
  75.  
  76.     // Assign the list-box control to the CListBox variable and
  77.     // then verify.
  78.     VERIFY( box.SubclassDlgItem( IDC_LISTBOX, this ) );
  79.     
  80.     // Obtain pointers to unknown and enumerate-variant objects.
  81.  
  82.     LPUNKNOWN lpunk;
  83.     LPENUMVARIANT lpenumvar;
  84.  
  85.     // Declare and allocate a Devices object and assign the 
  86.     // collection of all the available input and output devices 
  87.     // for the video control to this object.
  88.     CBPCDevices pDevices = m_CVid.GetDevices();
  89.  
  90.     // Assign the unknown object to the value returned from the
  91.     // GetNewEnum function. GetNewEnum is a custom function added
  92.     // to the CBPCDevices class.
  93.     lpunk = pDevices.GetNewEnum(); 
  94.  
  95.     // Query the unknown object to obtain an enumerate-variant 
  96.     // object and then release the unknown object.
  97.     if( lpunk == NULL )
  98.         return FALSE;
  99.  
  100.     VERIFY( SUCCEEDED( lpunk->QueryInterface( IID_IEnumVARIANT, ( void** )&lpenumvar ) ) );
  101.     lpunk->Release();
  102.     
  103.     long celt;
  104.     COleVariant var;
  105.  
  106.     // While there are still devices that can provide input or  
  107.     // output for the video control, construct a DeviceBase object
  108.     // from the next variant obtained from the enumerate-variant
  109.     // object and add the device's name to the list box.
  110.     while( S_OK == lpenumvar->Next( 1, &var, ( unsigned long* )&celt ) )
  111.     {
  112.         ASSERT( var.vt == VT_DISPATCH );
  113.         CBPCDeviceBase id( var.pdispVal );
  114.         box.AddString( id.GetName() );
  115.  
  116.         var.Clear();
  117.     }
  118.  
  119.     // Release the enumerate-variant object.
  120.     lpenumvar->Release();
  121.  
  122.     return TRUE;  // return TRUE  unless you set the focus to a control
  123. }
  124.  
  125. // If you add a minimize button to your dialog, you will need the code below
  126. //  to draw the icon.  For MFC applications using the document/view model,
  127. //  this is automatically done for you by the framework.
  128.  
  129. void CVdMFCDlg::OnPaint() 
  130. {
  131.     if (IsIconic())
  132.     {
  133.         CPaintDC dc(this); // device context for painting
  134.  
  135.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  136.  
  137.         // Center icon in client rectangle
  138.         int cxIcon = GetSystemMetrics(SM_CXICON);
  139.         int cyIcon = GetSystemMetrics(SM_CYICON);
  140.         CRect rect;
  141.         GetClientRect(&rect);
  142.         int x = (rect.Width() - cxIcon + 1) / 2;
  143.         int y = (rect.Height() - cyIcon + 1) / 2;
  144.  
  145.         // Draw the icon
  146.         dc.DrawIcon(x, y, m_hIcon);
  147.     }
  148.     else
  149.     {
  150.         CDialog::OnPaint();
  151.     }
  152. }
  153.  
  154. // The system calls this to obtain the cursor to display while the user drags
  155. //  the minimized window.
  156. HCURSOR CVdMFCDlg::OnQueryDragIcon()
  157. {
  158.     return (HCURSOR) m_hIcon;
  159. }
  160.  
  161. void CVdMFCDlg::OnSetInput() 
  162. {
  163.     LPUNKNOWN lpunk;
  164.     LPENUMVARIANT lpenumvar;
  165.     BOOL bFound = FALSE;
  166.     IBPCDeviceBase* pDevice;
  167.  
  168.     CBPCDevices pDevices = m_CVid.GetDevices();
  169.     lpunk = pDevices.GetNewEnum(); //GetNewEnum custom function see 
  170.                                    //details
  171.  
  172.     if( lpunk == NULL )
  173.         return;
  174.  
  175.     VERIFY( SUCCEEDED( lpunk->QueryInterface( IID_IEnumVARIANT, ( void** )&lpenumvar ) ) );
  176.     lpunk->Release();
  177.  
  178.     for( int i = 0; i < m_CVid.GetDeviceCount(); i++ )
  179.     {
  180.         long celt;
  181.         COleVariant var;
  182.  
  183.         if( SUCCEEDED( lpenumvar->Next( 1, &var, (unsigned long*)&celt ) ) )
  184.         {
  185.             var.punkVal->QueryInterface( IID_IBPCDeviceBase, (void**)&pDevice );
  186.  
  187.             if( pDevice != NULL )
  188.             {
  189.                 BSTR bsDeviceName;
  190.                 pDevice->get_Name( &bsDeviceName );
  191.                 CString str( bsDeviceName );
  192.                 SysFreeString( bsDeviceName );
  193.  
  194.                 CString string;
  195.                 int sel = box.GetCurSel();
  196.  
  197.                 if( sel == LB_ERR )
  198.                 {
  199.                     AfxMessageBox( "Please select a device from the list" );
  200.                     pDevice->Release();
  201.                     lpenumvar->Release();
  202.                     var.Clear();
  203.                     return;
  204.                 }
  205.                 box.GetText( sel, string );
  206.  
  207.                 if( str == string )
  208.                 {
  209.                     m_CVid.SetInput( pDevice );
  210.                     m_pDeviceBase = pDevice;
  211.                     bFound = TRUE; 
  212.                 }
  213.                 pDevice->Release();
  214.             }
  215.             var.Clear();
  216.         }
  217.         if( bFound )
  218.             break;
  219.     }
  220.     ASSERT( lpenumvar != NULL );
  221.     lpenumvar->Release();    
  222. }
  223.  
  224. void CVdMFCDlg::OnSetOutput() 
  225. {
  226.     LPUNKNOWN lpunk;
  227.     LPENUMVARIANT lpenumvar;
  228.     BOOL bFound = FALSE;
  229.     IBPCDeviceBase* pDevice;
  230.  
  231.     CBPCDevices pDevices = m_CVid.GetDevices();
  232.     lpunk = pDevices.GetNewEnum(); //GetNewEnum custom function see 
  233.                                    //details
  234.  
  235.     if( lpunk == NULL )
  236.         return;
  237.  
  238.     VERIFY( SUCCEEDED( lpunk->QueryInterface( IID_IEnumVARIANT, ( void** )&lpenumvar ) ) );
  239.     lpunk->Release();
  240.  
  241.     for( int i = 0; i < m_CVid.GetDeviceCount(); i++ )
  242.     {
  243.         long celt;
  244.         COleVariant var;
  245.  
  246.         if( SUCCEEDED( lpenumvar->Next( 1, &var, (unsigned long*)&celt ) ) )
  247.         {
  248.             var.punkVal->QueryInterface( IID_IBPCDeviceBase, (void**)&pDevice );
  249.  
  250.             if( pDevice != NULL )
  251.             {
  252.                 BSTR bsDeviceName;
  253.                 pDevice->get_Name( &bsDeviceName );
  254.                 CString str( bsDeviceName );
  255.                 SysFreeString( bsDeviceName );
  256.  
  257.                 CString string;
  258.                 int sel = box.GetCurSel();
  259.  
  260.                 if( sel == LB_ERR )
  261.                 {
  262.                     AfxMessageBox( "Please select a device from the list" );
  263.                     pDevice->Release();
  264.                     lpenumvar->Release();
  265.                     var.Clear();
  266.                     return;
  267.                 }
  268.                 box.GetText( sel, string );
  269.  
  270.                 if( str == string )
  271.                 {
  272.                     m_CVid.SetOutput( pDevice );
  273.                     m_pDeviceBase = pDevice;
  274.                     bFound = TRUE; 
  275.                 }
  276.                 pDevice->Release();
  277.             }
  278.             var.Clear();
  279.         }
  280.         if( bFound )
  281.             break;
  282.     }
  283.     ASSERT( lpenumvar != NULL );
  284.     lpenumvar->Release();    
  285. }
  286.  
  287. void CVdMFCDlg::OnVideoOn() 
  288. {
  289.     m_CVid.SetVideoOn( !m_CVid.GetVideoOn() );
  290.     
  291. }
  292.  
  293. void CVdMFCDlg::OnSetChannel() 
  294. {
  295.     UpdateData( TRUE );
  296.      CBPCDeviceBase id;
  297.     
  298.     id.AttachDispatch( m_pDeviceBase, FALSE );
  299.     id.SetChannel( m_channel );
  300. }
  301.  
  302. void CVdMFCDlg::OnSetFile() 
  303. {
  304.     UpdateData( TRUE );
  305.     m_CVid.SetFileName( m_filename );
  306.     
  307. }
  308.  
  309. void CVdMFCDlg::OnPlay() 
  310. {
  311.     m_CVid.Run();
  312.     
  313. }
  314.  
  315. void CVdMFCDlg::OnPause() 
  316. {
  317.     m_CVid.Pause();
  318.     
  319. }
  320.  
  321. void CVdMFCDlg::OnStop() 
  322. {
  323.     m_CVid.Stop();
  324.     
  325. }
  326.  
  327.