測試C++代碼與WebBrowser HTML的互動

testWebBrowserDlg.hhtml

// 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-20
版本號:3
Test Env: Windows8.一、VS2013 Update2
內容:
[1]如何拿到html中的elements,取得它的屬性!
[2]如何響應element激發的事件
[3]如何修改指定element的屬性
[4]如何Render內存中的html字符串

參考資料
[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]《Loading HTML content from a Stream》
http://msdn.microsoft.com/en-us/library/ie/aa752047%28v=vs.85%29.aspx
[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
[12]《Using IHTMLEditDesigner》
http://www.codeproject.com/Articles/6546/Using-IHTMLEditDesigner
[13]《MFC C++ WebBrowser Control load HTML from a string》
http://stackoverflow.com/questions/9179179/mfc-c-webbrowser-control-load-html-from-a-string
[14]VC++ webbrowser函數使用範例
[15]《【webbrowser使用】_webbrowser使用的相關文章,教程,源碼》
http://www.xuebuyuan.com/zt/12577882.html
*/

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;
	void SetElementAttribute(MSHTML::IHTMLDocument2Ptr htmlDoc,CString elementID,CString attributeName,CString value);
	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 OnLostFocus(MSHTML::IHTMLEventObj *pEvtObj);
	void WriteHTML(const wchar_t* html);

	afx_msg void OnBnClickedMemoryRender();

	//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 OnBnClickedBtnSetspecifiedelementattr();
	virtual void OnOK();
	virtual void OnCancel();
};

  testWebBrowserDlg.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_RENDERSTRING, &CtestWebBrowserDlg::OnBnClickedMemoryRender)
	ON_BN_CLICKED(IDC_BTN_SETSPECIFIEDELEMENTATTR, &CtestWebBrowserDlg::OnBnClickedBtnSetspecifiedelementattr)
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);
	m_webBrowser.Navigate(L"about:blank", 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);
}


//測試:Render內存中的HTML
void CtestWebBrowserDlg::OnBnClickedMemoryRender()
{
	m_webBrowser.Navigate(L"app://mymemory.page",NULL,NULL,NULL,NULL);
}

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_INTERFACE_MAP(CtestWebBrowserDlg, CCmdTarget)
	INTERFACE_PART(CtestWebBrowserDlg, DIID_HTMLElementEvents2, Dispatch)
END_INTERFACE_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_ONFOCUSOUT", DISPID_HTMLELEMENTEVENTS2_ONFOCUSOUT, CtestWebBrowserDlg::OnLostFocus, VT_EMPTY, VTS_DISPATCH)
END_DISPATCH_MAP()

void CtestWebBrowserDlg::BeforeNavigate2Explorer1(LPDISPATCH pDisp, VARIANT* URL, VARIANT* Flags, VARIANT* TargetFrameName, VARIANT* PostData, VARIANT* Headers, BOOL* Cancel)
{
	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();
	}

	//演示,render內存中的html頁面!
	if (strURL.Find(_T("app://mymemory.page")) >= 0)
	{
		*Cancel = TRUE;
		WriteHTML(L"<html><body><h1>My Header</h1><p>Some text below the header</p></body></html>");
		return;
	}
}


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

}//end func

//演示:鼠標點擊事件
void CtestWebBrowserDlg::OnClick(MSHTML::IHTMLEventObj *pEvtObj)
{
	MSHTML::IHTMLElementPtr elem = pEvtObj->srcElement;
	CString cstrID = elem->Getid();
	CString cstrTag = elem->GettagName();//標籤的名字

	if (cstrID.GetLength()<=0)
	{
		return;
	}
	
	_variant_t name = elem->getAttribute(_T("name"), 0);
	CString cstrName;
	if (name.vt != VT_NULL)
	{
		cstrName = name;
	}

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

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

//演示:響應 標籤 失去焦點事件
void CtestWebBrowserDlg::OnLostFocus(MSHTML::IHTMLEventObj *pEvtObj)
{
	MSHTML::IHTMLElementPtr elem = pEvtObj->srcElement;

	CString cstrID = elem->Getid();
	if (cstrID.GetLength()<=0)
	{
		return;
	}

	CString msg;
	msg.Format(L"OnLostFocus cstrID = [%s]", cstrID.GetBuffer());
	AfxMessageBox(msg);
}

//演示:拿到指定ID的標籤元素,並打印它的屬性
void CtestWebBrowserDlg::DemoGetElement(LPDISPATCH pDisp, VARIANT* URL)
{
	IWebBrowser2Ptr webBrowser(pDisp);
	IDispatchPtr htmlDocDisp;
	(*webBrowser).get_Document(&htmlDocDisp);
	MSHTML::IHTMLDocument2Ptr htmlDoc(htmlDocDisp);

	MSHTML::IHTMLElementCollectionPtr elements;
	(*htmlDoc).get_all(&elements);

	IDispatchPtr disp;
	_variant_t index(0L, VT_I4);
	do
	{
		disp = (*elements).item(_variant_t("myFontTag"), index);
		if (disp != NULL)
		{
			MSHTML::IHTMLElementPtr element(disp);

			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;
	}

	//打印HTML頁面內容
	MSHTML::IHTMLElementPtr body = htmlDoc->Getbody();
	variant_t html = body->parentElement->outerHTML;
	//variant_t bodyHTML = body->GetouterHTML();
	CString cstrBodyHTML = html;
	TRACE(L"cstrBodyHTML.GetBuffer()========\n%s\n", cstrBodyHTML.GetBuffer());


	//取HTML中的元素
	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;
				}
			}//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、DIID_HTMLAnchorEvents二、DIID_HTMLButtonElementEvents
		AfxConnectionUnadvise(itr->first,  itr->second.iid, GetIDispatch(FALSE), FALSE, itr->second.cookie);
	}
	m_mapElem2EventCookie.clear();
}

