且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-30 19:39:28

你需要创建一个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!