Making a simple html editor in C++ Builder
Back in the day, I had to make a simple html editor. This can be accomplished in a few lines of code, by using a simple TCppWebBrowser (wb) component and the IHTMLDocument2 interface. You will also need a TMemo (memo1) control. Here is the source code. I hope it helps someone 🙂
1//---------------------------------------------------------------------------
2//author: Adrián Deccico - http://adrian.org.ar
3//---------------------------------------------------------------------------
4
5#include <vcl.h>
6#pragma hdrstop
7#include "mshtml.h"
8
9#include "Unit1.h"
10//---------------------------------------------------------------------------
11#pragma package(smart_init)
12#pragma link "SHDocVw_OCX"
13#pragma resource "*.dfm"
14TForm1 *Form1;
15//---------------------------------------------------------------------------
16__fastcall TForm1::TForm1(TComponent* Owner)
17 : TForm(Owner)
18{
19}
20//---------------------------------------------------------------------------
21void __fastcall TForm1::btnNavigateAndEditClick(TObject *Sender)
22{
23 wb->Navigate((WideString)"www.google.com");
24 while (wb->Busy)
25 Application->ProcessMessages();
26
27 if (wb->Document)
28 {
29 IHTMLDocument2 *html;
30 wb->Document->QueryInterface<ihtmldocument2>(&html);
31 html->put_designMode(L"On");
32 html->Release();
33 }
34}
35//---------------------------------------------------------------------------
36void __fastcall TForm1::btnInsertImageClick(TObject *Sender)
37{
38 if (wb->Document)
39 {
40 IHTMLDocument2 *html;
41 wb->Document->QueryInterface<ihtmldocument2>(&html);
42 VARIANT var;
43 VARIANT_BOOL receive;
44 html->execCommand(L"InsertImage",true,var, &receive);
45 html->Release();
46 }
47}
48//---------------------------------------------------------------------------
49void __fastcall TForm1::btnGetHtmlClick(TObject *Sender)
50{
51 if (wb->Document)
52 {
53 IHTMLDocument2 *html;
54 wb->Document->QueryInterface<ihtmldocument2>(&html);
55 IHTMLElement *pElement;
56 html->get_body(&pElement);
57 pElement->get_parentElement(&pElement);
58 wchar_t *tmp;
59 pElement->get_outerHTML(&tmp);
60 Memo1->Lines->Text = tmp;
61 pElement->Release();
62 html->Release();
63 }
64}
65//---------------------------------------------------------------------------