Code Examples & Download C++Builder Get Html Source in TCppWebBrowser--Full Source Code Download
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

How to Get the Html Content From TCppWebBrowser in C Builder

//-----------------------------------.h file --------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include "Classes.hpp"
#include "Controls.hpp"
#include "StdCtrls.hpp"
#include "Forms.hpp"
#include "SHDocVw_OCX.h"
#include "OleCtrls.hpp"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
    TButton *Button1;
    TButton *Button2;
    TCppWebBrowser *CppWebBrowser1;
    TMemo *Memo1;
    TButton *Button3;
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
    void __fastcall Button3Click(TObject *Sender);
private: // User declarations
public:  // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//----------------------------------.cpp file -----------------------------

//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "Unit1.h"
#include
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SHDocVw_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    CppWebBrowser1->Navigate(L"http://codeback.net");
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    if (CppWebBrowser1->Document == NULL)
        return;

    BSTR strContent;
    IHTMLDocument2* pDoc;
    if (SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc)))
    {
        IHTMLElement* pBody;
        pDoc->get_body(&pBody);
        pBody->get_innerHTML(&strContent);
        pDoc->close();
        pDoc->Release();
    }
    Memo1->Text = AnsiString(strContent);
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    IHTMLDocument2* pDoc;
    IDispatch* spDispatch;
    IHTMLElement* pElement;
    IHTMLElementCollection* pElementCol;

    if (SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc)))
    {
        if (SUCCEEDED(pDoc->get_all(&pElementCol)))
        {
            long lLength = 0;
            if (SUCCEEDED(pElementCol->get_length(&lLength)))
            {
                for (int i = 0; i < lLength; ++i)
                {
                    VARIANT index;
                    index.vt = VT_I4;
                    index.lVal = i;
                    if (SUCCEEDED(pElementCol->item(index, index, &spDispatch)))
                    {
                        if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLElement, (void**)&pElement)))
                        {
                            BSTR TagName = NULL;
                            pElement->get_tagName(&TagName);

                            if (AnsiString(TagName).AnsiPos("HTML") > 0)
                            {
                                BSTR HtmlContent;
                                pElement->get_outerHTML(&HtmlContent);
                                Memo1->Text = AnsiString(HtmlContent);
                                SysFreeString(HtmlContent);
                            }
                            SysFreeString(TagName);

                            pElement->Release();
                        }
                        spDispatch->Release();
                    }

                }//end of for

            }
            pElementCol->Release();
        }

        pDoc->Release();
    }
}
//---------------------------------------------------------------------------

//----------------------------------Form1 text--------------------------

object Form1: TForm1
  Left = 271
  Top = 173
  Width = 743
  Height = 495
  Caption =
    'How to get the html content from TCppWebBrowser in C Builder--Fr' +
    'om codeback.net'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 208
    Top = 416
    Width = 75
    Height = 25
    Caption = 'GoUrl'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 320
    Top = 416
    Width = 81
    Height = 25
    Caption = 'GetBodyHtml'
    TabOrder = 1
    OnClick = Button2Click
  end
  object CppWebBrowser1: TCppWebBrowser
    Left = 16
    Top = 8
    Width = 417
    Height = 393
    TabOrder = 2
    ControlData = {
      4C000000192B00009E2800000000000000000000000000000000000000000000
      000000004C000000000000000000000001000000E0D057007335CF11AE690800
      2B2E126208000000000000004C0000000114020000000000C000000000000046
      8000000000000000000000000000000000000000000000000000000000000000
      00000000000000000100000000000000000000000000000000000000}
  end
  object Memo1: TMemo
    Left = 440
    Top = 8
    Width = 273
    Height = 393
    Lines.Strings = (
      'Memo1')
    TabOrder = 3
  end
  object Button3: TButton
    Left = 432
    Top = 416
    Width = 81
    Height = 25
    Caption = 'GetFullHtml'
    TabOrder = 4
    OnClick = Button3Click
  end
end

The codes above has been tested under the enviroment of C++Builder 6 + Windows 7.


Code Examples & Download - C++Builder