René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback
 

Programming the MSHTML Web Browser Control with C++

Note: I have started to move some scripts and source code from this website to GitHub.

This affects some or all of the scripts found on this page. They should be found under
github.com/ReneNyffenegger/development_misc/ ... windows/mshtml.

I don't intend to maintain the scripts and or sources on this page any longer (so they might be outdated). But I will try to improve the code in the GitHub repository and accept Push Requests.

It is possible to render HTML in an ordinary Windows program with MSHTML. This makes it possible to have a web look and feel in a program. Because I think this is quite interesting, I decided to write some C++ classes that make it possible to use MSHTML in an easy way. I found some ideas on how to do that with Embed an HTML Control in your own window using plain C.

Test program

This test programm displays a simple HTML document in a window. The HTML document consists of three links. These links are fully operational.
MSHTMLTest.cpp
#include <windows.h>
#include "HTMLWindow.h"

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE /*unused__*/, LPSTR /*lpszCmdLine*/, int /*nCmdShow*/) {

  HTMLWindow* html_window = new HTMLWindow (

      // Parameter html_or_url:
      "<html><head>"
      "<title>MSHTMLTest</title>"   // seems a little useless in this context
      "</head><body>"
      "<h1>This is a test</h1>"
      "I offer the following links:"
      "<ul>"
      "<li><a href='http://www.google.com'>www.google.com</a>"
      "<li><a href='http://www.adp-gmbh.ch'>www.adp-gmbh.ch</a>"
      "<li><a href='http://www.yahoo.com'>www.yahoo.com</a>"
      "</ul>"
      "</body></html>",
      // Parameter title 
       "MSHTMLTest", 
       hInstance,
      false  // indicates: this not an url, but html 
       );

  MSG msg;
  while (GetMessage(&msg, 0, 0, 0)) {
    TranslateMessage(&msg);
    if (msg.message >= WM_KEYFIRST && 
        msg.message <= WM_KEYLAST) {
      ::SendMessage(html_window->hwnd_, msg.message, msg.wParam, msg.lParam);
    }
    DispatchMessage(&msg);
  }

  return 0;
}

Displaying an HTML document

The following program displayes an URL. So, it can be called like:
DisplayHTML.exe www.your-favorite-url.zzz
If the URL happens to be an HTML file on the harddisk, call it like so:
DisplayHTML.exe file://c:/path/to/your/document.html
DisplayHTML.cpp
#include <windows.h>
#include "HTMLWindow.h"

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) {

  if (__argc < 2) {
    ::MessageBox(0, "DisplayHTML.exe html-document", "Specify HTML document to be displayed", 0);
    return -1;
  }

  HTMLWindow* html_window = new HTMLWindow (
    __argv[1],
       "DisplayHTML", 
       hInstance,
       true  // it is an url 
     );

  MSG msg;
  while (GetMessage(&msg, 0, 0, 0)) {
    TranslateMessage(&msg);
    if (msg.message >= WM_KEYFIRST && 
        msg.message <= WM_KEYLAST) {
      ::SendMessage(html_window->hwnd_, msg.message, msg.wParam, msg.lParam);
    }
    DispatchMessage(&msg);
  }

  return 0;
}

Downloading MSHTMLTest

The sources (including makefile) as well as the exes can be downloaded here: MSHTMLTest. I compiled it with the mingw compiler.