开源项目之在线网页截图工具 IECapt(一)

2014-11-24 10:27:57 · 作者: · 浏览: 0

这个项目是win32程序,就一个目标文件。
class CEventSink :public CComObjectRootEx , public IDispatch 实现了网页接口的事件接收器。
class CMain :public CWindowImpl 窗口主要界面,快照的实现(获得网页接口对象)。

快照实现部分:
[cpp]
//////////////////////////////////////////////////////////////////
// Implementation of CMain::SaveSnapshot
//////////////////////////////////////////////////////////////////
BOOL CMain::SaveSnapshot(void)
{
long bodyHeight, bodyWidth, rootHeight, rootWidth, height, width;


CComPtr pDispatch;


// TODO: "If the document object type is not safe for scripting,
// this method returns successfully but sets ppDisp to NULL. For
// Internet Explorer 7 and later, the return code is S_FALSE..."

//获得对象
HRESULT hr = m_pWebBrowser->get_Document(&pDispatch);


if (FAILED(hr))
return true;

//获得ihtml接口
CComPtr spDocument;
hr = pDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&spDocument);


if (FAILED(hr))
return true;

//获得内容
CComPtr spBody;
hr = spDocument->get_body(&spBody);


// Apparently with MSHTML failing to get the body is not a failure,
// so if there is no HTML body to get, which may be the case with
// SVG images loaded directly, this succeeds but sets spBody to the
// NULL pointer, leading to a crash. I am not sure how to obtain
// the sizing information for SVG documents so this errors out here.
// A work around would be the make HTML host documents or wrapping
// the SVG code in a XHTML document, but that may break scripts.
if (FAILED(hr) || spBody == NULL)
return true;

//查找相关接口
CComPtr spBody2;
hr = spBody->QueryInterface(IID_IHTMLElement2, (void**)&spBody2);


if (FAILED(hr))
return true;


hr = spBody2->get_scrollHeight(&bodyHeight);


if (FAILED(hr))
return true;


hr = spBody2->get_scrollWidth(&bodyWidth);


if (FAILED(hr))
return true;


CComPtr spDocument3;
hr = pDispatch->QueryInterface(IID_IHTMLDocument3, (void**)&spDocument3);


if (FAILED(hr))
return true;


// We also need to get the dimensions from the due to quirks
// and standards mode differences. Perhaps this should instead check
// whether we are in quirks mode How does it work with IE8
CComPtr spHtml;
hr = spDocument3->get_documentElement(&spHtml);


if (FAILED(hr))
return true;


CComPtr spHtml2;
hr = spHtml->QueryInterface(IID_IHTMLElement2, (void**)&spHtml2);


if (FAILED(hr))
return true;


hr = spHtml2->get_scrollHeight(&rootHeight);


if (FAILED(hr))
return true;


hr = spHtml2->get_scrollWidth(&rootWidth);


if (FAILED(hr))
return true;


width = bodyWidth;
height = rootHeight > bodyHeight rootHeight : bodyHeight;


// TODO: What if width or height exceeds 32767 It seems Windows limits
// the window size, and Internet Explorer does not draw what's not visible.
::MoveWindow(m_hwndWebBrowser, 0, 0, width, height, TRUE);


CComPtr spViewObject;


// This used to get the interface from the m_pWebBrowser but that seems
// to be an undocumented feature, so we get it from the Document instead.
hr = spDocument3->QueryInterface(IID_IViewObject2, (void**)&spViewObject);


if (FAILED(hr))
return true;


RECTL rcBounds = { 0, 0, width, height };


_TCHAR* tcsExt = _tcsrchr(m_fileName, '.');
if (tcsExt && _tcscmp(_T(".emf"), tcsExt) == 0) {


USES_CONVERSION;
HDC hdcMain = GetDC();
int iWidthMM = GetDeviceCaps(hdcMain, HORZSIZE);
int iHeightMM = GetDeviceCaps(hdcMain, VERTSIZE);
int iWidthPels = GetDevic