当前位置:首页 > ?在MFC中使用CHtmlView与js交互 js调用C++的方法

?在MFC中使用CHtmlView与js交互 js调用C++的方法

点击次数:7451  更新日期:2014-07-31

在MFC中使用CHtmlView与js交互, js调用C++程序中的方法。(使用单文档或多文档模式)

1、在类定义时添加
DECLARE_DISPATCH_MAP()


如:

class CClientBrowserView : public CHtmlView

{

DECLARE_DISPATCH_MAP()

....
}

2、在构造函数中添加
EnableAutomation();

如:

CClientBrowserView::CClientBrowserView()

{

// TODO: 在此处添加构造代码

EnableAutomation();

}


3、在类实现文件前添加类似如下的映射

BEGIN_DISPATCH_MAP(CClientBrowserView, CHtmlView)

DISP_FUNCTION(CClientBrowserView, "PrintText", PrintText, VT_BOOL, VTS_BSTR)

END_DISPATCH_MAP()


即 BOOL PrintText(CString str);


4、重写OnGetExternal

HRESULT CClientBrowserView::OnGetExternal( LPDISPATCH *lppDispatch)

{

*lppDispatch = GetIDispatch(TRUE);

return S_OK;

}


==================================


在页面的JS脚本中可以这调用
<input type="button" value="按钮" onclick="window.external.PrintText('测试js调用mfc')"/>