带有CWebBrowser2的C ++(MFC)应用-> ChromeFrame-> HTML5应用程序,如何从HTML5到C ++应用程序获取点击事件? [英] C++ (MFC) app with CWebBrowser2 -> ChromeFrame -> HTML5 app, how to get click events from the HTML5 to the C++ app?

查看:336
本文介绍了带有CWebBrowser2的C ++(MFC)应用-> ChromeFrame-> HTML5应用程序,如何从HTML5到C ++应用程序获取点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个遗留的C ++(MFC)应用程序.它的一部分包含一个CWebBrowser2对象,在其中我们导航到一个url,这是我们正在开发的一个新模块,将添加到我们的应用程序中. 该模块是HTML5应用程序.由于CWebBrowser2使用IE(当前为IE8),因此我们传递给它的URL会加载Chrome浏览器内嵌框架,并传递给另一个URL,即我们的模块.

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.

因此,URL是这样的(注意:用户无法输入,它是由我们每个站点配置的):

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

在ChromeFrameWrapper.htm内部,我们有将捕获URL的代码,占第一个URL后的部分?并将chromeFrame.src设置为该值,因此我们的模块可以在我们的C ++应用程序中很好地显示.

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.

现在是问题所在.在此HTML5模块中,我们有一些导航按钮(请注意:如果需要的话,可以将其作为锚点).我们想要发生的是,当用户单击这些按钮之一时,会向主应用程序(C ++ MFC应用程序)发出该单击通知. 由于有多个按钮,我们需要根据单击的内容导航到C ++应用程序中的不同位置(我们确实有原因,这只是我们完全替换C ++应用程序的第一步),因此我们需要知道它是哪个按钮.

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).

在C ++应用程序中,我可以获取IHTMLDocument2对象和标签集合,但这是针对ChromeFrameWrapper HTML的.到目前为止,我已经走了.

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.

  1. 是否有可能挂钩某些内容,以便我们的C ++应用能够接收来自HTML5模块的点击事件?
  2. 如果是这样,怎么办?我被困住了,还没有找到任何涉及Chrome Frame托管HTML和事件的示例.是使用MSHTML的正确方法,还是还有其他方法?

这是ChromeFrameWrapper.htm的脚本和主要HTML.请注意,我们正在尝试证明其有效,因此除非能解决我的问题,否则无需进行代码批评.

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.

感谢您的帮助和见识.

<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>

推荐答案

正如Shane Holloway所建议的那样,这是使用WebSockets完成的. 我在MFC应用程序中打开了一个侦听套接字,然后让HTML5应用程序打开了一个Websocket以连接到MFC应用程序中的套接字.

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.

简单.我正在处理所有错误. 不知道这是否会对其他人有用,但以为我会分享我在GREAT运作时实现的解决方案.

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.

这篇关于带有CWebBrowser2的C ++(MFC)应用-> ChromeFrame-&gt; HTML5应用程序,如何从HTML5到C ++应用程序获取点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