UIFlash.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "StdAfx.h"
  2. #include "UIFlash.h"
  3. #include <atlcomcli.h>
  4. #define DISPID_FLASHEVENT_FLASHCALL ( 0x00C5 )
  5. #define DISPID_FLASHEVENT_FSCOMMAND ( 0x0096 )
  6. #define DISPID_FLASHEVENT_ONPROGRESS ( 0x07A6 )
  7. namespace DuiLib
  8. {
  9. IMPLEMENT_DUICONTROL(CFlashUI)
  10. CFlashUI::CFlashUI(void)
  11. : m_dwRef(0)
  12. , m_dwCookie(0)
  13. , m_pFlash(NULL)
  14. , m_pFlashEventHandler(NULL)
  15. {
  16. CDuiString strFlashCLSID=_T("{D27CDB6E-AE6D-11CF-96B8-444553540000}");
  17. OLECHAR szCLSID[100] = { 0 };
  18. #ifndef _UNICODE
  19. ::MultiByteToWideChar(::GetACP(), 0, strFlashCLSID, -1, szCLSID, lengthof(szCLSID) - 1);
  20. #else
  21. _tcsncpy(szCLSID, strFlashCLSID, lengthof(szCLSID) - 1);
  22. #endif
  23. ::CLSIDFromString(szCLSID, &m_clsid);
  24. }
  25. CFlashUI::~CFlashUI(void)
  26. {
  27. if (m_pFlashEventHandler)
  28. {
  29. m_pFlashEventHandler->Release();
  30. m_pFlashEventHandler=NULL;
  31. }
  32. ReleaseControl();
  33. }
  34. LPCTSTR CFlashUI::GetClass() const
  35. {
  36. return _T("FlashUI");
  37. }
  38. LPVOID CFlashUI::GetInterface( LPCTSTR pstrName )
  39. {
  40. if( _tcsicmp(pstrName, DUI_CTR_FLASH) == 0 ) return static_cast<CFlashUI*>(this);
  41. return CActiveXUI::GetInterface(pstrName);
  42. }
  43. HRESULT STDMETHODCALLTYPE CFlashUI::GetTypeInfoCount( __RPC__out UINT *pctinfo )
  44. {
  45. return E_NOTIMPL;
  46. }
  47. HRESULT STDMETHODCALLTYPE CFlashUI::GetTypeInfo( UINT iTInfo, LCID lcid, __RPC__deref_out_opt ITypeInfo **ppTInfo )
  48. {
  49. return E_NOTIMPL;
  50. }
  51. HRESULT STDMETHODCALLTYPE CFlashUI::GetIDsOfNames( __RPC__in REFIID riid, __RPC__in_ecount_full(cNames ) LPOLESTR *rgszNames, UINT cNames, LCID lcid, __RPC__out_ecount_full(cNames) DISPID *rgDispId )
  52. {
  53. return E_NOTIMPL;
  54. }
  55. HRESULT STDMETHODCALLTYPE CFlashUI::Invoke( DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr )
  56. {
  57. return S_OK;
  58. switch(dispIdMember)
  59. {
  60. case DISPID_FLASHEVENT_FLASHCALL:
  61. {
  62. if (pDispParams->cArgs != 1 || pDispParams->rgvarg[0].vt != VT_BSTR)
  63. return E_INVALIDARG;
  64. return this->FlashCall(pDispParams->rgvarg[0].bstrVal);
  65. }
  66. case DISPID_FLASHEVENT_FSCOMMAND:
  67. {
  68. if( pDispParams && pDispParams->cArgs == 2 )
  69. {
  70. if( pDispParams->rgvarg[0].vt == VT_BSTR &&
  71. pDispParams->rgvarg[1].vt == VT_BSTR )
  72. {
  73. return FSCommand(pDispParams->rgvarg[1].bstrVal, pDispParams->rgvarg[0].bstrVal);
  74. }
  75. else
  76. {
  77. return DISP_E_TYPEMISMATCH;
  78. }
  79. }
  80. else
  81. {
  82. return DISP_E_BADPARAMCOUNT;
  83. }
  84. }
  85. case DISPID_FLASHEVENT_ONPROGRESS:
  86. {
  87. return OnProgress(*pDispParams->rgvarg[0].plVal);
  88. }
  89. case DISPID_READYSTATECHANGE:
  90. {
  91. return this->OnReadyStateChange(pDispParams->rgvarg[0].lVal);
  92. }
  93. }
  94. return S_OK;
  95. }
  96. HRESULT STDMETHODCALLTYPE CFlashUI::QueryInterface( REFIID riid, void **ppvObject )
  97. {
  98. *ppvObject = NULL;
  99. if( riid == IID_IUnknown)
  100. *ppvObject = static_cast</*IOleWindow**/LPUNKNOWN>(this);
  101. else if( riid == IID_IDispatch)
  102. *ppvObject = static_cast<IDispatch*>(this);
  103. else if( riid == __uuidof(_IShockwaveFlashEvents))
  104. *ppvObject = static_cast<_IShockwaveFlashEvents*>(this);
  105. if( *ppvObject != NULL )
  106. AddRef();
  107. return *ppvObject == NULL ? E_NOINTERFACE : S_OK;
  108. }
  109. ULONG STDMETHODCALLTYPE CFlashUI::AddRef( void )
  110. {
  111. ::InterlockedIncrement(&m_dwRef);
  112. return m_dwRef;
  113. }
  114. ULONG STDMETHODCALLTYPE CFlashUI::Release( void )
  115. {
  116. ::InterlockedDecrement(&m_dwRef);
  117. return m_dwRef;
  118. }
  119. HRESULT CFlashUI::OnReadyStateChange (long newState)
  120. {
  121. if (m_pFlashEventHandler)
  122. {
  123. return m_pFlashEventHandler->OnReadyStateChange(newState);
  124. }
  125. return S_OK;
  126. }
  127. HRESULT CFlashUI::OnProgress(long percentDone )
  128. {
  129. if (m_pFlashEventHandler)
  130. {
  131. return m_pFlashEventHandler->OnProgress(percentDone);
  132. }
  133. return S_OK;
  134. }
  135. HRESULT CFlashUI::FSCommand (_bstr_t command, _bstr_t args)
  136. {
  137. if (m_pFlashEventHandler)
  138. {
  139. return m_pFlashEventHandler->FSCommand(command,args);
  140. }
  141. return S_OK;
  142. }
  143. HRESULT CFlashUI::FlashCall( _bstr_t request )
  144. {
  145. if (m_pFlashEventHandler)
  146. {
  147. return m_pFlashEventHandler->FlashCall(request);
  148. }
  149. return S_OK;
  150. }
  151. void CFlashUI::ReleaseControl()
  152. {
  153. //GetManager()->RemoveTranslateAccelerator(this);
  154. RegisterEventHandler(FALSE);
  155. if (m_pFlash)
  156. {
  157. m_pFlash->Release();
  158. m_pFlash=NULL;
  159. }
  160. }
  161. bool CFlashUI::DoCreateControl()
  162. {
  163. if (!CActiveXUI::DoCreateControl())
  164. return false;
  165. //GetManager()->AddTranslateAccelerator(this);
  166. GetControl(__uuidof(IShockwaveFlash),(LPVOID*)&m_pFlash);
  167. RegisterEventHandler(TRUE);
  168. return true;
  169. }
  170. void CFlashUI::SetFlashEventHandler( CFlashEventHandler* pHandler )
  171. {
  172. if (m_pFlashEventHandler!=NULL)
  173. {
  174. m_pFlashEventHandler->Release();
  175. }
  176. if (pHandler==NULL)
  177. {
  178. m_pFlashEventHandler=pHandler;
  179. return;
  180. }
  181. m_pFlashEventHandler=pHandler;
  182. m_pFlashEventHandler->AddRef();
  183. }
  184. LRESULT CFlashUI::TranslateAccelerator( MSG *pMsg )
  185. {
  186. if(pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST)
  187. return S_FALSE;
  188. if( m_pFlash == NULL )
  189. return E_NOTIMPL;
  190. // 当前Web窗口不是焦点,不处理加速键
  191. BOOL bIsChild = FALSE;
  192. HWND hTempWnd = NULL;
  193. HWND hWndFocus = ::GetFocus();
  194. hTempWnd = hWndFocus;
  195. while(hTempWnd != NULL)
  196. {
  197. if(hTempWnd == m_hwndHost)
  198. {
  199. bIsChild = TRUE;
  200. break;
  201. }
  202. hTempWnd = ::GetParent(hTempWnd);
  203. }
  204. if(!bIsChild)
  205. return S_FALSE;
  206. CComPtr<IOleInPlaceActiveObject> pObj;
  207. if (FAILED(m_pFlash->QueryInterface(IID_IOleInPlaceActiveObject, (LPVOID *)&pObj)))
  208. return S_FALSE;
  209. HRESULT hResult = pObj->TranslateAccelerator(pMsg);
  210. return hResult;
  211. }
  212. HRESULT CFlashUI::RegisterEventHandler( BOOL inAdvise )
  213. {
  214. if (m_pFlash==NULL)
  215. return S_FALSE;
  216. HRESULT hr=S_FALSE;
  217. CComPtr<IConnectionPointContainer> pCPC;
  218. CComPtr<IConnectionPoint> pCP;
  219. hr=m_pFlash->QueryInterface(IID_IConnectionPointContainer,(void **)&pCPC);
  220. if (FAILED(hr))
  221. return hr;
  222. hr=pCPC->FindConnectionPoint(__uuidof(_IShockwaveFlashEvents),&pCP);
  223. if (FAILED(hr))
  224. return hr;
  225. if (inAdvise)
  226. {
  227. hr = pCP->Advise((IDispatch*)this, &m_dwCookie);
  228. }
  229. else
  230. {
  231. hr = pCP->Unadvise(m_dwCookie);
  232. }
  233. return hr;
  234. }
  235. };