处理C ++代码中的HTML按钮单击事件 [英] Handling an HTML button click event in C++ code

查看:533
本文介绍了处理C ++代码中的HTML按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows本机C ++ / Win32 / MFC对话框应用程序。我正在使用对话框中的IE ActiveX控件来呈现一些HTML内容。正在呈现的HTML包含一个按钮。

 < input type =buttonid =uniqueButtonIDname = uniqueButtonNamevalue =blueonClick =OnButtonClick('blue');> 

目前,按钮点击是通过显示的JavaScript处理程序在页面中处理的。这一切都奏效。



我希望在对话框C ++代码中处理按钮单击。



我有一些处理对话中其他事件的经验。例如,下面的工作,并允许处理文档完整的事件和导航。

  BEGIN_EVENTSINK_MAP(DMyDlg,CDialog)
ON_EVENT(DMyDlg,IDC_EXPLORER2,259,DMyDlg :: DocumentCompleteExplorer2,VTS_DISPATCH VTS_PVARIANT)
ON_EVENT(DMyDlg,IDC_EXPLORER2,250,DMyDlg ::在BeforeNavigate2,VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
END_EVENTSINK_MAP()

虽然这些是预先定义的众所周知的事件。不知道如何将其转化为处理我在onClick =部分的按钮中定义的内容。



任何人都知道如何做到这一点?这里的动机是我在另一个定义了一些业务逻辑的C ++应用程序中使用了一些代码。我想在这里使用相同的业务逻辑。目前我必须每次将它翻译成Javascript。如果我可以在C ++代码中处理事件,那么我可以复制/粘贴(或通过DLL重新使用),并避免使用Javascript翻译阶段。

解决方案

你需要创建一个COM对象作为JavaScript和C ++之间的桥梁。 JavaScript初始化代码如下所示:

  var myhelper = new ActiveXObject(MyCompany.MyHelper); 

请注意,尽管名称为ActiveXObject,但该对象不必是完整的ActiveX控件,只是一个COM对象。然后,在您的onClick处理程序中,您可以执行以下操作:

  myhelper.DoThis(); 
myhelper.DoThat();

fun部分是用C ++创建COM对象--IDL文件,ATL类,IDispatch, IUnknown以及所有我想忘记的东西。



但有一点 - 如果C ++代码存在于您的应用程序exe中(而不是在单独的COM DLL中)不要忘了,当你的程序启动时,你必须为MyCompany.MyHelper创建一个类工厂实例,并将它注册到COM中,以便JavaScript中的新ActiveXObject成功。



[update 1]

我应该在最初的回复中添加 - 如果可以避免,我实际上不建议这样做。在C#和.NET存在之前,我们公司实际上虽然COM是一件好事,但值得花时间和精力来学习它。今天...没有那么多。

在你的情况中,使用C ++和JavaScript再次使用业务逻辑可能看起来像很多额外的工作 - 但至少它是您可以计划的额外工作,分配资源并有希望能够完成。一旦你走下了C ++ / COM / ActiveScripting的道路,当东西停止工作时 - 你可能花费的时间来追逐模糊的COM相关问题的时间没有限制。无论如何祝你好运!


I have a Windows native C++/Win32/MFC dialog app. I'm using the IE ActiveX control in the dialog to render some HTML content. The HTML being rendered contains a button. The button has an onClick javascript handler as shown below.

<input type="button" id="uniqueButtonID" name="uniqueButtonName" value="blue" onClick="OnButtonClick('blue');">

Currently the button click is handled in the page by the javascript handler shown. This all works.

I'd like to, instead, handle the button click in the dialog C++ code.

I have some experience handling other events in the dialog. For example, the below works and allows handling the doc complete event and navigation.

BEGIN_EVENTSINK_MAP(DMyDlg, CDialog)
ON_EVENT(DMyDlg, IDC_EXPLORER2, 259, DMyDlg::DocumentCompleteExplorer2, VTS_DISPATCH VTS_PVARIANT)
ON_EVENT(DMyDlg, IDC_EXPLORER2, 250, DMyDlg::BeforeNavigate2, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
END_EVENTSINK_MAP()

These are pre-defined well known events though. Not sure how to translate that into handling something I've defined in the button in the onClick="" section.

Anyone know how to do this? The motivation here is that I have some code used in another C++ app which defines some business logic. I want the same business logic used here. Currently I have to translate that into Javascript every time. If I could handle the event in the C++ code I could just copy/paste (or re-use through a DLL) and avoid the Javascript translation stage.

解决方案

You will need to create a COM object as the bridge between JavaScript and C++. The JavaScript initialization code will look something like:

var myhelper = new ActiveXObject("MyCompany.MyHelper");

Note that despite the name "ActiveXObject", the object does not have to be a full ActiveX control, just a COM object. Then in your onClick handler you can just do:

myhelper.DoThis();
myhelper.DoThat();

The "fun" part is creating the COM object in C++ - IDL files, ATL classes, IDispatch, IUnknown and all that stuff I am trying to forget.

One thing though - if the C++ code exists in your application exe (not in a separate COM DLL), don't forget that when your program starts up, you must create a class factory instance for "MyCompany.MyHelper" and register it with COM so that the "new ActiveXObject" in JavaScript succeeds.

[update 1]

I should have added in my initial response - I don't actually recommend doing this if you can avoid it. Back before C# and .NET existed, we in our company actually though COM was a good thing and it was worth the time and effort to learn it. Today ... not so much.

In your case, having your business logic in C++ and again in JavaScript might seem like a lot of extra work - but at least it is extra work that you can plan, allocate resources to and have a hope of being able to finish. Once you go down the path of C++/COM/ActiveScripting, when stuff stops working - there is no limit to the amount of time you might spend trying to chase down obscure COM related issues. Anyhow good luck!

这篇关于处理C ++代码中的HTML按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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