1.在VC++工程中创建继承CHtml类的CMapView类,该类用于实现网页与VC++代码交互接口。
class CMapView : public CHtmlView
{
}
2.在CMapView类构造函数中添加:
EnableAutomation();
下面是微软官方文档对EnableAutomation()的定义:
Call this function to enable OLE automation for an object.
This function is typically called from the constructor of your object and should only be called if a dispatch map has been declared for the class. For more information on automation see the articles Automation Clients and Automation Servers.
只用在构造函数中调用了EnableAutomation(),CMapView()才能获取到网页的信息,否则会报错如下:
Debug Assertion Failed! ............For information on how your program can cause an assertion failure,see the Visual C++ documentation on asserts.
注:OLE automation现在已经更名为automation,它是一种机制,这种机制允许一个应用程序调用在另外一个应用程序实现的对象或者允许一个应用程序暴露对象供其他应用程序调用。
3.重写OnGetExternal()
微软官方文档对OnGetExternal()函数解释如下:
Override OnGetExternal to react to the GetExternal notification from the Microsoft Web Browser control. See IDocHostUIHandler::GetExternal in the Windows SDK for more information.
OnGetExternal()函数的主要作用就是从网页获取消息,如果重写此函数,网页消息无法映射到CMapView类,提示错误:当前页面的脚本发生错误:无法获取属性“函数名”的值:对象null或未定义。
并且值得OnGetExternal()调用遵循OLE机制,所以调用OnGetExternal()之前必须要在构造函数中调用EnableAutomation()。
3.1在CMapView.h文件中添加代码:
virtual HRESULT OnGetExternal(LPDISPATCH *lppDispatch);
3.2在
CMapView.cpp文件中添加代码:
HRESULT CMapView::OnGetExternal(LPDISPATCH *lppDispatch)
{
*lppDispatch = GetIDispatch(TRUE);
return S_OK;
}
4.添加消息响应函数
4.1在CMapView.h文件中添加代码:
DECLARE_DISPATCH_MAP()
4.2在
CMapView.cpp文件中添加代码:
BEGIN_DISPATCH_MAP(CMapView, CCmdTarget)
DISP_FUNCTION(CMapView, "CB_TDOALocation",CB_TDOALocation,VT_EMPTY, VTS_BSTR)
DISP_FUNCTION(CMapView, "CB_TDOAStopLocation",CB_TDOAStopLocation,VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()
DISP_FUNCTION在一个dispatch map中定义一个OLE automation 函数,下面是他的语法结构:
DISP_FUNCTION(theClass, pszName, pfnMember, vtRetVal, vtsParams )
vtRetVal和vtsParams包括很多种类型,更多信息详见MSDN中对DISP_FUNCTION的定义:ms-elp://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vclib/html/8a1c666b-304b-4641-ab20-4f1a6d3c2d2e.htm