MFC C++代码与WebBrowser HTML的互动(还需完善)

testWebBrowser.h

// testWebBrowserDlg.h : 头文件
//

#pragma once
#include "explorer1.h"


#import "C:\windows\system32\mshtml.tlb" // location of mshtml.tlb

#include <map>

#include <comdef.h>
#include <mshtml.h>
#include <mshtmdid.h>
/*
标题:研究C++代码与WebBrowser HTML的互动
Author:Kagula
Date:2014-08-03
Test Env: Windows8.1、VS2013 Update2
内容:
[1]如何拿到html中的elements,并取得它的属性!
[2]如何响应element激发的事件
[3]如何修改指定element的属性

参考资料
[1]《MFC中针对WebBrowser控件增加link链接点击事件监控》
http://www.mworkbox.com/wp/work/509.html
[2]《IWebBrowser2 interface》
http://msdn.microsoft.com/en-us/library/aa752127(VS.85).aspx
[3]《Handling HTML Element Events》
http://msdn.microsoft.com/en-us/library/bb508508(v=vs.85).aspx
[4]《如何从 VC web 浏览器应用程序中调用脚本函数》
http://support.microsoft.com/kb/q185127
[5]《MFC中针对WebBrowser控件增加link链接点击事件监控》
http://www.mworkbox.com/wp/work/509.html
[6]《How do I get the font color from a piece of HTML source code?》
http://stackoverflow.com/questions/7402347/how-do-i-get-the-font-color-from-a-piece-of-html-source-code
[7]《How to create a sink interface in a MFC-based COM client》
http://support.microsoft.com/default.aspx?scid=kb;en-us;181845
[8]《How To Use the Microsoft WebBrowser Control to Render HTML from Memory》
http://www.nuonsoft.com/blog/2010/03/24/how-to-use-the-microsoft-webbrowser-control-to-render-html-from-memory/comment-page-1/
[9]《How do I get the font color from a piece of HTML source code?》
http://stackoverflow.com/questions/7402347/how-do-i-get-the-font-color-from-a-piece-of-html-source-code
[10]《Using the WebBrowser control, simplified》
http://www.codeproject.com/Articles/3919/Using-the-WebBrowser-control-simplified
[11]《Microsoft Internet Explorer 5.5 behaviors》
http://msdn.microsoft.com/en-us/magazine/cc301528.aspx
*/

namespace kagula
{
	struct ConnectionInfo
	{
		IDispatch* dispatch;
		IID iid;		
		DWORD cookie;

		ConnectionInfo() {}
		ConnectionInfo(IDispatch *dispatch, IID iid, DWORD cookie)
		{
			this->dispatch = dispatch, this->iid = iid, this->cookie = cookie;
		}
	};
}
// CtestWebBrowserDlg 对话框
class CtestWebBrowserDlg : public CDialogEx
{
// 构造
public:
	CtestWebBrowserDlg(CWnd* pParent = NULL);	// 标准构造函数

// 对话框数据
	enum { IDD = IDD_TESTWEBBROWSER_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支持


// 实现
protected:
	HICON m_hIcon;

	// 生成的消息映射函数
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()

public:
	CExplorer1 m_webBrowser;
	std::map<IDispatch*, kagula::ConnectionInfo> m_mapElem2EventCookie;//用于释放Connection
	void ReleaseHTMLConnection();

	void DemoGetElement(LPDISPATCH pDisp, VARIANT* URL);
	void DemoGetAllLinkElement(LPDISPATCH pDisp, VARIANT* URL);

	void OnClick(MSHTML::IHTMLEventObj *pEvtObj);
	void OnMouseOver(MSHTML::IHTMLEventObj *pEvtObj);

	afx_msg void OnBnClickedBtnGetall();

	//added new three map macros
	DECLARE_EVENTSINK_MAP()
	DECLARE_DISPATCH_MAP()	
	DECLARE_INTERFACE_MAP()

	void BeforeNavigate2Explorer1(LPDISPATCH pDisp, VARIANT* URL, VARIANT* Flags, VARIANT* TargetFrameName, VARIANT* PostData, VARIANT* Headers, BOOL* Cancel);
	void DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL);
	afx_msg void OnClose();
};

