带有 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?

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

问题描述

我们有一个传统的 C++ (MFC) 应用程序.它的一部分承载了一个 CWebBrowser2 对象,我们在其中导航到一个 url,这是我们正在开发以添加到我们的应用程序的新模块.该模块是一个 HTML5 应用程序.由于 CWebBrowser2 使用 IE(当前为 IE8),因此我们传递给它的 URL 会加载 Chrome Frame 并传递给另一个 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,取第一个 ?并将 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.

简单.我的做法是错误的.不知道这对其他人是否是一个有用的问题/答案,但我想我会分享我实施的解决方案,因为它非常有效.

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

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