Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • it uses the hidden TWebBrowser control, but I would like to load the HTML document directly through the IHTMLDocument interface and render certain element on my own canvas
  • if I create and load the IHTMLDocument manually e.g. this way then the renderer method IHTMLElementRender.DrawToDC doesn't paint anything (maybe because there's no canvas for rendering of the document)
  • even worse is that IHTMLElementRender.DrawToDC is deprecated at this time, so I'm looking for an alternative method for rendering elements on my own canvas

Is there a clean way to solve this using MSHTML ?

like image 616
TLama Avatar asked Nov 29 '11 14:11

TLama


People also ask

How can I run HTML code without browser?

To display HTML, CSS and JS content you can use any notepad editor available on the Operating System you use like notepad, notepad++ etc. But to render it, you can use IDE's like Eclipse which provide you with a 'Web Developer Tools' plugin.

Can HTML be run outside of browser?

As a website, the answer is no.

How do I display HTML code without rendering?

You can show HTML tags as plain text in HTML on a website or webpage by replacing < with &lt; or &60; and > with &gt; or &62; on each HTML tag that you want to be visible. Ordinarily, HTML tags are not visible to the reader on the browser.


2 Answers

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

like image 67
kobik Avatar answered Sep 27 '22 22:09

kobik


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

like image 40
André Avatar answered Sep 27 '22 21:09

André