CHtmlView::GetHtmlDocument LPDISPATCH GetHtmlDocument( ) const;Return ValueA pointer to theIDispatch interface of the active document object.ResCall this member function to retrieve the HTML document for the active document.Applies to Internet Explorer and WebBrowser.CHtmlView Overview | Class Members | Hierarchy ChartSee Also IWebBrowser2::get_Document然后去查 IWebBrowser2 里面有你要的东西
if (SUCCEEDED(hr)&& pElem != NULL) { BSTR bstrValue; pElem->get_value(&bstrValue); CString str(bstrValue); AfxMessageBox(str); //pElem->click(); pElem->Release(); } pColl->Release(); } } else AfxMessageBox(_T("发生错误1!")); CDialog::OnOK(); } 尽管我在stdafx.h加了: #import <mshtml.tlb> // Internet Explorer 5 #import <shdocvw.dll>可提示如下错误 F:\VTWHF\WhDialog.cpp(105) : error C2027: use of undefined type 'IHTMLDocument2' D:\cheng_huan\Microsoft Visual Studio\VC98\INCLUDE\comdef.h(427) : see declaration of 'IHTMLDocument2' F:\VTWHF\WhDialog.cpp(105) : error C2227: left of '->get_all' must point to class/struct/union F:\VTWHF\WhDialog.cpp(115) : error C2027: use of undefined type 'IHTMLElementCollection' D:\cheng_huan\Microsoft Visual Studio\VC98\INCLUDE\comdef.h(358) : see declaration of 'IHTMLElementCollection' F:\VTWHF\WhDialog.cpp(115) : error C2227: left of '->item' must point to class/struct/union F:VTWHF\WhDialog.cpp(125) : error C2027: use of undefined type 'IHTMLInputTextElement' D:\cheng_huan\Microsoft Visual Studio\VC98\INCLUDE\comdef.h(376) : see declaration of 'IHTMLInputTextElement' F:VTWHF\WhDialog.cpp(125) : error C2227: left of '->get_value' must point to class/struct/union F:VTWHF\WhDialog.cpp(129) : error C2027: use of undefined type 'IHTMLInputTextElement' D:\cheng_huan\Microsoft Visual Studio\VC98\INCLUDE\comdef.h(376) : see declaration of 'IHTMLInputTextElement' F:VTWHF\WhDialog.cpp(129) : error C2227: left of '->Release' must point to class/struct/union F:\VTWHF\WhDialog.cpp(131) : error C2027: use of undefined type 'IHTMLElementCollection' D:\cheng_huan\Microsoft Visual Studio\VC98\INCLUDE\comdef.h(358) : see declaration of 'IHTMLElementCollection' F:\VTWHF\WhDialog.cpp(131) : error C2227: left of '->Release' must point to class/struct/union Generating Code... Error executing cl.exe.VTWHF.exe - 10 error(s), 0 warning(s)
具体代码 CString CJHWebBrowser::QueryInputElementValue(LPCTSTR name, UINT type) { CString value(""); MSHTML::IHTMLDocument2* pHtmlDoc2 = (MSHTML::IHTMLDocument2*)GetDocument(); if(pHtmlDoc2) { HRESULT hr = S_OK; MSHTML::IHTMLElementCollection* pColl = NULL; hr = pHtmlDoc2->get_all(&pColl); if( SUCCEEDED(hr) && pColl) { long len = 0; if (!SUCCEEDED(pColl->get_length(&len))) return value; for (int i=0; i<len; i++) { _variant_t Elementindex; Elementindex.vt = VT_I4; Elementindex.intVal = i; _variant_t Elementname(name); IDispatchPtr disp = NULL; disp = pColl->item(Elementname, Elementindex); if (disp == NULL) return value; //若未查出有此name的Element,即disp为空,则返回 switch(type) { case INPUT_BUTTON: case INPUT_SUBMIT: case INPUT_RESET: { MSHTML::IHTMLInputButtonElement* pInputButtonElem = NULL; if (disp) hr = disp->QueryInterface(&pInputButtonElem); if (SUCCEEDED(hr) && pInputButtonElem ) { BSTR bstrValue; hr = pInputButtonElem->get_value(&bstrValue); if (!SUCCEEDED(hr)) return value; value = bstrValue; pInputButtonElem->Release(); } break; } case INPUT_TEXT: case INPUT_PASSWORD: { MSHTML::IHTMLInputTextElement* pInputTextElem = NULL; if (disp) hr = disp->QueryInterface(&pInputTextElem);
if (SUCCEEDED(hr) && pInputTextElem ) { BSTR bstrValue; hr = pInputTextElem->get_value(&bstrValue); if (!SUCCEEDED(hr)) return value; value = bstrValue; pInputTextElem->Release(); } break; } case INPUT_FILE: { MSHTML::IHTMLInputFileElement* pInputFileElem = NULL; if (disp) hr = disp->QueryInterface(&pInputFileElem);
if (SUCCEEDED(hr) && pInputFileElem ) { BSTR bstrValue; hr = pInputFileElem->get_value(&bstrValue); if (!SUCCEEDED(hr)) return value; value = bstrValue; pInputFileElem->Release(); } break; } case INPUT_CHECKBOX: { MSHTML::IHTMLElement* pInputElem = NULL; if (disp) hr = disp->QueryInterface(&pInputElem);
if (SUCCEEDED(hr) && pInputElem ) { BSTR bstrValue; hr = pInputElem->get_innerText(&bstrValue); if (!SUCCEEDED(hr)) return value; value = bstrValue; pInputElem->Release(); } break; } case INPUT_HIDDEN: { MSHTML::IHTMLInputHiddenElement* pInputHiddenElem = NULL; if (disp) hr = disp->QueryInterface(&pInputHiddenElem);
if (SUCCEEDED(hr) && pInputHiddenElem ) { BSTR bstrValue; hr = pInputHiddenElem->get_value(&bstrValue); if (!SUCCEEDED(hr)) return value; value = bstrValue; pInputHiddenElem->Release(); } break; } case INPUT_RADIO: { MSHTML::IHTMLInputButtonElement* pInputButtonElem = NULL; if (disp) hr = disp->QueryInterface(&pInputButtonElem);
if (SUCCEEDED(hr) && pInputButtonElem ) { BSTR bstrValue; hr = pInputButtonElem->get_value(&bstrValue); if (!SUCCEEDED(hr)) return value; value = bstrValue; pInputButtonElem->Release(); } break; } case INPUT_IMAGE: //查询INPUT_IMAGE的地址 { MSHTML::IHTMLInputImage* pInputImageElem = NULL; if (disp) hr = disp->QueryInterface(&pInputImageElem);
if (SUCCEEDED(hr) && pInputImageElem ) { BSTR bstrValue; hr = pInputImageElem->get_src(&bstrValue); if (!SUCCEEDED(hr)) return value; value = bstrValue; pInputImageElem->Release(); } break; } default: break; } if (!value.IsEmpty()) break; } pColl->Release(); } } return value; }
LPDISPATCH GetHtmlDocument( ) const;Return ValueA pointer to theIDispatch interface of the active document object.ResCall this member function to retrieve the HTML document for the active document.Applies to Internet Explorer and WebBrowser.CHtmlView Overview | Class Members | Hierarchy ChartSee Also IWebBrowser2::get_Document然后去查 IWebBrowser2 里面有你要的东西
我也知道通过内存的Html可以办到,但缺乏具体操作的经验,不知道该怎么获得,还需要研究一下to:ifengfeng(fengfeng)
你的方式我也试过,无法实现数据的传接
{
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;
hr=pPwdElement->get_value(&name);
if(SUCCEEDED(hr))
{
if(name.Length()!=0)
{
CComBSTR msg;
msg=name;
}
else
{
return 0;
}
}
}
}
else
{
return 0;
}
}
}
}
}
}
}
pColl->Release();
}
pDoc2->Release();
return 1;
return 0;
}
void CWhDialog::OnOK()
{
IHTMLDocument2* pHtmlDoc2 = (IHTMLDocument2*)m_RightWeb.GetDocument();
if(pHtmlDoc2)
{
HRESULT hr = S_OK;
IHTMLElementCollection* pColl = NULL;
hr = pHtmlDoc2->get_all(&pColl);
if( hr==S_FALSE && pColl)
AfxMessageBox(_T("发生错误2!"));
else
{
_variant_t index;
index.vt=VT_I4;
index.intVal=0;
_variant_t name("textfield");
IDispatchPtr disp = NULL;
disp = pColl->item(name,index);
IHTMLInputTextElement* pElem = NULL;
if (disp)
disp->QueryInterface(&pElem);
else
hr=E_FAIL;
if (SUCCEEDED(hr)&& pElem != NULL)
{
BSTR bstrValue;
pElem->get_value(&bstrValue);
CString str(bstrValue);
AfxMessageBox(str);
//pElem->click();
pElem->Release();
}
pColl->Release();
}
}
else
AfxMessageBox(_T("发生错误1!")); CDialog::OnOK();
}
尽管我在stdafx.h加了:
#import <mshtml.tlb> // Internet Explorer 5
#import <shdocvw.dll>可提示如下错误
F:\VTWHF\WhDialog.cpp(105) : error C2027: use of undefined type 'IHTMLDocument2'
D:\cheng_huan\Microsoft Visual Studio\VC98\INCLUDE\comdef.h(427) : see declaration of 'IHTMLDocument2'
F:\VTWHF\WhDialog.cpp(105) : error C2227: left of '->get_all' must point to class/struct/union
F:\VTWHF\WhDialog.cpp(115) : error C2027: use of undefined type 'IHTMLElementCollection'
D:\cheng_huan\Microsoft Visual Studio\VC98\INCLUDE\comdef.h(358) : see declaration of 'IHTMLElementCollection'
F:\VTWHF\WhDialog.cpp(115) : error C2227: left of '->item' must point to class/struct/union
F:VTWHF\WhDialog.cpp(125) : error C2027: use of undefined type 'IHTMLInputTextElement'
D:\cheng_huan\Microsoft Visual Studio\VC98\INCLUDE\comdef.h(376) : see declaration of 'IHTMLInputTextElement'
F:VTWHF\WhDialog.cpp(125) : error C2227: left of '->get_value' must point to class/struct/union
F:VTWHF\WhDialog.cpp(129) : error C2027: use of undefined type 'IHTMLInputTextElement'
D:\cheng_huan\Microsoft Visual Studio\VC98\INCLUDE\comdef.h(376) : see declaration of 'IHTMLInputTextElement'
F:VTWHF\WhDialog.cpp(129) : error C2227: left of '->Release' must point to class/struct/union
F:\VTWHF\WhDialog.cpp(131) : error C2027: use of undefined type 'IHTMLElementCollection'
D:\cheng_huan\Microsoft Visual Studio\VC98\INCLUDE\comdef.h(358) : see declaration of 'IHTMLElementCollection'
F:\VTWHF\WhDialog.cpp(131) : error C2227: left of '->Release' must point to class/struct/union
Generating Code...
Error executing cl.exe.VTWHF.exe - 10 error(s), 0 warning(s)
CString CJHWebBrowser::QueryInputElementValue(LPCTSTR name, UINT type)
{
CString value(""); MSHTML::IHTMLDocument2* pHtmlDoc2 = (MSHTML::IHTMLDocument2*)GetDocument();
if(pHtmlDoc2)
{
HRESULT hr = S_OK;
MSHTML::IHTMLElementCollection* pColl = NULL;
hr = pHtmlDoc2->get_all(&pColl);
if( SUCCEEDED(hr) && pColl)
{
long len = 0;
if (!SUCCEEDED(pColl->get_length(&len)))
return value;
for (int i=0; i<len; i++)
{
_variant_t Elementindex;
Elementindex.vt = VT_I4;
Elementindex.intVal = i;
_variant_t Elementname(name); IDispatchPtr disp = NULL;
disp = pColl->item(Elementname, Elementindex);
if (disp == NULL) return value; //若未查出有此name的Element,即disp为空,则返回
switch(type)
{
case INPUT_BUTTON:
case INPUT_SUBMIT:
case INPUT_RESET:
{
MSHTML::IHTMLInputButtonElement* pInputButtonElem = NULL;
if (disp)
hr = disp->QueryInterface(&pInputButtonElem);
if (SUCCEEDED(hr) && pInputButtonElem )
{
BSTR bstrValue;
hr = pInputButtonElem->get_value(&bstrValue);
if (!SUCCEEDED(hr)) return value;
value = bstrValue;
pInputButtonElem->Release();
}
break;
}
case INPUT_TEXT:
case INPUT_PASSWORD:
{
MSHTML::IHTMLInputTextElement* pInputTextElem = NULL;
if (disp)
hr = disp->QueryInterface(&pInputTextElem);
if (SUCCEEDED(hr) && pInputTextElem )
{
BSTR bstrValue;
hr = pInputTextElem->get_value(&bstrValue);
if (!SUCCEEDED(hr)) return value;
value = bstrValue;
pInputTextElem->Release();
}
break;
}
case INPUT_FILE:
{
MSHTML::IHTMLInputFileElement* pInputFileElem = NULL;
if (disp)
hr = disp->QueryInterface(&pInputFileElem);
if (SUCCEEDED(hr) && pInputFileElem )
{
BSTR bstrValue;
hr = pInputFileElem->get_value(&bstrValue);
if (!SUCCEEDED(hr)) return value;
value = bstrValue;
pInputFileElem->Release();
}
break;
}
case INPUT_CHECKBOX:
{
MSHTML::IHTMLElement* pInputElem = NULL;
if (disp)
hr = disp->QueryInterface(&pInputElem);
if (SUCCEEDED(hr) && pInputElem )
{
BSTR bstrValue;
hr = pInputElem->get_innerText(&bstrValue);
if (!SUCCEEDED(hr)) return value;
value = bstrValue;
pInputElem->Release();
}
break;
}
case INPUT_HIDDEN:
{
MSHTML::IHTMLInputHiddenElement* pInputHiddenElem = NULL;
if (disp)
hr = disp->QueryInterface(&pInputHiddenElem);
if (SUCCEEDED(hr) && pInputHiddenElem )
{
BSTR bstrValue;
hr = pInputHiddenElem->get_value(&bstrValue);
if (!SUCCEEDED(hr)) return value;
value = bstrValue;
pInputHiddenElem->Release();
}
break;
} case INPUT_RADIO:
{
MSHTML::IHTMLInputButtonElement* pInputButtonElem = NULL;
if (disp)
hr = disp->QueryInterface(&pInputButtonElem);
if (SUCCEEDED(hr) && pInputButtonElem )
{
BSTR bstrValue;
hr = pInputButtonElem->get_value(&bstrValue);
if (!SUCCEEDED(hr)) return value;
value = bstrValue;
pInputButtonElem->Release();
}
break;
}
case INPUT_IMAGE: //查询INPUT_IMAGE的地址
{
MSHTML::IHTMLInputImage* pInputImageElem = NULL;
if (disp)
hr = disp->QueryInterface(&pInputImageElem);
if (SUCCEEDED(hr) && pInputImageElem )
{
BSTR bstrValue;
hr = pInputImageElem->get_src(&bstrValue);
if (!SUCCEEDED(hr)) return value;
value = bstrValue;
pInputImageElem->Release();
}
break;
}
default:
break;
}
if (!value.IsEmpty())
break;
}
pColl->Release();
}
} return value;
}
值得学习!!