-->

C++, win32 api: How to create an html rendering wi

2019-05-10 10:28发布

问题:

What I need is simple: we have a console app project. We want to have such a function that would open a simple window with nothing but html (default system based) html + js rendering window that would read some default html+js string (form string or const char*). we want to have our own api joined with default js api so that when JS calls some our.function(argument); we would have our C++ application performe some function and for example cout argument. How to create such thing on windows?

回答1:

  1. CoCreate the MSHTML com object for HTML Documents:

    CComPtr spDoc; HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (void**)&spDoc);

  2. Do something like this to read your HTML string and make the document render it.

  3. Depending on what you need in terms of callbacks, you can use the COM DOM Interfaces to traverse the tree, and then sink the appropriate DispInterfaces to get events on the elements you're interested in. I would recommend this.

  4. If what I suggest in #3 isn't good enough for you (and I'd like to hear why) then you can implement your own ActiveX control and have script on your page call methods on it as suggested by raj.



回答2:

Brushing aside any security / cross browser/platforms concerns you can use implement an ActiveX object in your C++ that you can invoked from javascript.

http://msdn.microsoft.com/en-us/library/7sw4ddf8(v=vs.94).aspx



回答3:

The host for the WebBrowser control can provide an object that will be accessible to scripts via the external object.

See http://msdn.microsoft.com/en-us/library/aa770041.aspx#GetExternal