查看html源码 ( 积分: 50 )

  • 主题发起人 主题发起人 tangtanger
  • 开始时间 开始时间
T

tangtanger

Unregistered / Unconfirmed
GUEST, unregistred user!
请问谁知道怎样用delphi查看网页html源码?谢谢指教![?]
 
请问谁知道怎样用delphi查看网页html源码?谢谢指教![?]
 
void CGetIhtmlDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
if(m_bCapture){
m_bCapture=FALSE;
ReleaseCapture();

static TCHAR buf[100];

POINT pt;
GetCursorPos(&pt);
HWND hwnd=::WindowFromPoint(pt);
if(hwnd!=NULL){
::GetClassName( hwnd, (LPTSTR)&buf, 100 );
if ( _tcscmp( buf, _T("Internet Explorer_Server") ) == 0 ){
if(m_uAction==_GETHTML_)
GetHtmlSource(GetDocInterface(hwnd));
// GetHtmlSource(GetDocInterfaceByMSAA(hwnd));
else{
POINT iept=pt;
::ScreenToClient(hwnd,&iept);
GetPassword(GetDocInterface(hwnd),iept);
// GetPassword(GetDocInterfaceByMSAA(hwnd),iept);
}
}
}
}

void CGetIhtmlDlg::GetHtmlSource(IHTMLDocument2* pHDoc2)
{
if(pHDoc2==NULL)return;

CComPtr<IHTMLElementCollection> pAllColl;
HRESULT hr;
hr=pHDoc2->get_all(&pAllColl);
if(hr==S_OK){
LONG length=0;
hr=pAllColl->get_length(&length);
if(hr==S_OK){
for(int i=0;i<length;i++){
VARIANT vIndex,vName;
vName.vt=vIndex.vt=VT_I4;
vName.lVal=vIndex.lVal=i;
CComPtr<IDispatch> pDisp;
hr=pAllColl->item(vName,vIndex,&pDisp);
if( hr==S_OK ){
CComPtr<IHTMLElement> pElement;
hr=pDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement);
if( hr==S_OK ){
CComBSTR tagName;
hr=pElement->get_tagName(&tagName);
if(hr==S_OK){
CString str(tagName);
if(str=="HTML"){
CComBSTR pContent;
hr=pElement->get_outerHTML(&pContent);
if(hr==S_OK){
SaveAndShow(pContent);
i=length;//以便退出循环
}
else{//if get_outerHTML failed
AfxMessageBox("can't get html code");
}
}//else if tagName isnot 'HTML'
}//else if get_tagName failed
}//else if don't get IHMTLElement interface
}//if no items
}
}//if get_length failed
}//if get_all failed
pHDoc2->Release();
}
 
有更简单的方法:
添加一个WebBrowser控件,在Uses里加一个MSHTML
比如点击按钮,在Memo里显示源码:
这样写:
var
doc:IHTMLDocument2;
elec:IHTMLElementCollection;
element:IHTMLElement;
i:integer;
begin
doc:=WebBrowser1.document as IHTMLDocument2;
elec:=doc.all;
for i:=0 to elec.length -1 do
begin
element:=elec.item(i,emptyparam) as IHTMLElement;
memo1.lines.add(element.innerHTML);
end;
这样你就能获得网页的源码了!
 
多人接受答案了。
 
后退
顶部