Transferred from: http://www.cnblogs.com/javaexam2/archive/2012/07/14/2632959.html
① needs a class cwebpage written by someone else , adding its two files WebPage.h and WebPage.cpp to the project.
Cwebpage class header file and implementation file:
http://download.csdn.net/detail/masikkk/4427190
② Add the webBrowser control, #include "webbrowser2.h" in the header file of the View/dialog class , increase the member variable m_webbrowser, and call the Navigate () function to load a HTML page.
③ in the dialog box or view implementation file, add #include "WebPage.h", in the response function of the button can call JavaScript function, the specific code is as follows:
cwebpage web;
web. Setdocument (M_webbrowser.getdocument ());
CComVariant Varresult; Store return value
Note: Parameters are passed in string type
web. Calljscript ("Tansto", M_latitude, M_longitude, &varresult); //Call js function tansto (), and pass 2 parameters (latitude, longitude varresult
if (varresult dblval = = 122.211) //double type corresponding dblval, shaping corresponding intval
MessageBox (" success in obtaining variable values from JS ");
④ defines the corresponding function in the script tag of the HTML file, with the following code:
<script type= "Text/javascript" >
function Tansto (lat, LNG)//go to the specified position with two parameters, latitude and longitude
{
Map.panto (New glatlng (lat, LNG)); Google Maps API, jump to coordinates specified location
return 122.211; Returns the value of a double type to varresult
}
</script>
Attention:
(1) the panto () function in JavaScript above is a function in the Google Map API that can customize the relevant function according to different needs.
(2) The above implementation does not need to tube cwebpage class what role, directly with the line, if you want to learn more about the content, please search for "COM component Automation" "IDispatch interface" article.
JavaScript functions are called through the Cwebpage class in VC/MFC (to pass parameters to the JS function and get the return value)