Reputation: 1516
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.
Upvotes: 0
Views: 3168
Reputation: 7952
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!
Upvotes: 1