Search notes:

Programming the MSHTML Web Browser Control with C++

2018-12-06: moved from http://www.adp-gmbh.ch/win/misc/mshtml/
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 thought 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 on a now defunct link on codeproject.com (the title of the link was: Embed an HTML Control in your own window using plain C.).

Displaying an URL

The following program takes one argument which is an URL to be displayed:
#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;
}
Github repository cpp-MSHTML, path: /DisplayHTML.cpp
The executable can be called from cmd.exe like so:
C:\> DisplayHTML.exe www.your-favorite-url.zzz

Rendering HTML

Instead of an URL, it's also possible to render HTML (as text) with the following simple program:
#include <windows.h>
#include <string>

#include "HTMLWindow.h"

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

  char curDir[256];
  GetCurrentDirectoryA(255, curDir);

  std::string html = 
           "<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='https://www.google.com'>Google</a>"
           "<li><a href='https://www.renenyffenegger.ch'>renenyffenegger.ch</a>"
           "<li><a href='https://github.com/ReneNyffenegger/cpp-MSHTML'>github repository</a>"
       //
       //  Unfortunately, file:// links don't seem to work:
       //
       //  "<li><a href='file://"; html += curDir;  html += "/test/index.html'>test/index.html</a>"
           ;
  html += "</ul>"
          "</body></html>",
        // Parameter title 
          "MSHTMLTest";
//MessageBox(0, html.c_str(), 0, 0);

  HTMLWindow* html_window = new HTMLWindow (
         html,         // HTML text (or url)
        "MSHTML Test", // Title
         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;
}
Github repository cpp-MSHTML, path: /MSHTMLTest.cpp
When executed, it will open a Window like so:

github repository

The source code is hosted on github at github.com/ReneNyffenegger/cpp-MSHTML.
I was able to compile with MinGW.

TODO: EventReceiverTest.cpp

EventReceiverTest.cpp is supposed to handle events (such as mouse clicks) in a MSHTML application.
However, I didn't find this file anymore somewhere in one of my backups but I was able to recover a version from archive.org.
EventReceiverTest.cpp also needs HTMLWindow.cpp etc. However, the version of HTMLWindow.cpp that I found on archive.org was different from the one in my local source code repository.
Therefore, I stored the archive.org version under HTMLWindow2.cpp and HTMLWindow2.h, respectively.
Also, when compiling EventReceiverTest.cpp along with HTMLWindow2, the produced executable does not work properly.

See also

Microsoft HTML Obect Library

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1741107219, '3.140.240.187', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/development/MSHTML/index(171): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78