Programming the MSHTML Web Browser Control with C++

这篇文章介绍了在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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值