VC HTML IHtmlDocument相关代码
1、从IE窗体获得IHTMLDocument2 接口
2、获得 IHTMLDocument2 全部文本内容
参考 VC HTML IHtmlDocument相关代码片段
http://blog.csdn.net/demon_evil/archive/2008/03/21/2201976.aspx
《.h》
#include <mshtml.h>
#include <atlbase.h>
#include <oleacc.h>
class CHtmlOper
{
public:
CHtmlOper(void);
~CHtmlOper(void);
static IHTMLDocument2 * GetHTMLDocInterface(HWND hWnd);
static CString GetHtmlSource(IHTMLDocument2 * pHDoc2);
static void OnBnClickedButton35SaveAndShow(BSTR pContent);
};
《.cpp》
IHTMLDocument2 * CHtmlOper::GetHTMLDocInterface(HWND hWnd)
{
// 从IE窗体获得IHTMLDocument2 接口
IHTMLDocument2* spDoc = NULL;
// fun1: IHTMLDocument2 * MyGetHTMLDocInterface(HWND hWnd)
//#include <mshtml.h>
//#include <atlbase.h>
//#include <oleacc.h>
//Header Oleacc.h
//Library Oleacc.lib
{
IHTMLDocument2* spDoc2=NULL;
// Explicitly load MSAA so we know if it 's installed
HINSTANCE hInst = ::LoadLibrary(_T("OLEACC.DLL"));
if (hInst != NULL)
{
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT"));
::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, "ObjectFromLresult" );
if (pfObjectFromLresult != NULL )
{
HRESULT hr;
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc2 );
if(SUCCEEDED(hr))
{
CComPtr <IDispatch> spDisp;
CComQIPtr <IHTMLWindow2> spWin;
spDoc2-> get_Script(&spDisp);
spWin = spDisp;
spWin-> get_document(&spDoc2);
spDisp.Release();
spWin.Release();
}
} // else document not ready
::FreeLibrary( hInst );
}
// else Active Accessibility is not installed
spDoc = spDoc2; // return spDoc2;
}
return spDoc;
//////////////////////////////////////////////////////////////////
IHTMLDocument2* spDoc2=NULL;
// Explicitly load MSAA so we know if it 's installed
HINSTANCE hInst = ::LoadLibrary(_T("OLEACC.DLL"));
if (hInst != NULL)
{
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT"));
::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, "ObjectFromLresult" );
if (pfObjectFromLresult != NULL )
{
HRESULT hr;// = ObjectFromLresult( lRes, IID_IHTMLDocument, 0, (void**)&spDoc2 );
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc2 );
if(SUCCEEDED(hr))
{
CComPtr <IDispatch> spDisp;
CComQIPtr <IHTMLWindow2> spWin;
spDoc2-> get_Script(&spDisp);
spWin = spDisp;
spWin-> get_document(&spDoc2);
spDisp.Release();
spWin.Release();
}
} // else document not ready
::FreeLibrary( hInst );
return spDoc2;
}
return NULL;
}
CString CHtmlOper::GetHtmlSource(IHTMLDocument2 * pHDoc2)
{
// 获得 IHTMLDocument2 全部文本内容
CString strContent;
if(pHDoc2==NULL)
{
return _T("");
}
CComPtr <IHTMLElementCollection> pAllColl;
HRESULT hr;
hr=pHDoc2-> get_all(&pAllColl);
if(hr!=S_OK)
{
return _T("");
}
LONG length=0;
hr=pAllColl->get_length(&length);
if(hr!=S_OK)
return _T("");
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== _T("HTML"))
{
CComBSTR pContent;
hr=pElement-> get_outerHTML(&pContent);
if(hr==S_OK)
{
CString s(pContent);
strContent.Format(_T("%s"), (LPCTSTR)s);
//OnBnClickedButton35SaveAndShow(pContent); // !!!
i=length;//以便退出循环
}
else
{//if get_outerHTML failed
AfxMessageBox( _T("can't get code"));
}
}//else if tagName isnot 'HTML '
}//else if get_tagName failed
}//else if don 't get IHMTLElement interface
}//if no items
}
pHDoc2-> Release();
return strContent;
}
void CHtmlOper::OnBnClickedButton35SaveAndShow(BSTR pContent)
{
TCHAR lpFileName[MAX_PATH] = _T("c:\\FileName.txt");
TCHAR lpTempPath[MAX_PATH] = _T("c:\\TempPath.txt");
DWORD len;
if(GetTempPath(50,lpTempPath)!=0)
{
if(GetTempFileName(lpTempPath,_T( "Get "),0,lpFileName)!=0)
{
HANDLE hFile=::CreateFile(lpFileName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_FLAG_WRITE_THROUGH,
NULL );
if(hFile!=NULL)
{
CString strContent(pContent);
if(!WriteFile(hFile,strContent,strContent.GetLength()*sizeof(TCHAR),&len,NULL))
{
AfxMessageBox( _T("Can't not write to file "));
}
// CloseHandle(hFile);
USES_CONVERSION;
CString str=_T( "notepad ");
str+=lpFileName;
::WinExec(T2A(str), SW_SHOW);
}
else
{
AfxMessageBox( _T("Error in open file"));
}
}
}
}