HRESULT vmsDomHelper::GetWindowDocument(IHTMLWindow2* pWnd, IHTMLDocument2** ppDoc)
{
	assert (pWnd != NULL && ppDoc != NULL);
	if (!ppDoc)
		return E_INVALIDARG;
	*ppDoc = NULL;
	if (!pWnd)
		return E_INVALIDARG;

	HRESULT hr = pWnd->get_document (ppDoc);

	if (*ppDoc)
		return hr;

	IWebBrowser2Ptr spWB;
	GetWebBrowser (pWnd, &spWB);

	if (spWB != NULL)
	{
		IDispatchPtr spdDoc;
		spWB->get_Document (&spdDoc);
		if (spdDoc != NULL)
		{
			spdDoc->QueryInterface (IID_IHTMLDocument2, (void**)ppDoc);
			if (*ppDoc)
				hr = S_OK;
		}
	}
	
	return hr;
}
HRESULT vmsDomHelper::GetParentDocument(IHTMLDocument2 *pDoc, IHTMLDocument2 **ppParentDoc)
{
	*ppParentDoc = NULL;

	ATLASSERT (pDoc != NULL);
	if (!pDoc)
		return E_INVALIDARG;

	IHTMLWindow2Ptr spWnd;
	HRESULT hr = pDoc->get_parentWindow (&spWnd);
	if (FAILED (hr))
		return hr;

	IHTMLWindow2Ptr spWndParent;
	hr = spWnd->get_parent (&spWndParent);
	if (FAILED (hr))
		return hr;

	hr = spWndParent->get_document (ppParentDoc);
	if (hr == E_ACCESSDENIED)
	{
		IWebBrowser2Ptr spWB;
		hr = GetWebBrowser (spWndParent, &spWB);
		if (FAILED (hr))
			return E_ACCESSDENIED;
		IDispatchPtr spdDoc;
		hr = spWB->get_Document (&spdDoc);
		if (FAILED (hr))
			return hr;
		hr = spdDoc->QueryInterface (IID_IHTMLDocument2, (void**)ppParentDoc);
	}
	if (FAILED (hr))
		return hr;

	IUnknownPtr spUnk1 (pDoc);
	IUnknownPtr spUnk2 (*ppParentDoc);
	if (pDoc == *ppParentDoc || spUnk1 == spUnk2)
	{
		(*ppParentDoc)->Release ();
		*ppParentDoc = NULL;
	}

	return S_OK;
}
Example #3
0
void vmsWebBrowserEvents::OnDocumentComplete(LPDISPATCH pdWB, VARIANT *URL)
{
	if (m_spUIHandler != NULL)
	{
		IWebBrowser2Ptr spWB (pdWB);
		if (spWB == NULL)
			return;

		IDispatchPtr spdDoc;
		spWB->get_Document (&spdDoc);
		IHTMLDocumentPtr spDoc (spdDoc);
		if (spDoc == NULL)
			return;

		ICustomDocPtr spCD (spDoc); 
		if (spCD != NULL)
			spCD->SetUIHandler (m_spUIHandler);
	}
}
void CDownloads_Opinions_WBEvents::OnDocumentComplete(LPDISPATCH pdWB, VARIANT *URL)
{
	IWebBrowser2Ptr spWB (pdWB);
	if (spWB == NULL)
		return;

	IDispatchPtr spdDoc;
	spWB->get_Document (&spdDoc);
	IHTMLDocumentPtr spDoc (spdDoc);
	if (spDoc == NULL)
		return;

	ICustomDocPtr spCD (spDoc); 
	if (spCD != NULL)
		spCD->SetUIHandler (&m_wbUIHandler);

	bool bSetTab = false;
	if (m_pwndOpinions->m_cSetAsCurrentTabAfterLoadComplete)
	{
		InterlockedDecrement (&m_pwndOpinions->m_cSetAsCurrentTabAfterLoadComplete);
		bSetTab = true;
	}

	if (m_pwndOpinions->m_dld == NULL)
		return;

	if (bSetTab)
		_pwndDownloads->m_wndDownloads.m_info.set_CurTab (DIT_OPINIONS);

	

	IDispatch *pdScript;
	spDoc->get_Script (&pdScript);
	if (pdScript == NULL)
		return;

	

	DISPID didFun;
	OLECHAR* pwszFunName = L"processform";

	HRESULT hr;
	hr = pdScript->GetIDsOfNames (IID_NULL, &pwszFunName, 1, LOCALE_SYSTEM_DEFAULT, &didFun);
	if (FAILED (hr))
		return;	
	
	COleVariant vtResult;
	BYTE params [] = {VTS_BSTR VTS_BSTR VTS_BSTR VTS_BSTR VTS_BSTR};

	
	

	CString strVersion;
	CMainFrame* pFrm = (CMainFrame*)AfxGetApp ()->m_pMainWnd;
	strVersion.Format ("%s_%d", vmsFdmAppMgr::getBuildNumberAsString (), pFrm->m_Customizations.get_AffiliateID ());

	CString strState;
	if (m_pwndOpinions->m_dld->pMgr->IsDone ())
		strState = "Downloaded";
	else if (m_pwndOpinions->m_dld->pMgr->IsRunning ())
		strState = "Downloading";
	else
		strState = "Paused";
	CString strSize;
	UINT64 u = m_pwndOpinions->m_dld->pMgr->GetSSFileSize ();
	if (u != _UI64_MAX) {
		char sz [100];
		_i64toa ((__int64)u, sz, 10);
		strSize = sz;
	}
	else
		strSize = "Unknown";
	CString strComment = m_pwndOpinions->m_dld->strComment;
	strComment.Replace ("\r\n", " ");
	strComment.Replace ("\r", " ");
	strComment.Replace ("\n", " ");

	

	CString strUrl;
	if (m_pwndOpinions->m_dld->pMgr->IsBittorrent ())
		strUrl = m_pwndOpinions->m_dld->pMgr->GetBtDownloadMgr ()->get_InfoHash ();
	else
		strUrl = m_pwndOpinions->m_dld->pMgr->get_URL ();

	COleDispatchDriver ddr (pdScript);
	ddr.InvokeHelper (didFun, DISPATCH_METHOD, VT_VARIANT, (void*)&vtResult,
		params, strVersion, strUrl, strComment, 
		strState, strSize); 

	
	
	
	
	RetrieveLinkToUsText (spdDoc);
}
/******************************************************************************
*   Invoke -- Takes a dispid and uses it to call another of the methods of this 
*   class. Returns S_OK if the call was successful.
******************************************************************************/ 
STDMETHODIMP WebBrowserEventSinker::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid,
	WORD wFlags, DISPPARAMS* pDispParams,
	VARIANT* pVarResult,
	EXCEPINFO* pExcepInfo,
	UINT* puArgErr)
{
	//Validate arguments.
	if ((riid != IID_NULL))
		return E_INVALIDARG;

	//if (dispIdMember != DISPID_STATUSTEXTCHANGE)//过于频繁的不显示了
	//{
	//	TCHAR buf[128] = {0};
	//	_stprintf(buf, _T("WebBrowserEventSinker::Invoke dispId = %d\n"), dispIdMember);
	//	TRACE(buf);
	//}

	HRESULT hr = S_OK;  // Initialize.

	switch (dispIdMember)
	{
		//注意:pDispParams的参数和MFC的参数顺序是相反的,MFC的第一个参数是WebBrowser的this指针
		//The last of pDispParams is an object that evaluates to the top-level or frame WebBrowser object corresponding to the event.

// 	case DISPID_NEWWINDOW3:
// 		// (IDispatch **ppDisp, VARIANT_BOOL *Cancel, DWORD dwFlags, BSTR bstrUrlContext, BSTR bstrUrl)
// 		return E_NOTIMPL;
// 		break;
// 
// 	case DISPID_NEWWINDOW2:
// 		// pDispParams->rgvarg[0]: BOOL*   Cancel
// 		// pDispParams->rgvarg[1]: LPDISPATCH*   ppDisp, dest app
// 		{
// 			int nCount = pDispParams->cArgs;
// 			_variant_t vVal = pDispParams->rgvarg[1];
// 			IWebBrowser2Ptr pWB = vVal.pdispVal;
// 			AdviseWebBrowser(pWB);
// 		}
// 		break;

//	case DISPID_BEFORENAVIGATE2:
		// pDispParams->rgvarg[6]: WebBrowser object
		// pDispParams->rgvarg[5 - 0]: 
		// (LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName, CByteArray& baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel)
//		{
//			int nCount = pDispParams->cArgs;
//			_variant_t vVal = pDispParams->rgvarg[nCount - 2];
//		}
//		break;

	case DISPID_NAVIGATECOMPLETE2:
		//(LPCTSTR lpszURL)
		// pDispParams->rgvarg[0]: URL to navigate to - VT_BYREF|VT_VARIANT
		// pDispParams->rgvarg[1]: An object that evaluates to the top-level or frame WebBrowser object corresponding to the event.
		if (pDispParams->cArgs >= 2 && pDispParams->rgvarg[1].vt == VT_DISPATCH)
			SetCustomDocUIHandler(pDispParams->rgvarg[1].pdispVal);
		break;


	case DISPID_DOCUMENTCOMPLETE:
		//(LPCTSTR lpszURL)
		// pDispParams->rgvarg[0]: URL to navigate to - VT_BYREF|VT_VARIANT
		// pDispParams->rgvarg[1]: An object that evaluates to the top-level or frame WebBrowser object corresponding to the event.
		if (pDispParams->cArgs >= 2 && pDispParams->rgvarg[1].vt == VT_DISPATCH)
		{
			IWebBrowser2Ptr pBrowser;
			(pDispParams->rgvarg[1].pdispVal)->QueryInterface(IID_IWebBrowser2,(void**)&pBrowser);
			if (pBrowser)
			{
				IHTMLDocument2Ptr pDocument;
				IDispatchPtr spDisp;
				pBrowser->get_Document(&spDisp);
				pDocument = spDisp;
				if (pDocument)
				{
					IHTMLElementPtr pElement;
					IHTMLBodyElementPtr pBody; //IID_IHTMLBodyElement
					pDocument->get_body(&pElement);
					pBody = pElement;
					if (pBody != NULL)
					{
						pBody->put_scroll((BSTR)L"auto");

						IHTMLStylePtr pStyle;
						pElement->get_style(&pStyle);
						pStyle->put_border((BSTR)L"none");
						pStyle->put_overflow((BSTR)L"auto");
					}
				}
			}
		}
		break;

	default:
		break;
	}

	return hr;
}