testWebBrowser.cpp

// testWebBrowserDlg.cpp : 实现文件
//

#include "stdafx.h"


#include "testWebBrowser.h"
#include "testWebBrowserDlg.h"
#include "afxdialogex.h"

#include <string>

#include <afxctl.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CtestWebBrowserDlg 对话框
CtestWebBrowserDlg::CtestWebBrowserDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CtestWebBrowserDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CtestWebBrowserDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EXPLORER1, m_webBrowser);
}

BEGIN_MESSAGE_MAP(CtestWebBrowserDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_GETALL, &CtestWebBrowserDlg::OnBnClickedBtnGetall)
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// CtestWebBrowserDlg 消息处理程序

BOOL CtestWebBrowserDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO:  在此添加额外的初始化代码
	EnableAutomation();//没有这行代码会导致GetIDispatch(FALSE)失败!

	m_webBrowser.Navigate(L"D:\\Workspace\\testWebBrowser\\testWebBrowser\\test.html",NULL,NULL,NULL,NULL);

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CtestWebBrowserDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	CDialogEx::OnSysCommand(nID, lParam);
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CtestWebBrowserDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CtestWebBrowserDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CtestWebBrowserDlg::OnBnClickedBtnGetall()
{
	// TODO: Add your control notification handler code here
}
BEGIN_EVENTSINK_MAP(CtestWebBrowserDlg, CDialogEx)
	ON_EVENT(CtestWebBrowserDlg, IDC_EXPLORER1, 250, CtestWebBrowserDlg::BeforeNavigate2Explorer1, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
	ON_EVENT(CtestWebBrowserDlg, IDC_EXPLORER1, 259, CtestWebBrowserDlg::DocumentCompleteExplorer1, VTS_DISPATCH VTS_PVARIANT)
END_EVENTSINK_MAP()

/* 第三步: 某种事件(元素类型无关)和哪个响应函数连*/
BEGIN_DISPATCH_MAP(CtestWebBrowserDlg, CCmdTarget)
	DISP_FUNCTION_ID(CtestWebBrowserDlg, "HTMLELEMENTEVENTS2_ONCLICK", DISPID_HTMLELEMENTEVENTS2_ONCLICK, CtestWebBrowserDlg::OnClick, VT_EMPTY, VTS_DISPATCH)
	DISP_FUNCTION_ID(CtestWebBrowserDlg, "HTMLELEMENTEVENTS2_ONMOUSEOVER", DISPID_HTMLELEMENTEVENTS2_ONMOUSEOVER, CtestWebBrowserDlg::OnMouseOver, VT_EMPTY, VTS_DISPATCH)
END_DISPATCH_MAP()

/* 第二步(方案一):处理所有种类元素的事件 */
BEGIN_INTERFACE_MAP(CtestWebBrowserDlg, CCmdTarget)
	INTERFACE_PART(CtestWebBrowserDlg, DIID_HTMLElementEvents2, Dispatch)
END_INTERFACE_MAP()
/* 第二步(方案二): 只处理两种元素的事件*/
//BEGIN_INTERFACE_MAP(CtestWebBrowserDlg, CCmdTarget)
//	INTERFACE_PART(CtestWebBrowserDlg, DIID_HTMLButtonElementEvents2, Dispatch)
//	INTERFACE_PART(CtestWebBrowserDlg, DIID_HTMLAnchorEvents2, Dispatch)	
//END_INTERFACE_MAP()

void CtestWebBrowserDlg::BeforeNavigate2Explorer1(LPDISPATCH pDisp, VARIANT* URL, VARIANT* Flags, VARIANT* TargetFrameName, VARIANT* PostData, VARIANT* Headers, BOOL* Cancel)
{
	// TODO: Add your message handler code here
	CString strURL(URL->bstrVal);
	*Cancel = FALSE;
	if (strURL == _T("about:blank"))
		*Cancel = FALSE;
	else
	{
		if (strURL.Find(_T("ThePageNeverReach.htm")) > 0 )
		{//阻止跳转到指定页面!
			*Cancel = TRUE;
			return;
		}
	}

	if (!(*Cancel))
	{
		//进入新页面之前,先释放掉事件连接
		ReleaseHTMLConnection();
	}

	//演示:HTML元素属性或内容替换
}


void CtestWebBrowserDlg::DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL)
{
	//DemoGetElement(pDisp, URL);
	DemoGetAllLinkElement(pDisp, URL);
}

