描述:
我用ATL开发了一个控件要从网页中获取一个IP地址的参数。也就是在JavaScript写的网页中有一个IP地址的参数,我想在ATL控件的开发代码中得到这个IP地址,如何得到?
解决方案1:
181678 HOWTO: Retrieve the URL of a Web Page from an ActiveX Control
172763 INFO: Accessing the Object Model from Within an ActiveX Control
可以通过http://support.microsoft.com/default.aspx?kbid=文章编号来访问指定编号的文章。
RESULT CCompanyDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
CAxWindow wndIE;
wndIE = GetDlgItem(IDC_IE);
HRESULT hr;
hr = wndIE.QueryControl ( &m_spWebBrowser );
if ( m_spWebBrowser )
{
CComQIPtr<IConnectionPointContainer, &IID_IConnectionPointContainer> spCPC;
CComVariant v; // empty variant
////////////////////
//为 WebBrowser 事件查赵连接点
spCPC=m_spWebBrowser;
hr = spCPC->FindConnectionPoint(DIID_DWebBrowserEvents2, &m_spCP);
if (FAILED(hr))
MessageBox("Error","Error");
// 将我们的事件处理器传递给容器。事件发生时容器将调用我们实现的
// IDispatch 接口函数
InternalAddRef();
hr = m_spCP->Advise( reinterpret_cast<IDispatch*>(this), &m_dwCookie);
////////////////////
m_spWebBrowser->Navigate ( CComBSTR("test.html"),
&v, &v, &v, &v );
}
return 1; // Let the system set the focus
};
BOOL CCompanyDlg::SaveData()
{
IHTMLDocument2 *pDoc2=m_pDoc2;
CComBSTR msg;
if(pDoc2==NULL)
return 0;
IHTMLElementCollection *pColl=NULL;
CComPtr<IHTMLElement> pElement;
pDoc2->get_body(&pElement);
if(pElement==NULL)
return 0;
HRESULT hr=pDoc2->get_all(&pColl);
if(SUCCEEDED(hr) && pColl!=NULL)
{
/////////////////////////////////////////
long lcount = 0;
pColl->get_length(&lcount);
for(int i=0;i<lcount;i++)
{
CComVariant index;
index.vt=VT_I4;
index.intVal=i;
CComPtr<IDispatch> disp;
pColl->item(index,index,&disp);
if(disp==NULL)
hr=E_FAIL;
else
{
//////////////////////////
CComPtr<IHTMLInputTextElement> pPwdElement;
hr=disp->QueryInterface(IID_IHTMLInputTextElement,
(void**)&pPwdElement);
if(SUCCEEDED(hr))
{
CComBSTR type;
hr=pPwdElement->get_type(&type);
if(SUCCEEDED(hr))
{
//get edit text
if(type==_T("text"))
{
CComBSTR name,value;
pPwdElement->get_name(&name);
hr=pPwdElement->get_value(&value);
if(SUCCEEDED(hr))
{
if(name.Length()!=0)
{
...
}
}
}
}
}
}
}
pColl->Release();
}