twebbrowser

Some ranking sites and search engines like alexa and google always cached a site's index page and store as an image in database for the users searching.

We could have this functino very easy in delphi. In this sample we use the TWebBrowser component to load a webpage, then use a function to capture the contents of the page and save it to an image. Ok, let us see the codes.

uses ActiveX;

//Loading a webpage when the program run.
procedure TForm1.FormCreate(Sender: TObject) ;
begin
  WebBrowser1.Navigate('http://codeback.net') ;
end;

procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant) ;
begin
  CaptureWebPagesContent(WebBrowser1,'c:\codeback.jpg') ;
end;

procedure CaptureWebPagesContent(const web: TWebBrowser; const fileName: TFileName) ;
var
  vObject : IvObject;
  r : TRect;
  bitmap : TBitmap;
begin
  if web.Document nil then
  begin
    web.Document.QueryInterface(IvObject, vObject) ;
    if Assigned(vObject) then
    try
      bitmap := TBitmap.Create;
      try
        r := Rect(0, 0, web.Width, web.Height) ;

        bitmap.Width := web.Width;
 bitmap.Height := web.Height;

        vObject.Draw(DVASPECT_CONTENT, 1, nil, nil, Application.Handle, bitmap.Canvas.Handle, @r, nil, nil, 0) ;

        //Convert the bitmap(.bmp format) to jpg file format.
        with TJPEGImage.Create do
        try
          Assign(bitmap) ; 
          SaveToFile(fileName) ;
        finally
          Free;
        end;
 //end of convert.

      finally
        bitmap.Free; //release the bitmap.
      end;
    finally
      vObject._Release; //release the viewObject.
    end;
  end;
end;

Note: this program will save the "http://codeback.net" site's frontpage as a jpg format of picture named codeback.jpg and stored it as "c:\codeback.jpg".
 

 

To get a webpage's html source code is very easy via the TWebBrowser component. Let us look an example.

Create a new project, drag a TWebBrowser, a TMemo and a TButton component onto the form. Adding codes as below shows.

uses mshtml;

//Loading a page when the program runs
procedure TForm1.FormCreate(Sender: TObject) ;
begin
  WebBrowser1.Navigate('http://codeback.net') ;
end;

//In the OnClick events of button1, adding codes.
procedure TForm1.Button1Click(Sender: TObject);
var
   iElement : IHTMLElement;
begin
   if Assigned(WebBrowser1.Document) then
   begin
     iElement := (WebBrowser1.Document AS IHTMLDocument2).body;

     while iElement.parentElement nil do
     begin
       iElement := iElement.parentElement;
     end;
     memo1.Text := iElement.outerHTML;
   end;
end;

After the page loading complete, click the button, then you would find the memo1 shows the html source. If you are using the C++Builder, see the code example here: 

How to Get the Content of Html Source From TCppWebBrowser in C Builder .


 

 

How to enable the twebbrowser to be editing?
Inface, twebbrowser has a design mode state. It could be set to allow the user to edit the content of a webpage. In delphi, this function is very easy to be called.

See an example.

Create a new object, drag a TWebBrowser onto the form, double click the form and add the following codes to its creat event.

uses MSHTML;
procedure TForm1.FormCreate(Sender: TObject) ;
begin
  WebBrowser1.Navigate('http://codeback.com') ;
end;

In the webbrowser1's event, double click the onDocumentComplete event to add codes.
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
   const pDisp: IDispatch; var URL: OleVariant) ;
begin
   ((Sender as TWebBrowser).Document as IHTMLDocument2).designMode := 'on';
end;

Run the project, when the page loaded, move your mouse onto the webbrowser area and click, see what happen.

Notice that when finish the editing, the designMode should be set to 'no' for saving the document's change.

Use this function in c++builder, see How to make the TCppWebbrowser editable in c++ builder

 

This is because you do not call the OleInitialize function. As the OLE operation is not thread-safe, so operation to the memory will cause failure or error. OleInitialize ensure that the concurrent modules run in an STA mode (that is, a single-threaded mode)  to avoid the sharing of resources error. Therefore, in a program's starting function WinMain, we should make some changes as following.
WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int)
(
    OleInitialize (NULL);
    try
    (
       ... ... //Normal statements
    ) catch (...)
    (
       ... ... //Normal statements
    )
    OleUninitialize ();
    return 0;
)

In a C-Builder program, you could also call the OleInitialize function in the main form's OnCreate event, like below mode. Last, call the OleUninitialize function when the form destroy.

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    OleInitialize(NULL);   
}

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
    OleUninitialize();   
}

In a Delphi program, is in a same way.

 
2
Next
End