Пример #1
0
HRESULT CPdnWnd::ContainerCreate(BOOL popUnder)
{
	/* Create the control */
	IOleObject* activeXControl = 
		m_ActiveXHost.Create(m_hWnd, L"Shell.Explorer.2", popUnder);
	if(activeXControl == NULL)
	{
		return E_FAIL;
	}
	/* Get the webbrowser control and initialize the browser */
	HRESULT hr = activeXControl->QueryInterface(IID_IWebBrowser2,
		(void**) &m_pBrowser);
	if(SUCCEEDED(hr))
	{
		DWORD dwCookie;
		IConnectionPointContainerPtr pCPC;
		IConnectionPointPtr pCP;
		HRESULT hr = activeXControl->QueryInterface(
			IID_IConnectionPointContainer, (LPVOID*) &pCPC);
		hr = pCPC->FindConnectionPoint(DIID_DWebBrowserEvents2, &pCP);
		hr = pCP->Advise(dynamic_cast<IDispatch*>(this), &dwCookie);

		m_pBrowser->put_RegisterAsBrowser(VARIANT_FALSE);
		m_pBrowser->put_RegisterAsDropTarget(VARIANT_FALSE);
		m_pBrowser->put_Silent(VARIANT_FALSE);
		m_pBrowser->Navigate(_bstr_t(m_URL.c_str()), 0, 0, 0, 0);
	}
	activeXControl->Release();

	return hr;
}
HRESULT WebBrowserEventSinker::AdviseWebBrowser(IWebBrowser2* pWebBrowser2)
{
	//设置连接点
	try
	{
		if (pWebBrowser2)
		{
			IConnectionPointContainerPtr pConnPtContainer;
			IConnectionPointPtr pConnectionPoint;

			// Get IConnectionPointContainer interface for the server.
			HRESULT hr = pWebBrowser2->QueryInterface( IID_IConnectionPointContainer, (void **)&pConnPtContainer );
			if (SUCCEEDED(hr) && pConnPtContainer != NULL)
			{
				// Find the connection point for events that you are interested in.
				hr = pConnPtContainer->FindConnectionPoint( DIID_DWebBrowserEvents2, &pConnectionPoint);
				if (SUCCEEDED(hr) && pConnectionPoint != NULL)
				{
					// Set up advisory connection.
					hr = pConnectionPoint->Advise((IUnknown*)this, &m_dwConnectionCookie);
					if (SUCCEEDED(hr))
					{
						m_pConnectionPoint = pConnectionPoint;
						m_pConnectionPoint->AddRef();
						return S_OK;
					}
				}
			}
		}
	}
	catch (...)
	{
	}
	return E_FAIL;
}