//測試,設置當前頁面指定元素的屬性
void CtestWebBrowserDlg::OnBnClickedBtnSetspecifiedelementattr()
{
	CComPtr<IDispatch> spDisp = m_webBrowser.get_Application();
	if (spDisp != NULL)
	{
		CComPtr<IWebBrowser2> spWeb;
		HRESULT hr = spDisp->QueryInterface(IID_IWebBrowser2, (void**)&spWeb);
		if (SUCCEEDED(hr))
		{
			IDispatchPtr htmlDocDisp;
			spWeb->get_Document(&htmlDocDisp);
			MSHTML::IHTMLDocument2Ptr htmlDoc(htmlDocDisp);

			if (htmlDoc == NULL) //URL屬性爲空
			{
				return;			
			}

			SetElementAttribute(htmlDoc, L"firstname", L"value", L"Marcia");
			SetElementAttribute(htmlDoc, L"lastname",L"value", L"JohnDoe");
			SetElementAttribute(htmlDoc, L"female",L"checked", L"1");
			SetElementAttribute(htmlDoc, L"bike",L"checked", L"");
			SetElementAttribute(htmlDoc, L"car",L"checked", L"1");

			//演示,重定向到其它URL
			//CComVariant varURL("http://www.intel.com");
			//spWeb->Navigate2(&varURL, NULL, NULL, NULL, NULL);
		}
	}

}

void CtestWebBrowserDlg::SetElementAttribute(MSHTML::IHTMLDocument2Ptr htmlDoc, CString elementID, CString attributeName, CString value)
{
	MSHTML::IHTMLElementCollectionPtr elements;
	htmlDoc->get_all(&elements);

	IDispatchPtr disp;
	_variant_t index(0L, VT_I4);
	do
	{
		disp = (*elements).item(_variant_t(elementID.GetBuffer()), index);
		if (disp != NULL)
		{
			MSHTML::IHTMLElementPtr element(disp);

			element->setAttribute(attributeName.GetBuffer(), value.GetBuffer(),0);

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

/*測試render內存中的html*/
void CtestWebBrowserDlg::WriteHTML(const wchar_t* html)
{
	IDispatch* pHtmlDoc = m_webBrowser.get_Document();

	/*
    在調用這段代碼以前,若是你尚未url須要navigate,就必須在
	OnInitDialog中插入下面的代碼,不然拿不到document!
	m_webBrowser.Navigate(L"about:blank",NULL,NULL,NULL,NULL);
	*/
	if (!pHtmlDoc)
		return;

	CComPtr<IHTMLDocument2> doc2;
	doc2.Attach((IHTMLDocument2*)pHtmlDoc);
	if (!doc2)
		return;
	// Creates a new one-dimensional array
	SAFEARRAY* psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
	if (!psaStrings)
		return;
	BSTR bstr = SysAllocString(html);
	if (bstr)
	{
		VARIANT* param;
		HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)¶m);
		if (SUCCEEDED(hr))
		{
			param->vt = VT_BSTR;
			param->bstrVal = bstr;
			hr = SafeArrayUnaccessData(psaStrings);
			if (SUCCEEDED(hr))
			{
				doc2->write(psaStrings);
				doc2->close();
			}
		}
	}
	// SafeArrayDestroy calls SysFreeString for each BSTR!
	if (psaStrings)
		SafeArrayDestroy(psaStrings);
}

//退出前要釋放連接
void CtestWebBrowserDlg::OnOK()
{
	ReleaseHTMLConnection();
	CDialogEx::OnOK();
}


void CtestWebBrowserDlg::OnCancel()
{
	ReleaseHTMLConnection();
	CDialogEx::OnCancel();
}

  test.htmlweb

<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
    <h1 id="myH1Tag">測試從H1標籤能不能獲得事件</h1>
    <font id="myFontTag" color=#5a6571>測試能不能從font標籤獲得事件</font> <br />
    <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>

    <form>
        First name: <input id='firstname' type='text' name='firstname' /><br />
        Last name: <input id='lastname' type='text' name='lastname' /><br />
        Password: <input id='password' type='password' name='pwd' /><br><br />
        <input type='radio' id='male' name='sex' value='male' />Male<br />
        <input type='radio' id='female' name='sex' value='female' />Female<br /><br />
        <input type='checkbox' id='bike' name='vehicle' value='Bike' />I have abdsmasterbike<br />
        <input type='checkbox' id='car' name='vehicle' value='Car' />I have a car <br /><br />
        <input type='button' id='ok' value='OK' /><br />
        <input type='button' id='cancel' value='Cancel' /><br /><br />
    </form>
</body>

  

轉載自:http://blog.csdn.net/lee353086/article/details/38537415windows

 

程序員的基礎教程:菜鳥程序員瀏覽器

相關文章
相關標籤/搜索