UIWebBrowser.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. #include "StdAfx.h"
  2. #include "UIWebBrowser.h"
  3. #include <atlconv.h>
  4. #include <atlcomcli.h>
  5. #include "../Utils/downloadmgr.h"
  6. #include <mshtml.h>
  7. namespace DuiLib
  8. {
  9. ////////////////////////////////////////////////////////////////////////
  10. //
  11. IMPLEMENT_DUICONTROL(CWebBrowserUI)
  12. CWebBrowserUI::CWebBrowserUI()
  13. : m_pWebBrowser2(NULL)
  14. , _pHtmlWnd2(NULL)
  15. , m_pWebBrowserEventHandler(NULL)
  16. , m_bAutoNavi(false)
  17. , m_dwRef(0)
  18. , m_dwCookie(0)
  19. {
  20. m_clsid=CLSID_WebBrowser;
  21. m_sHomePage.Empty();
  22. }
  23. bool CWebBrowserUI::DoCreateControl()
  24. {
  25. if (!CActiveXUI::DoCreateControl())
  26. return false;
  27. GetManager()->AddTranslateAccelerator(this);
  28. GetControl(IID_IWebBrowser2,(LPVOID*)&m_pWebBrowser2);
  29. if ( m_bAutoNavi && !m_sHomePage.IsEmpty())
  30. {
  31. this->Navigate2(m_sHomePage);
  32. }
  33. RegisterEventHandler(TRUE);
  34. return true;
  35. }
  36. void CWebBrowserUI::ReleaseControl()
  37. {
  38. m_bCreated=false;
  39. GetManager()->RemoveTranslateAccelerator(this);
  40. RegisterEventHandler(FALSE);
  41. }
  42. CWebBrowserUI::~CWebBrowserUI()
  43. {
  44. ReleaseControl();
  45. }
  46. STDMETHODIMP CWebBrowserUI::GetTypeInfoCount( UINT *iTInfo )
  47. {
  48. *iTInfo = 0;
  49. return E_NOTIMPL;
  50. }
  51. STDMETHODIMP CWebBrowserUI::GetTypeInfo( UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo )
  52. {
  53. return E_NOTIMPL;
  54. }
  55. STDMETHODIMP CWebBrowserUI::GetIDsOfNames( REFIID riid, OLECHAR **rgszNames, UINT cNames, LCID lcid,DISPID *rgDispId )
  56. {
  57. return E_NOTIMPL;
  58. }
  59. STDMETHODIMP CWebBrowserUI::Invoke( DISPID dispIdMember, REFIID riid, LCID lcid,WORD wFlags, DISPPARAMS* pDispParams,VARIANT* pVarResult, EXCEPINFO* pExcepInfo,UINT* puArgErr )
  60. {
  61. if ((riid != IID_NULL))
  62. return E_INVALIDARG;
  63. switch(dispIdMember)
  64. {
  65. case DISPID_BEFORENAVIGATE2:
  66. BeforeNavigate2(
  67. pDispParams->rgvarg[6].pdispVal,
  68. pDispParams->rgvarg[5].pvarVal,
  69. pDispParams->rgvarg[4].pvarVal,
  70. pDispParams->rgvarg[3].pvarVal,
  71. pDispParams->rgvarg[2].pvarVal,
  72. pDispParams->rgvarg[1].pvarVal,
  73. pDispParams->rgvarg[0].pboolVal);
  74. break;
  75. case DISPID_COMMANDSTATECHANGE:
  76. CommandStateChange(
  77. pDispParams->rgvarg[1].lVal,
  78. pDispParams->rgvarg[0].boolVal);
  79. break;
  80. case DISPID_NAVIGATECOMPLETE2:
  81. NavigateComplete2(
  82. pDispParams->rgvarg[1].pdispVal,
  83. pDispParams->rgvarg[0].pvarVal);
  84. break;
  85. case DISPID_NAVIGATEERROR:
  86. NavigateError(
  87. pDispParams->rgvarg[4].pdispVal,
  88. pDispParams->rgvarg[3].pvarVal,
  89. pDispParams->rgvarg[2].pvarVal,
  90. pDispParams->rgvarg[1].pvarVal,
  91. pDispParams->rgvarg[0].pboolVal);
  92. break;
  93. case DISPID_STATUSTEXTCHANGE:
  94. break;
  95. // case DISPID_NEWWINDOW2:
  96. // break;
  97. case DISPID_NEWWINDOW3:
  98. NewWindow3(
  99. pDispParams->rgvarg[4].ppdispVal,
  100. pDispParams->rgvarg[3].pboolVal,
  101. pDispParams->rgvarg[2].uintVal,
  102. pDispParams->rgvarg[1].bstrVal,
  103. pDispParams->rgvarg[0].bstrVal);
  104. break;
  105. case DISPID_TITLECHANGE:
  106. {
  107. TitleChange(pDispParams->rgvarg[0].bstrVal);
  108. break;
  109. }
  110. case DISPID_DOCUMENTCOMPLETE:
  111. {
  112. DocumentComplete(
  113. pDispParams->rgvarg[1].pdispVal,
  114. pDispParams->rgvarg[0].pvarVal);
  115. break;
  116. }
  117. default:
  118. return DISP_E_MEMBERNOTFOUND;
  119. }
  120. return S_OK;
  121. }
  122. STDMETHODIMP CWebBrowserUI::QueryInterface( REFIID riid, LPVOID *ppvObject )
  123. {
  124. *ppvObject = NULL;
  125. if( riid == IID_IDocHostUIHandler)
  126. *ppvObject = static_cast<IDocHostUIHandler*>(this);
  127. else if( riid == IID_IDispatch)
  128. *ppvObject = static_cast<IDispatch*>(this);
  129. else if( riid == IID_IServiceProvider)
  130. *ppvObject = static_cast<IServiceProvider*>(this);
  131. else if(riid == IID_IInternetSecurityManager ) {
  132. *ppvObject = static_cast<IInternetSecurityManager*>(this);
  133. }
  134. else if (riid == IID_IOleCommandTarget)
  135. *ppvObject = static_cast<IOleCommandTarget*>(this);
  136. if( *ppvObject != NULL )
  137. AddRef();
  138. return *ppvObject == NULL ? E_NOINTERFACE : S_OK;
  139. }
  140. STDMETHODIMP_(ULONG) CWebBrowserUI::AddRef()
  141. {
  142. InterlockedIncrement(&m_dwRef);
  143. return m_dwRef;
  144. }
  145. STDMETHODIMP_(ULONG) CWebBrowserUI::Release()
  146. {
  147. ULONG ulRefCount = InterlockedDecrement(&m_dwRef);
  148. return ulRefCount;
  149. }
  150. void CWebBrowserUI::Navigate2( LPCTSTR lpszUrl )
  151. {
  152. if (lpszUrl == NULL)
  153. return;
  154. if (m_pWebBrowser2)
  155. {
  156. CDuiVariant url;
  157. url.vt=VT_BSTR;
  158. url.bstrVal=T2BSTR(lpszUrl);
  159. HRESULT hr = m_pWebBrowser2->Navigate2(&url, NULL, NULL, NULL, NULL);
  160. }
  161. }
  162. void CWebBrowserUI::Refresh()
  163. {
  164. if (m_pWebBrowser2)
  165. {
  166. m_pWebBrowser2->Refresh();
  167. }
  168. }
  169. void CWebBrowserUI::GoBack()
  170. {
  171. if (m_pWebBrowser2)
  172. {
  173. m_pWebBrowser2->GoBack();
  174. }
  175. }
  176. void CWebBrowserUI::GoForward()
  177. {
  178. if (m_pWebBrowser2)
  179. {
  180. m_pWebBrowser2->GoForward();
  181. }
  182. }
  183. /// DWebBrowserEvents2
  184. void CWebBrowserUI::BeforeNavigate2( IDispatch *pDisp,VARIANT *&url,VARIANT *&Flags,VARIANT *&TargetFrameName,VARIANT *&PostData,VARIANT *&Headers,VARIANT_BOOL *&Cancel )
  185. {
  186. if (m_pWebBrowserEventHandler)
  187. {
  188. m_pWebBrowserEventHandler->BeforeNavigate2(this, pDisp,url,Flags,TargetFrameName,PostData,Headers,Cancel);
  189. }
  190. }
  191. void CWebBrowserUI::NavigateError( IDispatch *pDisp,VARIANT * &url,VARIANT *&TargetFrameName,VARIANT *&StatusCode,VARIANT_BOOL *&Cancel )
  192. {
  193. if (m_pWebBrowserEventHandler)
  194. {
  195. m_pWebBrowserEventHandler->NavigateError(this, pDisp,url,TargetFrameName,StatusCode,Cancel);
  196. }
  197. }
  198. void CWebBrowserUI::NavigateComplete2( IDispatch *pDisp,VARIANT *&url )
  199. {
  200. CComPtr<IDispatch> spDoc;
  201. m_pWebBrowser2->get_Document(&spDoc);
  202. if (spDoc)
  203. {
  204. CComQIPtr<ICustomDoc, &IID_ICustomDoc> spCustomDoc(spDoc);
  205. if (spCustomDoc)
  206. spCustomDoc->SetUIHandler(this);
  207. }
  208. if (m_pWebBrowserEventHandler)
  209. {
  210. m_pWebBrowserEventHandler->NavigateComplete2(this, pDisp,url);
  211. }
  212. }
  213. void CWebBrowserUI::ProgressChange( LONG nProgress, LONG nProgressMax )
  214. {
  215. if (m_pWebBrowserEventHandler)
  216. {
  217. m_pWebBrowserEventHandler->ProgressChange(this, nProgress,nProgressMax);
  218. }
  219. }
  220. void CWebBrowserUI::NewWindow3( IDispatch **pDisp, VARIANT_BOOL *&Cancel, DWORD dwFlags, BSTR bstrUrlContext, BSTR bstrUrl )
  221. {
  222. if (m_pWebBrowserEventHandler)
  223. {
  224. m_pWebBrowserEventHandler->NewWindow3(this, pDisp,Cancel,dwFlags,bstrUrlContext,bstrUrl);
  225. }
  226. }
  227. void CWebBrowserUI::CommandStateChange(long Command,VARIANT_BOOL Enable)
  228. {
  229. if (m_pWebBrowserEventHandler)
  230. {
  231. m_pWebBrowserEventHandler->CommandStateChange(this, Command,Enable);
  232. }
  233. }
  234. void CWebBrowserUI::TitleChange(BSTR bstrTitle)
  235. {
  236. if (m_pWebBrowserEventHandler)
  237. {
  238. m_pWebBrowserEventHandler->TitleChange(this, bstrTitle);
  239. }
  240. }
  241. void CWebBrowserUI::DocumentComplete(IDispatch *pDisp, VARIANT *&url)
  242. {
  243. if (m_pWebBrowserEventHandler)
  244. {
  245. m_pWebBrowserEventHandler->DocumentComplete(this, pDisp, url);
  246. }
  247. }
  248. // IDownloadManager
  249. STDMETHODIMP CWebBrowserUI::Download( /* [in] */ IMoniker *pmk, /* [in] */ IBindCtx *pbc, /* [in] */ DWORD dwBindVerb, /* [in] */ LONG grfBINDF, /* [in] */ BINDINFO *pBindInfo, /* [in] */ LPCOLESTR pszHeaders, /* [in] */ LPCOLESTR pszRedir, /* [in] */ UINT uiCP )
  250. {
  251. if (m_pWebBrowserEventHandler)
  252. {
  253. return m_pWebBrowserEventHandler->Download(this, pmk,pbc,dwBindVerb,grfBINDF,pBindInfo,pszHeaders,pszRedir,uiCP);
  254. }
  255. return S_OK;
  256. }
  257. // IDocHostUIHandler
  258. STDMETHODIMP CWebBrowserUI::ShowContextMenu( DWORD dwID, POINT* pptPosition, IUnknown* pCommandTarget, IDispatch* pDispatchObjectHit )
  259. {
  260. if (m_pWebBrowserEventHandler)
  261. {
  262. return m_pWebBrowserEventHandler->ShowContextMenu(this, dwID,pptPosition,pCommandTarget,pDispatchObjectHit);
  263. }
  264. return S_FALSE;
  265. }
  266. STDMETHODIMP CWebBrowserUI::GetHostInfo( DOCHOSTUIINFO* pInfo )
  267. {
  268. if (pInfo != NULL) {
  269. pInfo->dwFlags |= DOCHOSTUIFLAG_NO3DBORDER | DOCHOSTUIFLAG_NO3DOUTERBORDER;
  270. }
  271. if (m_pWebBrowserEventHandler) {
  272. return m_pWebBrowserEventHandler->GetHostInfo(this, pInfo);
  273. }
  274. return S_OK;
  275. }
  276. STDMETHODIMP CWebBrowserUI::ShowUI( DWORD dwID, IOleInPlaceActiveObject* pActiveObject, IOleCommandTarget* pCommandTarget, IOleInPlaceFrame* pFrame, IOleInPlaceUIWindow* pDoc )
  277. {
  278. if (m_pWebBrowserEventHandler)
  279. {
  280. return m_pWebBrowserEventHandler->ShowUI(this, dwID,pActiveObject,pCommandTarget,pFrame,pDoc);
  281. }
  282. return S_OK;
  283. }
  284. STDMETHODIMP CWebBrowserUI::HideUI()
  285. {
  286. if (m_pWebBrowserEventHandler)
  287. {
  288. return m_pWebBrowserEventHandler->HideUI(this);
  289. }
  290. return S_OK;
  291. }
  292. STDMETHODIMP CWebBrowserUI::UpdateUI()
  293. {
  294. if (m_pWebBrowserEventHandler)
  295. {
  296. return m_pWebBrowserEventHandler->UpdateUI(this);
  297. }
  298. return S_OK;
  299. }
  300. STDMETHODIMP CWebBrowserUI::EnableModeless( BOOL fEnable )
  301. {
  302. if (m_pWebBrowserEventHandler)
  303. {
  304. return m_pWebBrowserEventHandler->EnableModeless(this, fEnable);
  305. }
  306. return E_NOTIMPL;
  307. }
  308. STDMETHODIMP CWebBrowserUI::OnDocWindowActivate( BOOL fActivate )
  309. {
  310. if (m_pWebBrowserEventHandler)
  311. {
  312. return m_pWebBrowserEventHandler->OnDocWindowActivate(this, fActivate);
  313. }
  314. return E_NOTIMPL;
  315. }
  316. STDMETHODIMP CWebBrowserUI::OnFrameWindowActivate( BOOL fActivate )
  317. {
  318. if (m_pWebBrowserEventHandler)
  319. {
  320. return m_pWebBrowserEventHandler->OnFrameWindowActivate(this, fActivate);
  321. }
  322. return E_NOTIMPL;
  323. }
  324. STDMETHODIMP CWebBrowserUI::ResizeBorder( LPCRECT prcBorder, IOleInPlaceUIWindow* pUIWindow, BOOL fFrameWindow )
  325. {
  326. if (m_pWebBrowserEventHandler)
  327. {
  328. return m_pWebBrowserEventHandler->ResizeBorder(this, prcBorder,pUIWindow,fFrameWindow);
  329. }
  330. return E_NOTIMPL;
  331. }
  332. STDMETHODIMP CWebBrowserUI::TranslateAccelerator( LPMSG lpMsg, const GUID* pguidCmdGroup, DWORD nCmdID )
  333. {
  334. if (m_pWebBrowserEventHandler)
  335. {
  336. return m_pWebBrowserEventHandler->TranslateAccelerator(this, lpMsg,pguidCmdGroup,nCmdID);
  337. }
  338. return S_FALSE;
  339. }
  340. LRESULT CWebBrowserUI::TranslateAccelerator( MSG *pMsg )
  341. {
  342. if(pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST)
  343. return S_FALSE;
  344. if( m_pWebBrowser2 == NULL )
  345. return E_NOTIMPL;
  346. // 当前Web窗口不是焦点,不处理加速键
  347. BOOL bIsChild = FALSE;
  348. HWND hTempWnd = NULL;
  349. HWND hWndFocus = ::GetFocus();
  350. hTempWnd = hWndFocus;
  351. while(hTempWnd != NULL)
  352. {
  353. if(hTempWnd == m_hwndHost)
  354. {
  355. bIsChild = TRUE;
  356. break;
  357. }
  358. hTempWnd = ::GetParent(hTempWnd);
  359. }
  360. if(!bIsChild)
  361. return S_FALSE;
  362. IOleInPlaceActiveObject *pObj;
  363. if (FAILED(m_pWebBrowser2->QueryInterface(IID_IOleInPlaceActiveObject, (LPVOID *)&pObj)))
  364. return S_FALSE;
  365. HRESULT hResult = pObj->TranslateAccelerator(pMsg);
  366. pObj->Release();
  367. return hResult;
  368. }
  369. STDMETHODIMP CWebBrowserUI::GetOptionKeyPath( LPOLESTR* pchKey, DWORD dwReserved )
  370. {
  371. if (m_pWebBrowserEventHandler)
  372. {
  373. return m_pWebBrowserEventHandler->GetOptionKeyPath(this, pchKey,dwReserved);
  374. }
  375. return E_NOTIMPL;
  376. }
  377. STDMETHODIMP CWebBrowserUI::GetDropTarget( IDropTarget* pDropTarget, IDropTarget** ppDropTarget )
  378. {
  379. if (m_pWebBrowserEventHandler)
  380. {
  381. return m_pWebBrowserEventHandler->GetDropTarget(this, pDropTarget,ppDropTarget);
  382. }
  383. return S_FALSE; // 使用系统拖拽
  384. }
  385. STDMETHODIMP CWebBrowserUI::GetExternal( IDispatch** ppDispatch )
  386. {
  387. if (m_pWebBrowserEventHandler)
  388. {
  389. return m_pWebBrowserEventHandler->GetExternal(this, ppDispatch);
  390. }
  391. return S_FALSE;
  392. }
  393. STDMETHODIMP CWebBrowserUI::TranslateUrl( DWORD dwTranslate, OLECHAR* pchURLIn, OLECHAR** ppchURLOut )
  394. {
  395. if (m_pWebBrowserEventHandler)
  396. {
  397. return m_pWebBrowserEventHandler->TranslateUrl(this, dwTranslate,pchURLIn,ppchURLOut);
  398. }
  399. else
  400. {
  401. *ppchURLOut = 0;
  402. return E_NOTIMPL;
  403. }
  404. }
  405. STDMETHODIMP CWebBrowserUI::FilterDataObject( IDataObject* pDO, IDataObject** ppDORet )
  406. {
  407. if (m_pWebBrowserEventHandler)
  408. {
  409. return m_pWebBrowserEventHandler->FilterDataObject(this, pDO,ppDORet);
  410. }
  411. else
  412. {
  413. *ppDORet = 0;
  414. return E_NOTIMPL;
  415. }
  416. }
  417. void CWebBrowserUI::SetWebBrowserEventHandler( CWebBrowserEventHandler* pEventHandler )
  418. {
  419. if ( pEventHandler!=NULL && m_pWebBrowserEventHandler!=pEventHandler)
  420. m_pWebBrowserEventHandler=pEventHandler;
  421. }
  422. void CWebBrowserUI::Refresh2( int Level )
  423. {
  424. CDuiVariant vLevel;
  425. vLevel.vt=VT_I4;
  426. vLevel.intVal=Level;
  427. m_pWebBrowser2->Refresh2(&vLevel);
  428. }
  429. void CWebBrowserUI::SetAttribute( LPCTSTR pstrName, LPCTSTR pstrValue )
  430. {
  431. if (_tcsicmp(pstrName, _T("homepage")) == 0)
  432. {
  433. m_sHomePage = pstrValue;
  434. }
  435. else if (_tcsicmp(pstrName, _T("autonavi"))==0)
  436. {
  437. m_bAutoNavi = (_tcsicmp(pstrValue, _T("true")) == 0);
  438. }
  439. else
  440. CActiveXUI::SetAttribute(pstrName, pstrValue);
  441. }
  442. void CWebBrowserUI::NavigateHomePage()
  443. {
  444. if (!m_sHomePage.IsEmpty())
  445. this->NavigateUrl(m_sHomePage);
  446. }
  447. void CWebBrowserUI::NavigateUrl( LPCTSTR lpszUrl )
  448. {
  449. if (m_pWebBrowser2 && lpszUrl)
  450. {
  451. m_pWebBrowser2->Navigate((BSTR)SysAllocString(T2BSTR(lpszUrl)),NULL,NULL,NULL,NULL);
  452. }
  453. }
  454. LPCTSTR CWebBrowserUI::GetClass() const
  455. {
  456. return _T("WebBrowserUI");
  457. }
  458. LPVOID CWebBrowserUI::GetInterface( LPCTSTR pstrName )
  459. {
  460. if( _tcsicmp(pstrName, DUI_CTR_WEBBROWSER) == 0 ) return static_cast<CWebBrowserUI*>(this);
  461. return CActiveXUI::GetInterface(pstrName);
  462. }
  463. void CWebBrowserUI::SetHomePage( LPCTSTR lpszUrl )
  464. {
  465. m_sHomePage.Format(_T("%s"),lpszUrl);
  466. }
  467. LPCTSTR CWebBrowserUI::GetHomePage()
  468. {
  469. return m_sHomePage;
  470. }
  471. void CWebBrowserUI::SetAutoNavigation( bool bAuto /*= TRUE*/ )
  472. {
  473. if (m_bAutoNavi==bAuto) return;
  474. m_bAutoNavi=bAuto;
  475. }
  476. bool CWebBrowserUI::IsAutoNavigation()
  477. {
  478. return m_bAutoNavi;
  479. }
  480. STDMETHODIMP CWebBrowserUI::QueryService( REFGUID guidService, REFIID riid, void** ppvObject )
  481. {
  482. HRESULT hr = E_NOINTERFACE;
  483. *ppvObject = NULL;
  484. if ( guidService == SID_SDownloadManager && riid == IID_IDownloadManager)
  485. {
  486. *ppvObject = this;
  487. return S_OK;
  488. }
  489. if(guidService == SID_SInternetSecurityManager && riid == IID_IInternetSecurityManager) {
  490. *ppvObject = this;
  491. return S_OK;
  492. }
  493. return hr;
  494. }
  495. HRESULT CWebBrowserUI::RegisterEventHandler( BOOL inAdvise )
  496. {
  497. CComPtr<IWebBrowser2> pWebBrowser;
  498. CComPtr<IConnectionPointContainer> pCPC;
  499. CComPtr<IConnectionPoint> pCP;
  500. HRESULT hr = GetControl(IID_IWebBrowser2, (void**)&pWebBrowser);
  501. if (FAILED(hr))
  502. return hr;
  503. hr=pWebBrowser->QueryInterface(IID_IConnectionPointContainer,(void **)&pCPC);
  504. if (FAILED(hr))
  505. return hr;
  506. hr=pCPC->FindConnectionPoint(DIID_DWebBrowserEvents2,&pCP);
  507. if (FAILED(hr))
  508. return hr;
  509. if (inAdvise)
  510. {
  511. hr = pCP->Advise((IDispatch*)this, &m_dwCookie);
  512. }
  513. else
  514. {
  515. hr = pCP->Unadvise(m_dwCookie);
  516. }
  517. return hr;
  518. }
  519. DISPID CWebBrowserUI::FindId( IDispatch *pObj, LPOLESTR pName )
  520. {
  521. DISPID id = 0;
  522. if(FAILED(pObj->GetIDsOfNames(IID_NULL,&pName,1,LOCALE_SYSTEM_DEFAULT,&id))) id = -1;
  523. return id;
  524. }
  525. HRESULT CWebBrowserUI::InvokeMethod( IDispatch *pObj, LPOLESTR pMehtod, VARIANT *pVarResult, VARIANT *ps, int cArgs )
  526. {
  527. DISPID dispid = FindId(pObj, pMehtod);
  528. if(dispid == -1) return E_FAIL;
  529. DISPPARAMS dispparams;
  530. dispparams.cArgs = cArgs;
  531. dispparams.rgvarg = ps;
  532. dispparams.cNamedArgs = 0;
  533. dispparams.rgdispidNamedArgs = NULL;
  534. return pObj->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispparams, pVarResult, NULL, NULL);
  535. }
  536. HRESULT CWebBrowserUI::GetProperty( IDispatch *pObj, LPOLESTR pName, VARIANT *pValue )
  537. {
  538. DISPID dispid = FindId(pObj, pName);
  539. if(dispid == -1) return E_FAIL;
  540. DISPPARAMS ps;
  541. ps.cArgs = 0;
  542. ps.rgvarg = NULL;
  543. ps.cNamedArgs = 0;
  544. ps.rgdispidNamedArgs = NULL;
  545. return pObj->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &ps, pValue, NULL, NULL);
  546. }
  547. HRESULT CWebBrowserUI::SetProperty( IDispatch *pObj, LPOLESTR pName, VARIANT *pValue )
  548. {
  549. DISPID dispid = FindId(pObj, pName);
  550. if(dispid == -1) return E_FAIL;
  551. DISPPARAMS ps;
  552. ps.cArgs = 1;
  553. ps.rgvarg = pValue;
  554. ps.cNamedArgs = 0;
  555. ps.rgdispidNamedArgs = NULL;
  556. return pObj->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, &ps, NULL, NULL, NULL);
  557. }
  558. IDispatch* CWebBrowserUI::GetHtmlWindow()
  559. {
  560. IDispatch* pDp = NULL;
  561. HRESULT hr;
  562. if (m_pWebBrowser2)
  563. hr=m_pWebBrowser2->get_Document(&pDp);
  564. if (FAILED(hr))
  565. return NULL;
  566. CComQIPtr<IHTMLDocument2> pHtmlDoc2 = pDp;
  567. if (pHtmlDoc2 == NULL)
  568. return NULL;
  569. hr=pHtmlDoc2->get_parentWindow(&_pHtmlWnd2);
  570. if (FAILED(hr))
  571. return NULL;
  572. IDispatch *pHtmlWindown = NULL;
  573. hr=_pHtmlWnd2->QueryInterface(IID_IDispatch, (void**)&pHtmlWindown);
  574. if (FAILED(hr))
  575. return NULL;
  576. return pHtmlWindown;
  577. }
  578. IWebBrowser2* CWebBrowserUI::GetWebBrowser2( void )
  579. {
  580. return m_pWebBrowser2;
  581. }
  582. HRESULT STDMETHODCALLTYPE CWebBrowserUI::QueryStatus( __RPC__in_opt const GUID *pguidCmdGroup, ULONG cCmds, __RPC__inout_ecount_full(cCmds ) OLECMD prgCmds[ ], __RPC__inout_opt OLECMDTEXT *pCmdText )
  583. {
  584. HRESULT hr = OLECMDERR_E_NOTSUPPORTED;
  585. return hr;
  586. }
  587. HRESULT STDMETHODCALLTYPE CWebBrowserUI::Exec( __RPC__in_opt const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, __RPC__in_opt VARIANT *pvaIn, __RPC__inout_opt VARIANT *pvaOut )
  588. {
  589. HRESULT hr = S_OK;
  590. if (pguidCmdGroup && IsEqualGUID(*pguidCmdGroup, CGID_DocHostCommandHandler))
  591. {
  592. switch (nCmdID)
  593. {
  594. case OLECMDID_SHOWSCRIPTERROR:
  595. {
  596. IHTMLDocument2* pDoc = NULL;
  597. IHTMLWindow2* pWindow = NULL;
  598. IHTMLEventObj* pEventObj = NULL;
  599. BSTR rgwszNames[5] =
  600. {
  601. SysAllocString(L"errorLine"),
  602. SysAllocString(L"errorCharacter"),
  603. SysAllocString(L"errorCode"),
  604. SysAllocString(L"errorMessage"),
  605. SysAllocString(L"errorUrl")
  606. };
  607. DISPID rgDispIDs[5];
  608. VARIANT rgvaEventInfo[5];
  609. DISPPARAMS params;
  610. BOOL fContinueRunningScripts = true;
  611. int i;
  612. params.cArgs = 0;
  613. params.cNamedArgs = 0;
  614. // Get the document that is currently being viewed.
  615. hr = pvaIn->punkVal->QueryInterface(IID_IHTMLDocument2, (void **) &pDoc);
  616. // Get document.parentWindow.
  617. hr = pDoc->get_parentWindow(&pWindow);
  618. pDoc->Release();
  619. // Get the window.event object.
  620. hr = pWindow->get_event(&pEventObj);
  621. // Get the error info from the window.event object.
  622. for (i = 0; i < 5; i++)
  623. {
  624. // Get the property's dispID.
  625. hr = pEventObj->GetIDsOfNames(IID_NULL, &rgwszNames[i], 1,
  626. LOCALE_SYSTEM_DEFAULT, &rgDispIDs[i]);
  627. // Get the value of the property.
  628. hr = pEventObj->Invoke(rgDispIDs[i], IID_NULL,
  629. LOCALE_SYSTEM_DEFAULT,
  630. DISPATCH_PROPERTYGET, &params, &rgvaEventInfo[i],
  631. NULL, NULL);
  632. SysFreeString(rgwszNames[i]);
  633. }
  634. // At this point, you would normally alert the user with
  635. // the information about the error, which is now contained
  636. // in rgvaEventInfo[]. Or, you could just exit silently.
  637. (*pvaOut).vt = VT_BOOL;
  638. if (fContinueRunningScripts)
  639. {
  640. // Continue running scripts on the page.
  641. (*pvaOut).boolVal = VARIANT_TRUE;
  642. }
  643. else
  644. {
  645. // Stop running scripts on the page.
  646. (*pvaOut).boolVal = VARIANT_FALSE;
  647. }
  648. break;
  649. }
  650. default:
  651. hr = OLECMDERR_E_NOTSUPPORTED;
  652. break;
  653. }
  654. }
  655. else
  656. {
  657. hr = OLECMDERR_E_UNKNOWNGROUP;
  658. }
  659. return (hr);
  660. }
  661. }