本功能不仅实现网页和控件的简单交互,还实现了点击网页网页内容被修改。
1. 通过点击网页中的网页链接,调用脚本;
2. 脚本调用控件里的函数,控件里的函数把结果返回;
activeXtest.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ActiveX</title>
<script language = 'javascript'>
function GetUrl4(id)
{
var items=document.getElementById(id);
var strResult = MyActiveX.changeContent(items.href);
var oNewDoc = document.open("text/html", "replace");
oNewDoc.write(strResult);
oNewDoc.close();
}
</script>
</head>
<body>
<object id="MyActiveX" classid="clsid:319388B4-E66E-4646-B265-29312E86EC9A"> </object>
<fieldset>
<legend>MyActiveX</legend>
<table width="100%" border="1">
<tr>
<td width="20%"><a id="baidu" href="http://www.baidu.com" οnclick='GetUrl4(this.id);return false;'>百度2http://www.baidu.com</a> </td>
</tr>
<tr>
<td width="20%"><a id="soho" href="http://www.sohochina.com" οnclick='GetUrl4(this.id);return false;'>搜狐http://www.sohochina.com</a> </td>
</tr>
<tr>
<td width="20%"><a id="example" href="http://example.com" οnclick='GetUrl4(this.id);return false;'>http://example.com</a> </td>
</tr>
</table>
</fieldset>
</body>
</html>
MyActiveXCtrl.cpp的片段代码
BSTR CMyActiveXCtrl::changeContent(BSTR url)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString strResult;
strResult = COLE2T(url);
// TODO: Add your dispatch handler code here
//char* lpszText2 = _com_util::ConvertBSTRToString(url);
// TODO:这里可以修改网页的代码。。。
if (strResult == "http://www.baidu.com/")
{
strResult = "<html>\
<head>\
<script type=\"text/javascript\">\
</script>\
</head>\
<body>\
百度</body>\
</html>";
}
else if (strResult == "http://www.sohochina.com/")
{
strResult = "<html>\
<head>\
<script type=\"text/javascript\">\
</script>\
</head>\
<body>\
搜狐</body>\
</html>";
}
else
{
strResult = "<html>\
<head>\
<script type=\"text/javascript\">\
</script>\
</head>\
<body>\
未知网页</body>\
</html>";
}
return strResult.AllocSysString();
}
代码下载网址:http://download.csdn.net/detail/liuyan20092009/9585883