TLama
TLama

Reputation: 76663

How to render HTML element without using web browser?

Is there a way how to draw specific HTML element content on a canvas without using any web browser control ?

With this code I'm rendering the element to the form's canvas (just as an example).
It works though, but this code is not a good practice - see below, why...

uses
  SHDocVw, MSHTML;

procedure TForm1.Button1Click(Sender: TObject);
var
  WebBrowser: TWebBrowser;
  HTMLElement: IHTMLElement;
  HTMLRenderer: IHTMLElementRender;
begin
  WebBrowser := TWebBrowser.Create(nil);
  try
    WebBrowser.ParentWindow := Application.Handle;
    WebBrowser.Navigate('https://stackoverflow.com/questions/2975586/good-delphi-blogs');

    while WebBrowser.ReadyState < READYSTATE_COMPLETE do
      Application.ProcessMessages;

    HTMLElement := (WebBrowser.Document as IHTMLDocument3).getElementById('question');
    HTMLRenderer := (HTMLElement as IHTMLElementRender);
    HTMLRenderer.DrawToDC(Canvas.Handle);

  finally
    HTMLElement := nil;
    HTMLRenderer := nil;
    WebBrowser.Free;
  end;
end;

It's bad because

Is there a clean way to solve this using MSHTML ?

Upvotes: 15

Views: 13652

Answers (3)

kobik
kobik

Reputation: 21232

DrawToDC and IViewObject both require the TWebBrowser control to actually render the document into a target DC.

Upvotes: 3

Ravaut123
Ravaut123

Reputation: 2798

See THTMLabel from tms-software

Upvotes: 1

Andr&#233;
Andr&#233;

Reputation: 9112

Maybe you can try THtmlViewer? http://code.google.com/p/thtmlviewer/

Upvotes: 1

Related Questions