From: Qing Pan network-http://www.tsingfun.com/html/2015/dev_0826/CDHtmlDialog_cpp_call_js_interactive.html
"The basic use of CDHtmlDialog (JS call C + + function implementation)"
Based on the above articles, this article expands on its code base:
First, add a C + + button on the main dialog box, as follows:
The operation effect is as follows:
Second, for C + + button to add call JS code:
Jscppinteractive.htm to add a JS function for C + + calls, the code is as follows:
<script type= "Text/javascript" >
function Cppcalljsfunc () {
alert ("JS Alert pop-up box. ");
}
</script>
Add the following code at the end of the JSCppInteractiveDlg.h:
HRESULT calljsfunction (ihtmldocument2* pDoc2,
CString strfunctionname,
dispparams dispparams,
variant* Varresult,
excepinfo* exceptinfo,
uint* nargerr);
Public:
afx_msg void OnBnClickedButton1 ();
Add the following code at the end of the JSCppInteractiveDlg.cpp:
void Cjscppinteractivedlg::onbnclickedbutton1 () {//TODO: Add control notification handler code here ihtmldocument2* PDoc = NULL;
Cdhtmldialog::getdhtmldocument (&pdoc);
Dispparams param = {0};
VARIANT Vtret;
Calljsfunction (PDoc, _t ("Cppcalljsfunc"), param, &vtret, null, NULL); HRESULT cjscppinteractivedlg::calljsfunction (ihtmldocument2* pDoc2, CString strfunctionname, Dispparam S Dispparams, variant* varresult, excepinfo* exceptinfo, uint* nargerr) {IDispatch *pdispscript
= NULL;
HRESULT HRESULT;
HResult = Pdoc2->get_script (&pdispscript);
if (FAILED (HResult)) {return S_FALSE;
} DISPID DISPID;
CComBSTR objbstrvalue = strfunctionname;
BSTR Bstrvalue = Objbstrvalue.copy ();
Olechar *pszfunct = Bstrvalue; HResult = Pdispscript->getidsofnames (Iid_null, &pszfunct, 1, Locale_system_default, &dispid)
;
if (FAILED (HResult)) {pdispscript->release ();
return hResult; } VARRESULT->VT = Vt_VARIANT;
HResult = Pdispscript->invoke (DispID, Iid_null, Locale_user_default, Dispatch_method, &dispparams,
Varresult, Exceptinfo, Nargerr);
Pdispscript->release ();
return hResult; }
The final operating effect is as follows:
Engineering code point for this download.