这是CodeGuru上的一片文章,对你有用:
Introduction
Sometimes, when we are using the IE Browser Control inside of a C++ application, we need to access the HTML elements. We can do it by using standard COM objects such as IWebBrowser2, IHTMLDocument2, and so forth. By doing this, we easily can implement features such as click button, click anchor, get input string, get HTML text, and so on. Unfortunately, Microsoft did not provide similar objects for JavaScript. In any case, it is possible to make a control for the JavaScript object inside an HTML page by using a traditional COM approach. This article describes the class CWebPage that allows you to do it and a technique to call a JavaScript function from C++ code.
How to Do This
As the result of using the presented class, it will be easy to call any JavaScript function from C++ code. To implement this feature, we should get a pointer to the IHTMLDocument2 interface. If we are using the CHtmlView class from MFC, we can get one by using member function CHtmlView::GetHtmlDocument(). In the case of using the IWebBrowser or IWebBrowser2 components, the function get_Document will bring us the desired interface. Here is an example:
Calling Technique
The previously mentioned technique splits the following steps:
Getting a pointer to the IHTMLDocument2 interface.
Getting IDispatch for a JavaScript object in an HTML document.
Getting DISPID for a given name of a JavaScript function.
Putting parameters to the DISPPARAM structure.
Calling a JavaScript function by using the Invoke method of the IDispatch interface.
Here is an example of getting a IDispatch pointer to the Java Scripts objects:
Notes About the Demo
To call a JavaScript function from the demo, you should select the function in the tree of the left window. After this, press the ! button on the menu bar.