这篇文章介绍了在c++中通过mshtml实现 浏览器的实例。
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 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
本文介绍了一种使用C++和MSHTML在普通Windows程序中渲染HTML的方法,通过两个示例程序展示了如何显示简单的HTML文档及从URL加载内容。
2532

被折叠的 条评论
为什么被折叠?



