CHtmlView浏览器双击页面保存图片的初步实现

1、CHtmlView无法直接响应鼠标事件,可以在PreTranslateMessage函数中捕获鼠标事件;


2、部分网页可以比较容易获得图片地址实现双击保存图片,部分网页图片地址因为动态构造不好直接获得(还需继续努力),使用土办法右键复制再按ctrl键;


3、有些网站图片地址标签是src,比如<IMG style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; FONT-SIZE: 12pt; BORDER-LEFT-WIDTH: 0px" src="./W020140707501632610819.jpg" complete="complete" oldsrc="W020140707501632610819.jpg">;有些网站则不是,无法统一用一个标准解析。


BOOL CSampleBrowerView::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_LBUTTONDBLCLK)//双击保存图片
	{
		//OpenClipboard();//一、打开剪切板
		//HANDLE hClipMemory=::GetClipboardData(CF_TEXT);//二、获得指向剪切板的全局指针变量
		//LPBYTE lpClipMemory = (LPBYTE)GlobalLock(hClipMemory);//三、锁定剪切板
		//CString strLink = CString(lpClipMemory);//四、获取剪切板内容
		//GlobalUnlock(hClipMemory);//五、解锁剪切板
		//::CloseClipboard();//六、关闭剪切板
		//使用CHtmlView::GetHtmlDocument()方法获取LPDISPATCH指针,通过这个指针可以得到整个html页面元素对象的控制权
		LPDISPATCH lDispatch = CHtmlView::GetHtmlDocument();
		IHTMLDocument2* pDocument = (IHTMLDocument2*)lDispatch;//将得到的LPDISPATCH指针强制转换为IHTMLDocument2指针
		IHTMLElement* pElement=NULL;
		pDocument->elementFromPoint(pMsg->pt.x, pMsg->pt.y, &pElement);//得到IHTMLElement对象,这个IHTMLElement对象即为html里图片元素的对象(如果你点的是图片)
		
		
		VARIANT attr;
		BSTR bstrtemp1,bstrtemp2;
		//bstrtemp1 = SysAllocString(L"style");
		//bstrtemp2 = SysAllocString(L"class");
		//bstrtemp2 = SysAllocString();
		//pElement->get_tagName(&bstrtemp1);
		//pElement->get_tagName(&bstrtemp2);
		//pElement->getAttribute(bstrtemp2, 20, &attr);
		//bstrtemp2 = attr.bstrVal;
		pElement->get_outerHTML(&bstrtemp1);
		CString attrValue = bstrtemp1; 
		pElement->getAttribute(SysAllocString(L"src"), 20, &attr);//http://picture.youth.cn/qtdb/201407/t20140707_5477187_1.htm 页面解析
		bstrtemp2 = attr.bstrVal;//http://picture.youth.cn/qtdb/201407/W020140707501632574257.jpg

		CString strPath;
		strPath.Format("D:\\book\\%06d.png",indexBook);//%06d 表示6位整数,不足6位前面补零
		CString strLink=bstrtemp2;//获取当前url地址
		if(strLink=="")return CHtmlView::PreTranslateMessage(pMsg);
		URLDownloadToFile(NULL,strLink,strPath,0,NULL);
		indexBook++;
	}
	else if (pMsg->wParam == VK_CONTROL)//手工复制,按ctrl键保存图片
	{
		OpenClipboard();//一、打开剪切板
		HANDLE hClipMemory=NULL;
		//hClipMemory=::GetClipboardData(CF_TEXT);//二、获得指向剪切板的全局指针变量
		if (::GetClipboardData(CF_TEXT)!=NULL)
		{
			hClipMemory=::GetClipboardData(CF_TEXT);
		}
		if (hClipMemory==NULL)	return CHtmlView::PreTranslateMessage(pMsg);
		LPBYTE lpClipMemory = (LPBYTE)GlobalLock(hClipMemory);//三、锁定剪切板
		CString strLink = CString(lpClipMemory);//四、获取剪切板内容
		GlobalUnlock(hClipMemory);//五、解锁剪切板
		::CloseClipboard();//六、关闭剪切板

		CString strPath;
		strPath.Format("D:\\book\\%06d.png",indexBook);//%06d 表示6位整数,不足6位前面补零
		//GetUrlCacheEntryInfo();
		//CString strLink=bstrtemp2;//获取当前url地址
		URLDownloadToFile(NULL,strLink,strPath,0,NULL);
		indexBook++;
	}
	return CHtmlView::PreTranslateMessage(pMsg);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值