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 ?
IHTMLElement
doesn't supportIViewObject
. – KrouseParentWindow
set to theApplication.Handle
, what seems to me not so good. To the second question, because I want to avoid to useTWebBrowser
at all; I'd like to work directly withIHTMLDocument
. Why not chromium ? Just because I'm looking for a MS HTML solution :) I know it's better than the old IWebBrowser2 wrapper ;) – KrouseIHTMLDocument
manually but it paints the WB borders also... :/ I also tried to use"htmlfile"
ole object, but it doesn't paint anything on the output canvas... if DrawToDC is deprecated in IE9 then you probably should use a 3rd party solution likeTHtmlViewer
. – AdulterationIHTMLDocument
manually without web browser control ? That's probably what I'm looking for, at this time I think it's impossible because the document misses the renderer - the web browser control. I'm able to load the document, find the element but I can't render it without web browser control. – KrouseSetHtml
as string method, rather than Navigate. This can not be done without using the TWebBrowser IMHO. the response HTML is depended on the css, it can be dynamically created with JS, etc... so , the TWebBrowser control (Internet Explorer_Server
HWND) is rendering the page. I think your solution is good enough (if it's a minor functionality in you app). – AdulterationTWebBrowser
is necessary for content rendering. Anyway, thanks to all – Krouse