问题
We have a legacy C++ (MFC) application. One part of it hosts a CWebBrowser2 object in which we navigate to a url which is a new module we're developing to add to our application. The module is an HTML5 application. Since CWebBrowser2 uses IE (currently IE8) the URL we pass to it loads Chrome Frame and passes to that another URL which is our module.
So, the url is something like this (note: user cannot input this, it's configured by us per-site):
http://server/ChromeFrameWrapper.htm?http://server/Module.htm?Param1=something&Param2=somethingElse
Inside ChromeFrameWrapper.htm we have code that will grab the url, take the portion after the first ? and set chromeFrame.src to that, thus our module shows up nicely within our C++ application.
Now the problem. Within this HTML5 module we have some navigation buttons (note: it could be an anchor if that's what's needed). What we want to have happen is when the user clicks one of these buttons, the main application (C++ MFC app) is notified of the click. Since there are several buttons we need to know which button it was as we will navigate to different locations within the C++ app based on what's clicked (we do have reasons for this, it's just step one in our complete replacement of the C++ app).
In the C++ app I'm able to obtain the IHTMLDocument2 object and the collection of tags, however this is for the ChromeFrameWrapper HTML. That's as far as I've been able to go so far.
- Would it even be possible to hook something up so our C++ app is able to receive events of the clicks from the HTML5 module?
- If so, how? I'm stuck and haven't been able to find any examples involving Chrome Frame hosted HTML and events. Is using the MSHTML the right way, or is there something else?
Here's the SCRIPT and main HTML for ChromeFrameWrapper.htm. Note, we're trying to prove it works so no code critique unless it solves my problem.
Thanks for any help and insight.
<SCRIPT type="text/javascript">
function GetChromeFrame() {
var chromeFrame = window.document.ChromeFrame
return chromeFrame;
}
function onLoad() {
var theUrl = window.location.href;
// just grab the arguments and pass them as is
var chromeFrameSource = GetArgumentsFromUrl(theUrl);
var chromeFrame = GetChromeFrame();
chromeFrame.src = chromeFrameSource;
}
function GetArgumentsFromUrl(theUrl)
{
if(theUrl.indexOf("?") != -1)
{
return theUrl.substring(theUrl.indexOf("?") + 1);
}
return "";
}
</SCRIPT>
<BODY onload="onLoad();">
<center>
<OBJECT ID="ChromeFrame" WIDTH=1060 HEIGHT=800 CODEBASE="http://www.google.com" CLASSID="CLSID:E0A900DF-9611-4446-86BD-4B1D47E7DB2A">
<PARAM NAME="src" VALUE="http://www.google.com" />
<embed ID="ChromeFramePlugin" NAME="ChromeFrame" SRC="http://www.google.com" TYPE="application/chromeframe"/>
</OBJECT>
</center>
</BODY>
回答1:
As Shane Holloway suggested this was accomplished using WebSockets. I opened a listening socket in my MFC application then had the HTML5 application open a websocket to connect to the socket in the MFC application.
Simple. I was approaching it all wrong. Don't know if this will be a useful question/answer to anybody else, but thought I'd share the solution I implemented as it works GREAT.
来源:https://stackoverflow.com/questions/12800822/c-mfc-app-with-cwebbrowser2-chromeframe-html5-app-how-to-get-click-ev