home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / atlmovie / moviectl.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  4KB  |  154 lines

  1. // MovieCtl.cpp : Implementation of CMovieCtl
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include "stdafx.h"
  14. #include "ATLMovie.h"
  15. #include "MovieCtl.h"
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CMovieCtl
  19.  
  20. HRESULT CMovieCtl::OnDraw(ATL_DRAWINFO& di)
  21. {
  22.     // If we can get the ambient background color from the container then we
  23.     // will use it to fill our background
  24.     CComVariant var;
  25.     if (SUCCEEDED(m_spAmbientDispatch.GetProperty(DISPID_AMBIENT_BACKCOLOR, &var)))
  26.     {
  27.         LOGBRUSH    logbrush;
  28.         COLORREF    col;
  29.         logbrush.lbStyle = BS_SOLID;
  30.         OleTranslateColor(var.lVal, NULL, &col);
  31.         logbrush.lbColor = col;
  32.         HBRUSH hBrush = CreateBrushIndirect(&logbrush);
  33.         FillRect(di.hdcDraw, (RECT*)di.prcBounds, hBrush);
  34.         DeleteObject(hBrush);
  35.     }
  36.     return S_OK;
  37. }
  38.  
  39. STDMETHODIMP CMovieCtl::put_FileName(BSTR newVal)
  40. {
  41.     // Initialize Active Movie with the new filename
  42.     CreateFilterGraph(newVal);
  43.  
  44.     SysFreeString(newVal);
  45.     return S_OK; //hr;
  46. }
  47.  
  48. // Initialize Active Movie with the passed movie filename
  49. HRESULT CMovieCtl::CreateFilterGraph(LPCOLESTR strFile)
  50. {
  51.     HRESULT hr;
  52.  
  53.     // If we are resetting the filename we need to close the existing one,
  54.     // otherwise we'll get another window created.
  55.     if (m_spVideoWindow)
  56.     {
  57.         m_spVideoWindow->Visible = FALSE;   // Hide the video window
  58.         m_spVideoWindow->Owner = NULL;
  59.         m_spMediaControl.Release();
  60.         m_spVideoWindow.Release();
  61.     }
  62.     // Create the Active Movie object
  63.     hr = m_spVideoWindow.CreateInstance(__uuidof(FilgraphManager));
  64.     m_spMediaControl = m_spVideoWindow;
  65.  
  66.     // Open the passed file
  67.     hr = m_spMediaControl->RenderFile(strFile);
  68.  
  69.     // Now initialize the video window
  70.     HWND hWnd;
  71.     RECT rc = m_rcPos;
  72.     if (m_bWndLess)
  73.     {
  74.         // Obtain the HWND of the client's window
  75.         HDC hDC;
  76.         m_spInPlaceSite->GetDC(NULL, OLEDC_NODRAW, &hDC);
  77.         hWnd = WindowFromDC(hDC);
  78.     }
  79.     else
  80.     {
  81.         hWnd = m_hWnd;
  82.         OffsetRect(&rc, -rc.left, -rc.top);
  83.     }
  84.     m_spVideoWindow->Owner = (long)hWnd;
  85.     m_spVideoWindow->WindowStyle = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  86.     m_spVideoWindow->SetWindowPosition(rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);
  87.  
  88.     return hr;
  89. }
  90.  
  91. // Play the movie
  92. STDMETHODIMP CMovieCtl::Play()
  93. {
  94.     HRESULT hr;
  95.  
  96.     if (m_spVideoWindow == NULL)
  97.         return E_FAIL;
  98.  
  99.     // Find out if we've reached the end of the movie and if we have
  100.     // rewind to the beginning
  101.     IMediaPositionPtr spMP = m_spVideoWindow;
  102.     if (spMP)
  103.     {
  104.         if (spMP->CurrentPosition >= spMP->Duration)
  105.             spMP->CurrentPosition = 0;
  106.     }
  107.  
  108.     // Start the movie
  109.     if (m_spMediaControl)
  110.         hr = m_spMediaControl->Run();
  111.     else
  112.         hr = E_FAIL;
  113.  
  114.     return hr;
  115. }
  116.  
  117. // Pause the movie
  118. STDMETHODIMP CMovieCtl::Pause()
  119. {
  120.     if (m_spMediaControl)
  121.         m_spMediaControl->Pause();
  122.  
  123.     return S_OK;
  124. }
  125.  
  126. // Rewind the movie to the beginning
  127. STDMETHODIMP CMovieCtl::Reset()
  128. {
  129.     if (m_spVideoWindow)
  130.     {
  131.         IMediaPositionPtr spMP = m_spVideoWindow;
  132.         if (spMP)
  133.             spMP->CurrentPosition = 0;
  134.     }
  135.     return S_OK;
  136. }
  137.  
  138. // Stop the movie
  139. STDMETHODIMP CMovieCtl::Stop()
  140. {
  141.     if (m_spMediaControl)
  142.     {
  143.         m_spMediaControl->Stop();
  144.         Reset();                            // Go back to the beginning
  145.         m_spVideoWindow->Visible = FALSE;   // Hide the video window
  146.     }
  147.     return S_OK;
  148. }
  149.  
  150. // Override function so the COM support doesnt throw exeptions
  151. void __stdcall _com_raise_error(HRESULT hr, IErrorInfo* perrinfo) //throw(_com_error)
  152. {
  153. }
  154.