home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / MRCE / SOURCE.ZIP / MRCANIMA.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-11  |  11.1 KB  |  399 lines

  1. // MRCEXT: Micro Focus Extension DLL for MFC 2.1+
  2. // Copyright (C)1994-5    Micro Focus Inc, 2465 East Bayshore Rd, Palo Alto, CA 94303.
  3. // 
  4. //  This program is free software; you can redistribute it and/or modify
  5. //  it under the terms of the GNU General Public License as published by
  6. //  the Free Software Foundation. In addition, you may also charge for any
  7. //  application    using MRCEXT, and are under no obligation to supply source
  8. //  code. You must accredit Micro Focus Inc in the "About Box", or banner
  9. //  of your application. 
  10. //
  11. //  This program is distributed in the hope that it will be useful,
  12. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. //  GNU General Public License for more details.
  15. //
  16. //  You should also have received a copy of the GNU General Public License with this
  17. //  software, also indicating additional rights you have when using MRCEXT.  
  18. //
  19. //
  20. // mrcanima.cpp : implementation file
  21. //
  22. // $Date:   11 Sep 1995 09:48:46  $
  23. // $Revision:   1.8  $
  24. // $Author:   MRC  $
  25.     
  26.  
  27.  
  28. #include "mrcstafx.h"
  29. #include <mmsystem.h>        
  30. #include <digitalv.h>        
  31. #include "mrcext.h"
  32.  
  33. #ifdef LINK_TO_WINMM
  34. // link to the DLL
  35. #pragma comment(lib, "winmm.lib")
  36.  
  37. #else     // load the DLL dynamically
  38.  
  39. typedef MCIERROR (APIENTRY * pftmciSendCommand) (MCIDEVICEID mciId, UINT uMsg, DWORD dwP1, DWORD dwP2);
  40. static pftmciSendCommand pfmciSendCommand;
  41. typedef BOOL (APIENTRY * pftPlaySound)(LPCTSTR lpszName, HANDLE hModule, DWORD fdwSound);    
  42. static pftPlaySound pfPlaySound;
  43. typedef BOOL (APIENTRY * pftsndPlaySound)(LPCTSTR lpszName, UINT options);
  44. static pftsndPlaySound pfsndPlaySound;
  45.  
  46. static HINSTANCE hinstWinMM;
  47. #undef mciSendCommand
  48. #define mciSendCommand pfmciSendCommand
  49. #undef sndPlaySound
  50. #define sndPlaySound pfsndPlaySound
  51. #undef PlaySound
  52. #define PlaySound pfPlaySound
  53. #endif
  54.  
  55. BOOL CMRCAnimateCtrl::m_bInitialized = FALSE;
  56.  
  57. #ifdef _DEBUG
  58. #undef THIS_FILE
  59. static char BASED_CODE THIS_FILE[] = __FILE__;
  60. #endif
  61.  
  62.  
  63. //--------------------------------------------------------------------------------
  64. void CenterWindowWithinParent(CWnd * pWnd, CWnd * pAlternate)
  65. //--------------------------------------------------------------------------------
  66. {
  67.     CWnd * pOwner = pWnd->GetParent();        // owner rectangle - needed for positioning
  68.     CWnd * pCenterWnd;
  69.     if (pAlternate != NULL)
  70.         pCenterWnd = pAlternate;
  71.     else
  72.         pCenterWnd = pOwner;
  73.  
  74.     CRect Rect, ParRect;
  75.     pWnd->GetWindowRect(&Rect);
  76.     pCenterWnd->GetWindowRect(&ParRect);
  77.     Rect.left = (ParRect.right + ParRect.left - Rect.Width()) / 2; 
  78.     Rect.top  = (ParRect.bottom + ParRect.top - Rect.Height()) / 2; 
  79.     pOwner->ScreenToClient(&(Rect.TopLeft()));
  80.     
  81.     pWnd->SetWindowPos(NULL, Rect.left, Rect.top, 0, 0,
  82.                     SWP_NOZORDER | SWP_NOSIZE);
  83. }
  84.  
  85.  
  86. #define AVI_VIDEO "avivideo"
  87. #define REPEAT_TIMER_ID    1099
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CMRCAnimateCtrl
  90.  
  91. //--------------------------------------------------------------------------------
  92. CMRCAnimateCtrl::CMRCAnimateCtrl()
  93. //--------------------------------------------------------------------------------
  94. {
  95.     m_bFileOpen = FALSE;
  96.     m_nFrom = 0;
  97.     m_nTo = -1;
  98.     m_nRepeatDelay = 0;
  99.     m_dwPlayFlags = 0;
  100. }
  101.  
  102.  
  103. CMRCAnimateCtrl::~CMRCAnimateCtrl()
  104. {
  105.     Close();
  106. }
  107.  
  108.  
  109. BEGIN_MESSAGE_MAP(CMRCAnimateCtrl, CWnd)
  110.     //{{AFX_MSG_MAP(CMRCAnimateCtrl)
  111.     ON_WM_TIMER()
  112.     ON_WM_DESTROY()
  113.     //}}AFX_MSG_MAP
  114.     ON_MESSAGE(MM_MCINOTIFY, OnMCINotify)
  115. END_MESSAGE_MAP()
  116.  
  117.  
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CMRCAnimateCtrl message handlers
  120.  
  121.  
  122.  
  123. //---------------------------------------------------------------------------
  124. BOOL CMRCAnimateCtrl::Init() 
  125. //---------------------------------------------------------------------------
  126. {
  127.     if (m_bInitialized)
  128.         return TRUE;
  129.  
  130.     m_bInitialized = TRUE;
  131. #ifndef LINK_TO_WINMM
  132.     hinstWinMM = LoadLibrary("winmm.dll");
  133.     if (hinstWinMM == NULL)
  134.         return FALSE;
  135.     pfmciSendCommand = (pftmciSendCommand) GetProcAddress(hinstWinMM, "mciSendCommandA");    
  136.     if (pfmciSendCommand == NULL)
  137.         return FALSE;    
  138.     if (!(pfPlaySound = (pftPlaySound) GetProcAddress(hinstWinMM, "PlaySoundA")))    
  139.         return FALSE;
  140.     if (!(pfsndPlaySound = (pftsndPlaySound) GetProcAddress(hinstWinMM, "sndPlaySoundA")))    
  141.         return FALSE;
  142.  
  143.  
  144. #endif
  145.     return TRUE;
  146. }
  147.  
  148.  
  149. //---------------------------------------------------------------------------
  150. BOOL CMRCAnimateCtrl::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
  151. //---------------------------------------------------------------------------
  152. {
  153.     // if first create, attempt to load the DLL. If this fails then video is
  154.     // not available
  155.     if (Init() == FALSE)
  156.         return FALSE;
  157.     return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
  158. }
  159.  
  160.  
  161. //---------------------------------------------------------------------------
  162. BOOL CMRCAnimateCtrl::Open(LPCTSTR pFilename)             // open an AVI file
  163. //---------------------------------------------------------------------------
  164. {
  165.     MCI_DGV_OPEN_PARMS    mciOpen;
  166.     MCI_DGV_WINDOW_PARMS    mciWindow;
  167.     // MCI_DGV_STATUS_PARMS    mciStatus;
  168.  
  169.     // we have a .AVI movie to open, use MCI 
  170.     // set up the open parameters 
  171.     mciOpen.dwCallback = 0L;
  172.     mciOpen.wDeviceID = 0;
  173.     mciOpen.lpstrDeviceType = NULL;
  174.     mciOpen.lpstrElementName = (char *)pFilename;
  175.     mciOpen.lpstrAlias = NULL;
  176.     mciOpen.dwStyle = WS_CHILD;
  177.     mciOpen.hWndParent = m_hWnd;
  178.  
  179.     // try to open the file - creates a suitable child window
  180.     if (mciSendCommand(0, MCI_OPEN, (DWORD)MCI_OPEN_ELEMENT, (DWORD)&mciOpen) != 0)
  181.         return FALSE;
  182.     
  183.     m_MCIDeviceID = mciOpen.wDeviceID;    /* save ID */
  184.     mciWindow.hWnd = m_hWnd;
  185.     m_strFilename = pFilename;
  186.     if (mciSendCommand(m_MCIDeviceID, MCI_WINDOW, MCI_DGV_WINDOW_HWND, (DWORD) &mciWindow) != 0)
  187.     {
  188.         Close();
  189.          return FALSE;
  190.     }
  191.     m_bFileOpen = TRUE;
  192.     CRect rect;
  193.     GetMovieRect(&rect); 
  194.     SetWindowPos(NULL, 0, 0, rect.Width(), rect.Height(),
  195.                  SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
  196.  
  197.     return TRUE;
  198. }
  199.  
  200.  
  201. //---------------------------------------------------------------------------
  202. BOOL CMRCAnimateCtrl::Close()
  203. //---------------------------------------------------------------------------
  204. {
  205.     if (m_bFileOpen == FALSE)
  206.         return FALSE;
  207.  
  208.     // unshow the window
  209.     MCI_DGV_WINDOW_PARMS mciWindow;
  210.     mciWindow.nCmdShow = SW_HIDE;
  211.     mciSendCommand(m_MCIDeviceID, MCI_WINDOW, (DWORD)MCI_DGV_WINDOW_STATE, (DWORD)&mciWindow);
  212.  
  213.     
  214.     m_bFileOpen = FALSE;
  215.     MCI_GENERIC_PARMS  mciGeneric;
  216.  
  217.     mciSendCommand(m_MCIDeviceID, MCI_CLOSE, 0L, (DWORD)&mciGeneric);
  218.  
  219.     
  220.     // cause a total repaint to occur 
  221.     InvalidateRect(NULL, TRUE);
  222.     UpdateWindow();
  223.     return TRUE;
  224. }
  225.  
  226.  
  227. //---------------------------------------------------------------------------
  228. BOOL CMRCAnimateCtrl::Play(DWORD dwFlags, int nFrom, int nTo, int nRepeatDelay)
  229. //---------------------------------------------------------------------------
  230.     MCI_DGV_PLAY_PARMS    mciPlay;
  231.         
  232.     m_dwPlayFlags = dwFlags;
  233.     m_nFrom = nFrom;
  234.     m_nTo = nTo;
  235.     m_nRepeatDelay = nRepeatDelay;
  236.  
  237.     // init to play all 
  238.     mciPlay.dwCallback = (DWORD)m_hWnd;
  239.     mciPlay.dwFrom = mciPlay.dwTo = 0;
  240.     dwFlags = 0;
  241.     //if (m_nDirection == IDM_RPLAY)
  242.     //    dwFlags |= MCI_DGV_PLAY_REVERSE;
  243.     
  244.     if (m_nFrom >= 0)
  245.     {
  246.         mciPlay.dwFrom = m_nFrom;
  247.         dwFlags |= MCI_FROM;
  248.     }
  249.     
  250.     if (m_nTo >= 0)
  251.     {
  252.         mciPlay.dwTo = m_nTo;
  253.         dwFlags |= MCI_TO;
  254.     }
  255.     
  256.     if (m_dwPlayFlags & ACF_REPEAT)
  257.         if (m_nRepeatDelay == 0)
  258.             dwFlags |= MCI_DGV_PLAY_REPEAT;
  259.         else
  260.             dwFlags |= MCI_NOTIFY;
  261.     
  262.     if (mciSendCommand(m_MCIDeviceID, MCI_PLAY, dwFlags, (DWORD)&mciPlay) != 0)
  263.         return FALSE;
  264.  
  265.     return TRUE;
  266. }
  267.  
  268.  
  269. //---------------------------------------------------------------------------
  270. BOOL CMRCAnimateCtrl::Pause()
  271. //---------------------------------------------------------------------------
  272. {
  273.     MCI_DGV_PAUSE_PARMS    mciPause;
  274.     
  275.     // tell it to pause 
  276.     mciSendCommand(m_MCIDeviceID,MCI_PAUSE,0L,
  277.                      (DWORD)(LPMCI_DGV_PAUSE_PARMS)&mciPause);
  278.  
  279.     return TRUE;
  280. }
  281.  
  282.  
  283.  
  284. //---------------------------------------------------------------------------
  285. BOOL CMRCAnimateCtrl::InitAVI()
  286. //---------------------------------------------------------------------------
  287. {
  288.     MCI_DGV_OPEN_PARMS    mciOpen;
  289.         
  290.     /* set up the open parameters */
  291.     mciOpen.dwCallback         = 0L;
  292.     mciOpen.wDeviceID         = 0;
  293.     mciOpen.lpstrDeviceType     = AVI_VIDEO;
  294.     mciOpen.lpstrElementName     = NULL;
  295.     mciOpen.lpstrAlias         = NULL;
  296.     mciOpen.dwStyle         = 0;
  297.     mciOpen.hWndParent         = NULL;
  298.         
  299.     /* try to open the driver */
  300.     return (mciSendCommand(0, MCI_OPEN, (DWORD)(MCI_OPEN_TYPE),
  301.                           (DWORD)(LPMCI_DGV_OPEN_PARMS)&mciOpen) == 0);
  302. }
  303.  
  304.  
  305. //---------------------------------------------------------------------------
  306. void CMRCAnimateCtrl::Rewind()
  307. //---------------------------------------------------------------------------
  308. {
  309.     MCI_SEEK_PARMS mciSeek;
  310.     mciSeek.dwCallback = (DWORD)m_hWnd;
  311.     mciSeek.dwTo = 0;
  312.  
  313.     if(mciSendCommand(m_MCIDeviceID, MCI_SEEK, MCI_NOTIFY | MCI_SEEK_TO_START,
  314.             (DWORD)&mciSeek))
  315.         TRACE("CMRCAnimateCtrl:Error executing SEEK\n");
  316.  
  317.     TRACE("CMRCAnimateCtrl:MCI Sent Rewind\n");
  318. }
  319.  
  320.  
  321. //---------------------------------------------------------------------------
  322. void CMRCAnimateCtrl::GetMovieRect(RECT * pRect)
  323. // returns rectangle containing size of th
  324. //---------------------------------------------------------------------------
  325. {
  326.     MCI_DGV_RECT_PARMS    mciRect;
  327.  
  328.     // get the original size of the movie 
  329.     mciSendCommand(m_MCIDeviceID, MCI_WHERE,
  330.                   (DWORD)(MCI_DGV_WHERE_SOURCE),
  331.                   (DWORD)&mciRect);
  332.     
  333.     CopyRect(pRect, &mciRect.rc );    // get it in the movie bounds rect 
  334.  
  335. }
  336.  
  337.  
  338.  
  339.  
  340. //---------------------------------------------------------------------------
  341. afx_msg LONG CMRCAnimateCtrl::OnMCINotify(WPARAM wParam, LPARAM lParam)
  342. //---------------------------------------------------------------------------
  343. {
  344.  
  345.     switch (wParam)
  346.     {
  347.     case MCI_NOTIFY_SUCCESSFUL:
  348.         SetTimer(REPEAT_TIMER_ID, m_nRepeatDelay, NULL);
  349.         break;
  350.     
  351.     case MCI_NOTIFY_ABORTED:
  352.         break;
  353.     case MCI_NOTIFY_SUPERSEDED:
  354.         break;
  355.     case MCI_NOTIFY_FAILURE:
  356.         break;
  357.     default:
  358.         break;
  359.     }
  360.  
  361.     return 0;    
  362. }
  363.  
  364.  
  365. //--------------------------------------------------------------------------
  366. void CMRCAnimateCtrl::OnTimer(UINT nIDEvent) 
  367. // Timer event to kick start replaying the video again
  368. //--------------------------------------------------------------------------
  369. {
  370.     ASSERT(nIDEvent == REPEAT_TIMER_ID);
  371.     KillTimer(nIDEvent);
  372.     Rewind();
  373.     Play(m_dwPlayFlags, m_nFrom, m_nTo, m_nRepeatDelay);   
  374.  
  375.     CWnd::OnTimer(nIDEvent);
  376. }
  377.  
  378.  
  379. //--------------------------------------------------------------------------
  380. void CMRCAnimateCtrl::PostNcDestroy()
  381. // auto-delete
  382. //--------------------------------------------------------------------------
  383. {
  384.     delete this;
  385.  
  386. }
  387.  
  388.  
  389. //--------------------------------------------------------------------------
  390. void CMRCAnimateCtrl::OnDestroy()
  391. // auto-delete
  392. //--------------------------------------------------------------------------
  393. {
  394.     Close();
  395. }
  396.  
  397.  
  398.