描述:
用MFC开发了一个ACTIVEX控件,嵌入到网页中,想得到网页内容
IHTMLDocument2 * mFrameDoc;
mFrameDoc = (IHTMLDocument2 *)pBrowser->GetDocument();
IHTMLElement * pBody=NULL;
mFrameDoc->get_body(&pBody);
使用以上代码想获得IHTMLDocument2 接口,可是不知道这里的pBrowser指的是什么。在我的程序里应该如何写,而且提示我IHTMLDocument2 类型找不到。。请问这是缺少什么???
解决方案1:
////////////////////////////////////////////////////////////////////////
我这段程序是将一些信息填入HTML网页中的input项的,当然你也可以将put改为get获取网页的信息
看看对你有没有帮助
////////////////////////////////////////////////////////////////////////
void SetFile(IHTMLDocument2 *pDoc2,TCHAR* text)
{
//分离text中的LoginId和RealPassword
CString sRet,LoginID,LoginPW;
sRet = text;
int nn = sRet.Find(" ");
LoginID = sRet.Mid(0,nn);
LoginPW = sRet.Mid(nn+1);
//-------------------------------------
if(pDoc2==NULL)
return;
//声明一个IHTMLElement接口指针
CComPtr<IHTMLElement> pElement;
CComPtr<IHTMLElementCollection> pElementCollection;
IDispatch * Idisp;
IHTMLInputTextElement * input;
CComBSTR type;
HRESULT hr=pDoc2->get_all(&pElementCollection);
long n;
pElementCollection->get_length(&n);
for(long i=0;i<n;i++)
{
_variant_t d = i;
hr = pElementCollection->item(d,d,&Idisp);
if(SUCCEEDED(hr))
{
hr = Idisp->QueryInterface(IID_IHTMLInputTextElement,(void**)&input);
if(SUCCEEDED(hr))
{
hr = input->get_type(&type);
if(SUCCEEDED(hr))
{
if(type==_T("password"))
{
CComBSTR pwd = LoginPW;
input->put_value(pwd);
}
if(type==_T("text"))
{
CComBSTR idd = LoginID;
input->put_value(idd);
}
}
}
}
}
pDoc2->Release();
}
your control inherits from COleControl.
1. call IOleClientSite* pClientSite = GetClientSite();
2. IWebBrowser2* browser;
IServiceProvider* isp;
HRESULT hr = m_spClientSite->QueryInterface(IID_IServiceProvider, reinterpret_cast<void **>(&isp));
if(FAILED(hr)) return 0;
hr = isp->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, reinterpret_cast<void **>(&browser));
IDispatch **ppDispDoc;
browser->get_Document(ppDispDoc);
ppDispDoc->QueryInterface(IID_IHTMLDocument2
pBrowser IWebBrowser2指针
HOWTO: Retrieve the Top-Level IWebBrowser2 Interface from an ActiveX Control
Q257717
To retrieve the top-level IWebBrowser2 reference, get IServiceProvider from the client site and perform a QueryService for IID_IServiceProvider under the service SID_STopLevelBrowser (defined in Shlguid.h). From this second IServiceProvider, perform a QueryService for IID_IWebBrowser2 in the SID_SwebBrowserApp service.
The best place to perform this work is in the SetClientSite() method of IOleObject:
#include <SHLGUID.h>
#define COMRELEASE(ptr)\
if (ptr != NULL) {\
ptr->Release();\
ptr = NULL;\
}
IWebBrowser2 *browser = NULL;
STDMETHODIMP SetClientSite(IOleClientSite *pClientSite)
{
HRESULT hr = S_OK;
IServiceProvider *isp, *isp2 = NULL;
if (!pClientSite)
{
COMRELEASE(browser);
}
else
{
hr = pClientSite->QueryInterface(IID_IServiceProvider, reinterpret_cast<void **>(&isp));
if (FAILED(hr))
{
hr = S_OK;
goto cleanup;
}
hr = isp->QueryService(SID_STopLevelBrowser, IID_IServiceProvider, reinterpret_cast<void **>(&isp2));
if (FAILED(hr))
{
hr = S_OK;
goto cleanup;
}
hr = isp2->QueryService(SID_SwebBrowserApp, IID_IWebBrowser2, reinterpret_cast<void **>(&browser));
if (FAILED(hr))
{
hr = S_OK;
goto cleanup;
}
cleanup:
// Free resources.
COMRELEASE(isp);
COMRELEASE(isp2);
return hr;
}
}