//演示:鼠标点击事件
void CtestWebBrowserDlg::OnClick(MSHTML::IHTMLEventObj *pEvtObj)
{
	MSHTML::IHTMLElementPtr elem = pEvtObj->srcElement;
	CString cstrID = elem->Getid();
	CString cstrTag = elem->GettagName();//标签的名字
	
	_variant_t name = elem->getAttribute(_T("name"), 0);
	CString cstrName;
	if (name.vt != VT_NULL)
	{
		cstrName = name;
	}

	_variant_t href = elem->getAttribute(_T("href"), 0);
	CString cstrHref;
	if (href.vt != VT_NULL)
	{
		cstrHref = href.bstrVal;
	}

	CString msg;
	msg.Format(L"[id=%s][name=%s][tag=%s][href=%s]", cstrID.GetBuffer(), 
		cstrName.GetBuffer(), cstrTag.GetBuffer(), cstrHref.GetBuffer(MAX_PATH));
	AfxMessageBox(msg);
}

//演示:MouseOver事件
void CtestWebBrowserDlg::OnMouseOver(MSHTML::IHTMLEventObj *pEvtObj)
{
	MSHTML::IHTMLElementPtr elem = pEvtObj->srcElement;

	CString cstrID = elem->Getid();

	TRACE(L"OnMouseOver cstrID = [%s]", cstrID.GetBuffer());
}

//演示:拿到指定ID的标签元素,并打印它的属性
void CtestWebBrowserDlg::DemoGetElement(LPDISPATCH pDisp, VARIANT* URL)
{
	// Get the HTML document.
	//IHTMLDocument2Ptr htmlDoc;
	//htmlDoc = m_browser.GetDocument();
	IWebBrowser2Ptr webBrowser(pDisp);
	IDispatchPtr htmlDocDisp;
	(*webBrowser).get_Document(&htmlDocDisp);
	MSHTML::IHTMLDocument2Ptr htmlDoc(htmlDocDisp);


	// Get the collection of elements.
	MSHTML::IHTMLElementCollectionPtr elements;
	(*htmlDoc).get_all(&elements);

	//演示:如何拿到id属性为"myFontTag"的元素,并获取它的属性
	IDispatchPtr disp;
	_variant_t index(0L, VT_I4);
	do
	{
		disp = (*elements).item(_variant_t("myFontTag"), index);
		if (disp != NULL)
		{
			// Examine their action attribute to determine what should be done.
			MSHTML::IHTMLElementPtr element(disp);

			//打印mytag标签color属性的值
			variant_t vtValue = element->getAttribute("color", 0);
			CString cstr = vtValue;
			TRACE(L"mytag标签的color属性为%s\n", cstr.GetBuffer(MAX_PATH));

			++index.lVal;
		}
	} while (disp != NULL);
}

