home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 October / Chip_2002-10_cd1.bin / zkuste / vbasic / Data / Utils / WMP71SDK.exe / CWMPHost.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-01  |  14.1 KB  |  545 lines

  1. // CWMPHost.cpp : Implementation of the CWMPHost
  2. //
  3. // Copyright (C) 1996-2001 Microsoft Corporation
  4. // All rights reserved.
  5. //
  6.  
  7. #include "stdafx.h"
  8. #include "CWMPHost.h"
  9. #include "dialogs.h"
  10.  
  11.  
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CWMPHost
  14.  
  15. void CWMPHost::OnFinalMessage(HWND /*hWnd*/)
  16. {
  17.     ::PostQuitMessage(0);
  18. }
  19.  
  20. LRESULT CWMPHost::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  21. {
  22.     AtlAxWinInit();
  23.     CComPtr<IAxWinHostWindow>           spHost;
  24.     CComPtr<IConnectionPointContainer>  spConnectionContainer;
  25.     CComWMPEventDispatch                *pEventListener = NULL;
  26.     CComPtr<IDispatch>                  spDispEventListener;
  27.     HRESULT                             hr;
  28.     RECT                                rcClient;
  29.  
  30.     m_dwAdviseCookie = 0;
  31.  
  32.     // create window
  33.  
  34.     GetClientRect(&rcClient);
  35.     m_wndView.Create(m_hWnd, rcClient, _T("WMP Host"), WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);
  36.     if (NULL == m_wndView.m_hWnd)
  37.         goto FAILURE;
  38.     
  39.     // load OCX in window
  40.  
  41.     hr = m_wndView.QueryHost(&spHost);
  42.     if (FAILMSG(hr))
  43.         goto FAILURE;
  44.  
  45.     hr = spHost->CreateControl(CComBSTR(_T("{6BF52A52-394A-11d3-B153-00C04F79FAA6}")), m_wndView, 0);
  46.     if (FAILMSG(hr))
  47.         goto FAILURE;
  48.  
  49.     hr = m_wndView.QueryControl(&m_spWMPPlayer);
  50.     if (FAILMSG(hr))
  51.         goto FAILURE;
  52.  
  53.     // start listening to events
  54.  
  55.     hr = CComWMPEventDispatch::CreateInstance(&pEventListener);
  56.     spDispEventListener = pEventListener;
  57.     if (FAILMSG(hr))
  58.         goto FAILURE;
  59.  
  60.     hr = m_spWMPPlayer->QueryInterface( &spConnectionContainer );
  61.     if (FAILMSG(hr))
  62.         goto FAILURE;
  63.  
  64.     hr = spConnectionContainer->FindConnectionPoint( __uuidof(_WMPOCXEvents), &m_spConnectionPoint );
  65.     if (FAILMSG(hr))
  66.         goto FAILURE;
  67.  
  68.     hr = m_spConnectionPoint->Advise( spDispEventListener, &m_dwAdviseCookie );
  69.     if (FAILMSG(hr))
  70.         goto FAILURE;
  71.  
  72.     return 0;
  73.  
  74. FAILURE:
  75.     ::PostQuitMessage(0);
  76.     return 0;
  77. }
  78.  
  79. LRESULT CWMPHost::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  80. {
  81.     // stop listening to events
  82.  
  83.     if (m_spConnectionPoint)
  84.     {
  85.         if (0 != m_dwAdviseCookie)
  86.             m_spConnectionPoint->Unadvise(m_dwAdviseCookie);
  87.         m_spConnectionPoint.Release();
  88.     }
  89.  
  90.     // close the OCX
  91.  
  92.     if (m_spWMPPlayer)
  93.     {
  94.         m_spWMPPlayer->close();
  95.         m_spWMPPlayer.Release();
  96.     }
  97.  
  98.     bHandled = FALSE;
  99.     return 0;
  100. }
  101.  
  102. LRESULT CWMPHost::OnErase(UINT /* uMsg */, WPARAM /* wParam */, LPARAM /* lParam */, BOOL& bHandled)
  103. {
  104.     return 1;
  105. }
  106.  
  107. LRESULT CWMPHost::OnSize(UINT /* uMsg */, WPARAM /* wParam */, LPARAM /* lParam */, BOOL& /* lResult */)
  108. {
  109.     RECT rcClient;
  110.     GetClientRect(&rcClient);
  111.     m_wndView.MoveWindow(rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
  112.     return 0;
  113. }
  114.  
  115. LRESULT CWMPHost::OnFileOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  116. {
  117.     CFileOpenDlg dlgOpen;
  118.     HRESULT      hr;
  119.  
  120.     if (dlgOpen.DoModal(m_hWnd) == IDOK)
  121.     {
  122.         hr = m_spWMPPlayer->put_URL(dlgOpen.m_bstrName);
  123.         if (FAILMSG(hr))
  124.             return 0;
  125.     }
  126.     return 0;
  127. }
  128.  
  129. LRESULT CWMPHost::OnFileExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  130. {
  131.     DestroyWindow();
  132.     return 0;
  133. }
  134.  
  135. LRESULT CWMPHost::OnWMPCoreClose(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  136. {
  137.     HRESULT     hr;
  138.  
  139.     hr = m_spWMPPlayer->close();
  140.     if (FAILMSG(hr))
  141.         return 0;
  142.  
  143.     return 0;
  144. }
  145.  
  146. LRESULT CWMPHost::OnWMPCoreURL(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  147. {
  148.     USES_CONVERSION;
  149.     CComBSTR    m_bstrValue;
  150.     HRESULT     hr;
  151.  
  152.     hr = m_spWMPPlayer->get_URL(&m_bstrValue);
  153.     if (FAILMSG(hr))
  154.         return 0;
  155.  
  156.     CStringDlg dlgString(_T("IWMPCore->URL"), OLE2T(m_bstrValue));
  157.  
  158.     if (dlgString.DoModal(m_hWnd) == IDOK)
  159.     {
  160.         hr = m_spWMPPlayer->put_URL(dlgString.m_bstrValue);
  161.         if (FAILMSG(hr))
  162.             return 0;
  163.     }
  164.     return 0;
  165. }
  166.  
  167. LRESULT CWMPHost::OnWMPCoreOpenState(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  168. {
  169.     HRESULT      hr;
  170.     WMPOpenState osValue;
  171.  
  172.     hr = m_spWMPPlayer->get_openState(&osValue);
  173.     if (FAILMSG(hr))
  174.         return 0;
  175.  
  176.     TCHAR   szValue[MAX_STRING];
  177.  
  178.     ::wsprintf(szValue, _T("Value = %d"), osValue);
  179.     ::MessageBox(NULL, szValue, _T("IWMPCore->openState"), MB_OK);
  180.  
  181.     return 0;
  182. }
  183.  
  184. LRESULT CWMPHost::OnWMPCorePlayState(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  185. {
  186.     HRESULT      hr;
  187.     WMPPlayState psValue;
  188.  
  189.     hr = m_spWMPPlayer->get_playState(&psValue);
  190.     if (FAILMSG(hr))
  191.         return 0;
  192.  
  193.     TCHAR   szValue[MAX_STRING];
  194.  
  195.     ::wsprintf(szValue, _T("Value = %d"), psValue);
  196.     ::MessageBox(NULL, szValue, _T("IWMPCore->playState"), MB_OK);
  197.  
  198.     return 0;
  199. }
  200.  
  201. LRESULT CWMPHost::OnWMPCoreVersionInfo(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  202. {
  203.     USES_CONVERSION;
  204.     CComBSTR    m_bstrValue;
  205.     HRESULT     hr;
  206.  
  207.     hr = m_spWMPPlayer->get_versionInfo(&m_bstrValue);
  208.     if (FAILMSG(hr))
  209.         return 0;
  210.  
  211.     TCHAR   szValue[MAX_STRING];
  212.  
  213.     ::wsprintf(szValue, _T("Version = %s"), OLE2T(m_bstrValue));
  214.     ::MessageBox(NULL, szValue, _T("IWMPCore->versionInfo"), MB_OK);
  215.  
  216.     return 0;
  217. }
  218.  
  219. LRESULT CWMPHost::OnWMPCoreLaunchURL(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  220. {
  221.     CComBSTR    m_bstrValue;
  222.     HRESULT     hr;
  223.  
  224.     CStringDlg dlgString(_T("IWMPCore->LaunchURL"));
  225.  
  226.     if (dlgString.DoModal(m_hWnd) == IDOK)
  227.     {
  228.         hr = m_spWMPPlayer->launchURL(dlgString.m_bstrValue);
  229.         if (FAILMSG(hr))
  230.             return 0;
  231.     }
  232.     return 0;
  233. }
  234.  
  235. LRESULT CWMPHost::OnWMPCoreIsOnline(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  236. {
  237.     HRESULT      hr;
  238.     VARIANT_BOOL fValue;
  239.  
  240.     hr = m_spWMPPlayer->get_isOnline(&fValue);
  241.     if (FAILMSG(hr))
  242.         return 0;
  243.  
  244.     TCHAR   szValue[MAX_STRING];
  245.  
  246.     ::wsprintf(szValue, _T("Value = %s"), fValue ? _T("TRUE") : _T("FALSE"));
  247.     ::MessageBox(NULL, szValue, _T("IWMPCore->isOnline"), MB_OK);
  248.  
  249.     return 0;
  250. }
  251.  
  252. LRESULT CWMPHost::OnWMPCoreStatus(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  253. {
  254.     USES_CONVERSION;
  255.     CComBSTR    m_bstrValue;
  256.     HRESULT     hr;
  257.  
  258.     hr = m_spWMPPlayer->get_status(&m_bstrValue);
  259.     if (FAILMSG(hr))
  260.         return 0;
  261.  
  262.     TCHAR   szValue[MAX_STRING];
  263.  
  264.     ::wsprintf(szValue, _T("Status = %s"), OLE2T(m_bstrValue));
  265.     ::MessageBox(NULL, szValue, _T("IWMPCore->status"), MB_OK);
  266.  
  267.     return 0;
  268. }
  269.  
  270. LRESULT CWMPHost::OnWMPCoreInterface(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  271. {
  272.     HRESULT     hr;
  273.     TCHAR       szName[MAX_STRING];
  274.     void        *pUnknown;
  275.  
  276.     hr = E_FAIL;
  277.     szName[0] = _T('\0');
  278.     pUnknown = NULL;
  279.  
  280.     switch (wID)
  281.     {
  282.     case ID_WMPCORE_CONTROLS:
  283.         {
  284.             CComPtr<IWMPControls> spWMPControls;
  285.  
  286.             hr = m_spWMPPlayer->get_controls(&spWMPControls);
  287.             if (spWMPControls)
  288.             {
  289.                 spWMPControls->QueryInterface(__uuidof(IWMPControls), &pUnknown);
  290.                 lstrcpy(szName, _T("IWMPCore->controls"));
  291.             }
  292.         }
  293.         break;
  294.     case ID_WMPCORE_SETTINGS:
  295.         {
  296.             CComPtr<IWMPSettings> spWMPSettings;
  297.  
  298.             hr = m_spWMPPlayer->get_settings(&spWMPSettings);
  299.             if (spWMPSettings)
  300.             {
  301.                 spWMPSettings->QueryInterface(__uuidof(IWMPSettings), &pUnknown);
  302.                 lstrcpy(szName, _T("IWMPCore->settings"));
  303.             }
  304.         }
  305.         break;
  306.     case ID_WMPCORE_CURRENTMEDIA:
  307.         {
  308.             CComPtr<IWMPMedia> spWMPMedia;
  309.  
  310.             hr = m_spWMPPlayer->get_currentMedia(&spWMPMedia);
  311.             if (spWMPMedia)
  312.             {
  313.                 spWMPMedia->QueryInterface(__uuidof(IWMPMedia), &pUnknown);
  314.                 lstrcpy(szName, _T("IWMPCore->currentMedia"));
  315.             }
  316.         }
  317.         break;
  318.     case ID_WMPCORE_MEDIACOLLECTION:
  319.         {
  320.             CComPtr<IWMPMediaCollection> spWMPMediaCollection;
  321.  
  322.             hr = m_spWMPPlayer->get_mediaCollection(&spWMPMediaCollection);
  323.             if (spWMPMediaCollection)
  324.             {
  325.                 spWMPMediaCollection->QueryInterface(__uuidof(IWMPMediaCollection), &pUnknown);
  326.                 lstrcpy(szName, _T("IWMPCore->currentMediaCollection"));
  327.             }
  328.         }
  329.         break;
  330.     case ID_WMPCORE_PLAYLISTCOLLECTION:
  331.         {
  332.             CComPtr<IWMPPlaylistCollection> spWMPPlaylistCollection;
  333.  
  334.             hr = m_spWMPPlayer->get_playlistCollection(&spWMPPlaylistCollection);
  335.             if (spWMPPlaylistCollection)
  336.             {
  337.                 spWMPPlaylistCollection->QueryInterface(__uuidof(IWMPPlaylistCollection), &pUnknown);
  338.                 lstrcpy(szName, _T("IWMPCore->playlistCollection"));
  339.             }
  340.         }
  341.         break;
  342.     case ID_WMPCORE_NETWORK:
  343.         {
  344.             CComPtr<IWMPNetwork> spWMPNetwork;
  345.  
  346.             hr = m_spWMPPlayer->get_network(&spWMPNetwork);
  347.             if (spWMPNetwork)
  348.             {
  349.                 spWMPNetwork->QueryInterface(__uuidof(IWMPNetwork), &pUnknown);
  350.                 lstrcpy(szName, _T("IWMPCore->network"));
  351.             }
  352.         }
  353.         break;
  354.     case ID_WMPCORE_CURRENTPLAYLIST:
  355.         {
  356.             CComPtr<IWMPPlaylist> spWMPPlaylist;
  357.  
  358.             hr = m_spWMPPlayer->get_currentPlaylist(&spWMPPlaylist);
  359.             if (spWMPPlaylist)
  360.             {
  361.                 spWMPPlaylist->QueryInterface(__uuidof(IWMPPlaylist), &pUnknown);
  362.                 lstrcpy(szName, _T("IWMPCore->currentPlaylist"));
  363.             }
  364.         }
  365.         break;
  366.     case ID_WMPCORE_CDROMCOLLECTION:
  367.         {
  368.             CComPtr<IWMPCdromCollection> spWMPCDRomCollection;
  369.  
  370.             hr = m_spWMPPlayer->get_cdromCollection(&spWMPCDRomCollection);
  371.             if (spWMPCDRomCollection)
  372.             {
  373.                 spWMPCDRomCollection->QueryInterface(__uuidof(IWMPCdromCollection), &pUnknown);
  374.                 lstrcpy(szName, _T("IWMPCore->cdromCollection"));
  375.             }
  376.         }
  377.         break;
  378.     case ID_WMPCORE_CLOSEDCAPTION:
  379.         {
  380.             CComPtr<IWMPClosedCaption> spWMPClosedCaption;
  381.  
  382.             hr = m_spWMPPlayer->get_closedCaption(&spWMPClosedCaption);
  383.             if (spWMPClosedCaption)
  384.             {
  385.                 spWMPClosedCaption->QueryInterface(__uuidof(IWMPClosedCaption), &pUnknown);
  386.                 lstrcpy(szName, _T("IWMPCore->closedCaption"));
  387.             }
  388.         }
  389.         break;
  390.     case ID_WMPCORE_ERROR:
  391.         {
  392.             CComPtr<IWMPError> spWMPError;
  393.  
  394.             hr = m_spWMPPlayer->get_error(&spWMPError);
  395.             if (spWMPError)
  396.             {
  397.                 spWMPError->QueryInterface(__uuidof(IWMPError), &pUnknown);
  398.                 lstrcpy(szName, _T("IWMPCore->error"));
  399.             }
  400.         }
  401.         break;
  402.     }
  403.  
  404.     if (FAILMSG(hr))
  405.         return 0;
  406.  
  407.     if (!pUnknown)
  408.     {
  409.         FAILMSG(E_NOINTERFACE);
  410.         return 0;
  411.     }
  412.  
  413.     ((IUnknown *)pUnknown)->Release();
  414.  
  415.     ::MessageBox(NULL, _T("Got the expected interface"), szName, MB_OK);
  416.  
  417.     return 0;
  418. }
  419.  
  420. LRESULT CWMPHost::OnWMPPlayerEnabled(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  421. {
  422.     HRESULT      hr;
  423.     VARIANT_BOOL fValue;
  424.  
  425.     hr = m_spWMPPlayer->get_enabled(&fValue);
  426.     if (FAILMSG(hr))
  427.         return 0;
  428.  
  429.     CBooleanDlg dlgBoolean(_T("IWMPPlayer->enabled"), fValue);
  430.  
  431.     if (dlgBoolean.DoModal(m_hWnd) == IDOK)
  432.     {
  433.         hr = m_spWMPPlayer->put_enabled(dlgBoolean.m_fValue);
  434.         if (FAILMSG(hr))
  435.             return 0;
  436.     }
  437.  
  438.     return 0;
  439. }
  440.  
  441. LRESULT CWMPHost::OnWMPPlayerFullScreen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  442. {
  443.     HRESULT      hr;
  444.     VARIANT_BOOL fValue;
  445.  
  446.     hr = m_spWMPPlayer->get_fullScreen(&fValue);
  447.     if (FAILMSG(hr))
  448.         return 0;
  449.  
  450.     CBooleanDlg dlgBoolean(_T("IWMPPlayer->fullScreen"), fValue);
  451.  
  452.     if (dlgBoolean.DoModal(m_hWnd) == IDOK)
  453.     {
  454.         hr = m_spWMPPlayer->put_fullScreen(dlgBoolean.m_fValue);
  455.         if (FAILMSG(hr))
  456.             return 0;
  457.     }
  458.  
  459.     return 0;
  460. }
  461.  
  462. LRESULT CWMPHost::OnWMPPlayerEnableContextMenu(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  463. {
  464.     HRESULT      hr;
  465.     VARIANT_BOOL fValue;
  466.  
  467.     hr = m_spWMPPlayer->get_enableContextMenu(&fValue);
  468.     if (FAILMSG(hr))
  469.         return 0;
  470.  
  471.     CBooleanDlg dlgBoolean(_T("IWMPPlayer->enableContextMenu"), fValue);
  472.  
  473.     if (dlgBoolean.DoModal(m_hWnd) == IDOK)
  474.     {
  475.         hr = m_spWMPPlayer->put_enableContextMenu(dlgBoolean.m_fValue);
  476.         if (FAILMSG(hr))
  477.             return 0;
  478.     }
  479.  
  480.     return 0;
  481. }
  482.  
  483. LRESULT CWMPHost::OnWMPPlayerUIMode(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  484. {
  485.     USES_CONVERSION;
  486.     CComBSTR    m_bstrValue;
  487.     HRESULT     hr;
  488.  
  489.     hr = m_spWMPPlayer->get_uiMode(&m_bstrValue);
  490.     if (FAILMSG(hr))
  491.         return 0;
  492.  
  493.     CStringDlg dlgString(_T("IWMPCore->uiMode"), OLE2T(m_bstrValue));
  494.  
  495.     if (dlgString.DoModal(m_hWnd) == IDOK)
  496.     {
  497.         hr = m_spWMPPlayer->put_uiMode(dlgString.m_bstrValue);
  498.         if (FAILMSG(hr))
  499.             return 0;
  500.     }
  501.     return 0;
  502. }
  503.  
  504. LRESULT CWMPHost::OnWMPPlayer2StretchToFit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  505. {
  506.     HRESULT      hr;
  507.     VARIANT_BOOL fValue;
  508.     CComPtr<IWMPPlayer2> spWMPPlayer2;
  509.  
  510.     hr = m_spWMPPlayer.QueryInterface(&spWMPPlayer2);
  511.     if (FAILMSG(hr))
  512.         return 0;
  513.  
  514.     hr = spWMPPlayer2->get_stretchToFit(&fValue);
  515.     if (FAILMSG(hr))
  516.         return 0;
  517.  
  518.     CBooleanDlg dlgBoolean(_T("IWMPPlayer2->stretchToFit"), fValue);
  519.  
  520.     if (dlgBoolean.DoModal(m_hWnd) == IDOK)
  521.     {
  522.         hr = spWMPPlayer2->put_stretchToFit(dlgBoolean.m_fValue);
  523.         if (FAILMSG(hr))
  524.             return 0;
  525.     }
  526.  
  527.     return 0;
  528. }
  529.  
  530. LRESULT CWMPHost::FowardMsgToWMP(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  531. {
  532.     HRESULT hr;
  533.     LRESULT llResult = 0;
  534.     CComPtr<IOleInPlaceObjectWindowless> spSite = NULL;
  535.  
  536.     hr = m_spWMPPlayer->QueryInterface(&spSite);
  537.     if( SUCCEEDED(hr) )
  538.     {
  539.         spSite->OnWindowMessage(uMsg, wParam, lParam, &llResult);
  540.     }
  541.     bHandled = TRUE;
  542.  
  543.     return llResult;
  544. }
  545.