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;
}
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;
}
When executed, it will open a Window like so:
TODO: EventReceiverTest.cpp
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.
Also, when compiling EventReceiverTest.cpp along with HTMLWindow2, the produced executable does not work properly.