简体   繁体   English

在C ++代码中处理HTML按钮单击事件

[英]Handling an HTML button click event in C++ code

I have a Windows native C++/Win32/MFC dialog app. 我有一个Windows本机C ++ / Win32 / MFC对话框应用程序。 I'm using the IE ActiveX control in the dialog to render some HTML content. 我在对话框中使用IE ActiveX控件来呈现一些HTML内容。 The HTML being rendered contains a button. 呈现的HTML包含一个按钮。 The button has an onClick javascript handler as shown below. 该按钮具有一个onClick javascript处理程序,如下所示。

<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. 当前,按钮的单击由页面上显示的javascript处理程序处理。 This all works. 所有这一切。

I'd like to, instead, handle the button click in the dialog C++ code. 相反,我想处理对话框C ++代码中的按钮单击。

I have some experience handling other events in the dialog. 我在处理对话框中的其他事件方面有一些经验。 For example, the below works and allows handling the doc complete event and navigation. 例如,以下工作原理并允许处理doc complete事件和导航。

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. 不知道如何将其转换为处理我在onClick =“”部分的按钮中定义的内容。

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. 这里的动机是我在另一个定义一些业务逻辑的C ++应用程序中使用了一些代码。 I want the same business logic used here. 我想要这里使用相同的业务逻辑。 Currently I have to translate that into Javascript every time. 目前,我每次都必须将其翻译成Javascript。 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. 如果我可以用C ++代码处理事件,则可以复制/粘贴(或通过DLL重用),并避免Javascript转换阶段。

You will need to create a COM object as the bridge between JavaScript and C++. 您将需要创建一个COM对象作为JavaScript和C ++之间的桥梁。 The JavaScript initialization code will look something like: JavaScript初始化代码如下所示:

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. 请注意,尽管名称为“ ActiveXObject”,但对象不一定是完整的ActiveX控件,而只是COM对象。 Then in your onClick handler you can just do: 然后,您可以在onClick处理程序中执行以下操作:

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. “乐趣”部分是在C ++中创建COM对象-IDL文件,ATL类,IDispatch,IUnknown以及所有我想忘记的东西。

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. 但是,有一件事-如果您的应用程序exe中存在C ++代码(而不是在单独的COM DLL中),请不要忘记,当程序启动时,必须为“ MyCompany.MyHelper”创建一个类工厂实例,并向其注册COM,以便JavaScript中的“新ActiveXObject”成功。

[update 1] [更新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. 早在C#和.NET存在之前,尽管COM是一件好事,但我们公司中的我们实际上还是值得花时间和精力来学习它的。 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. 就您而言,在C ++中使用业务逻辑以及在JavaScript中使用业务逻辑似乎是很多额外的工作-但至少您可以计划,分配资源并希望能够完成这些额外的工作。 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. 一旦走上C ++ / COM / ActiveScripting的道路,当东西停止工作时,您可以花费大量时间来解决晦涩的COM相关问题。 Anyhow good luck! 无论如何,祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM