Easiest way to embed HTML/CSS/JS content in a Windows (XP +) C++ application?
I have an app written in C++ using Visual Studio 2010 to run on Windows (version XP upwards) as an .EXE. It uses plain Win32 for the existing UI.
I also have some content based on web browser formats, HTML/CSS/JavaScript. I would like to have this content displayed in the application window, in the same way that WebView works on Android and UIWebV开发者_如何转开发iew works on iPhone. The web content should be able to communicate with the surrounding native application using calls to custom JavaScript methods.
I am aware of the WebKit project. However, looking at the binaries available for download it appears to be presented as a stand-alone application, rather than a library that can be linked against a C++ app to allow browser content to displayed.
Can anyone suggest a good way of doing this?
If you're using MFC, Patrick's answer is correct.
If you're not using MFC, you can embed Internet Explorer using "ATL control containment" - see How to add ATL control containment support to any window in Visual C++.
It boils down to linking with the right libraries and then using this one-liner:
// Creates the Web Browser control and navigates to the
// specified web page.
HWND hWnd = ::CreateWindow("AtlAxWin", "http://www.microsoft.com",
WS_CHILD|WS_VISIBLE, 10, 10, 500, 300, hParent, NULL,
::GetModuleHandle(NULL), NULL);
Getting your JavaScript to call functions in your C++ application is a bit of a fiddle - you need to create an object that implements IDispatch
, then pass that to the Advise
method of the IE control's IConnectionPoint
interface, which you obtain via IConnectionPointContainer::FindConnectionPoint
. Your JavaScript then calls window.external.my_func(...)
which becomes a call to the Invoke
method of your IDispatch
-implementing object.
You could try to include the Internet Explorer as a COM component in your application. This is explained on http://msdn.microsoft.com/en-us/library/aa752046(VS.85).aspx.
You could also use the QtWeb library (see http://www.qtweb.net/).
精彩评论