/*
拿到元素,并做链接
[1]《AfxConnectionAdvise》
http://msdn.microsoft.com/en-us/library/b9h84ebk.aspx
[2]《How to create a sink interface in a MFC-based COM client》
http://support.microsoft.com/default.aspx?scid=kb;en-us;181845
[3]《同Document建立Connection》
http://www.popkistopki.ru/ch08e.htm
*/
void CtestWebBrowserDlg::DemoGetAllLinkElement(LPDISPATCH pDisp, VARIANT* URL)
{
	// Get the HTML document. //
	IWebBrowser2Ptr webBrowser(pDisp);
	IDispatchPtr htmlDocDisp;
	(*webBrowser).get_Document(&htmlDocDisp);
	MSHTML::IHTMLDocument2Ptr htmlDoc(htmlDocDisp);

	if (htmlDoc == NULL) //URL属性为空
	{
		return;
	}


	DWORD dwCookie = 0;
	// Get the collection of elements.
	MSHTML::IHTMLElementCollectionPtr elements;
	(*htmlDoc).get_all(&elements);

	IDispatchPtr disp;
	_variant_t index(0L, VT_I4);
	do
	{
		//Get all elements
		disp = (*elements).item(index, index);
		if (disp != NULL)
		{
			// Examine their action attribute to determine what should be done.
			IDispatchPtr element(disp);
			MSHTML::IHTMLElementPtr elemTag(disp);

			//第一步:建立Connection
			DWORD dwCookie = 0;
			BSTR name = NULL;
			elemTag->get_tagName(&name);
			if (name != NULL)
			{
				//is link!!!!
				LPUNKNOWN pUnkSink = GetIDispatch(FALSE);

				//关联全部类型元素
				if (AfxConnectionAdvise(element, DIID_HTMLElementEvents2, pUnkSink, FALSE, &dwCookie))
				{
					kagula::ConnectionInfo ci(element.GetInterfacePtr(), DIID_HTMLElementEvents2, dwCookie);
					m_mapElem2EventCookie[element.GetInterfacePtr()] = ci;
				}

				//只关联下面两种类型事件,解除注释
				//if (_tcsicmp(name, _T("a")) == 0)
				//{
				//	if (AfxConnectionAdvise(element, DIID_HTMLAnchorEvents2, pUnkSink, FALSE, &dwCookie))
				//	{
				//		kagula::ConnectionInfo ci(element.GetInterfacePtr(), DIID_HTMLAnchorEvents2,dwCookie);
				//		m_mapElem2EventCookie[element.GetInterfacePtr()] = ci;
				//		if (name != NULL)
				//		{							
				//			std::wstring wsName = name;//打印调试信息
				//			TRACE(L"get tag:%s, bind DIID_HTMLAnchorEvents2\n", wsName.c_str());
				//		}
				//	}
				//}
				//else if (_tcsicmp(name, _T("button")) == 0)
				//{
				//	if (AfxConnectionAdvise(element, DIID_HTMLButtonElementEvents2, pUnkSink, FALSE, &dwCookie))
				//	{
				//		kagula::ConnectionInfo ci(element.GetInterfacePtr(), DIID_HTMLButtonElementEvents2, dwCookie);
				//		m_mapElem2EventCookie[element.GetInterfacePtr()] = ci;
				//	}
				//}
			}//end if
			++index.lVal;
		}
	} while (disp != NULL);
}

//释放同HTML的Connection
void CtestWebBrowserDlg::ReleaseHTMLConnection()
{
	std::map<IDispatch *, kagula::ConnectionInfo>::iterator itr;
	for (itr = m_mapElem2EventCookie.begin(); itr != m_mapElem2EventCookie.end(); itr++)
	{//DIID_HTMLDocumentEvents
		AfxConnectionUnadvise(itr->first,  itr->second.iid, GetIDispatch(FALSE), FALSE, itr->second.cookie);//DIID_HTMLAnchorEvents2,DIID_HTMLButtonElementEvents
	}
	m_mapElem2EventCookie.clear();
}

void CtestWebBrowserDlg::OnClose()
{
	//在窗口关闭前,释放Connection,否则Process会挂掉
	ReleaseHTMLConnection();

	CDialogEx::OnClose();
}

test.html

<head>
    <title></title>
</head>
<body>
    <h1 id="myH1Tag">这是主页面,用于测试C++得到事件</h1>
    <font id="myFontTag" color=#5a6571>这是我的标签</font> <br />
    <input id="myInputTag" type="button" value="这里元素不会响应button类型事件"/><br/>
    <button id="myButtonTag" type="button">测试点击事件</button>
    <br />
    <br />
    <a id="idOfA" name="nameOfA" href="file:D:\Workspace\testWebBrowser\testWebBrowser\ThePageNeverReach.htm">测试禁止页面跳转</a><br/>
    <a id="id2OfA" name="name2OfA" href="file:D:\Workspace\testWebBrowser\testWebBrowser\HTMLPage.htm">测试用户点击链接, C++后台得到消息!</a>
</body>

ThePageNeverReach.html

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio">
<TITLE></TITLE>
</HEAD>
<BODY>
    <h1>不会跳转到这个页面!</h1>
</BODY>
</HTML>



你可能感兴趣的:(MFC C++代码与WebBrowser HTML的互动(还需完善))