如何在不使用Web浏览器的情况下呈现HTML元素? [英] How to render HTML element without using web browser?

查看:230
本文介绍了如何在不使用Web浏览器的情况下呈现HTML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在画布上绘制特定的HTML元素内容,而不使用任何Web浏览器控件?

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('http://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 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

有没有一个干净的方法来解决这个使用MSHTML?

Is there a clean way to solve this using MSHTML ?

推荐答案

DrawToDC和 IViewObject 都要求TWebBrowser控件实际将文档呈现到目标DC。

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

这篇关于如何在不使用Web浏览器的情况下呈现HTML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