C++ Builder and Dynamic HTML

Published:
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ...

Some objects defined and used in this source include:

   THTMLEvent
   THTMLElement
   THTMLTxtRange
   THTMLStyleSheet
   THTMLDocument
   THTMLTable , ....


Steps to use this code:

1

Put a TWebBrowser component on your form.

2

Put a TButton component on your form.

3

Define an instance of THTMLDocument:
THTMLDocument document; //in global scope

Open in new window

4

In your OnButtonClick event, write this code:
WebBrowser1->Navigate("http://www.google.com");  // test with this URL

Open in new window

5

In your OnWebBrowser1DocumentComplete handler, write this code:
document.webBrowser = WebBrowser1;

Open in new window

6

Now the document object is ready to use.
Sample usage:
THTMLElement elm = document.getElementById("gog");
                      ShowMessage(elm.innerHTML);

Open in new window

7

For event handling needs, define a global instance of THTMLElement:
THTMLElement myElm;

Open in new window

8

In your OnWebBrowser1DocumentComplete handler, write this code:
document.webBrowser = WebBrowser1;
                      myElm = document.getElementById("gog");
                      myElm.onclick = &OnClick;

Open in new window

9

OnClick defines as:
In public section of TForm1 write:
void OnClick();

Open in new window

...and in the CPP file write:
void TForm1::OnClick()
                      {
                           THTMLEventObj event = document.parentWindow.event;
                           ShowMessage(event.x); // mouse pos
                           ShowMessage(event.ctrlKey); // if ctrl pressed
                           event.returnValue = 0; // for cancel event
                      ...
                      }

Open in new window

Notes:
Supported: BCB 2009, 2010
Not supported BCB XE yet;

Files:
html.cpp
html.h
1
9,087 Views

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.