delphi - How to render WebBrowser to device context? -
i want render web-page (i.e. twebbrowser) device context. want use internet explorer's layout engine render content device context (i.e. metafile, pdf metafile).
starting internet explorer 9 ihtmlelementrender interface no longer supported:
ihtmlelementrender interface
use interface draw contents of element specified device context, printer.
members
ihtmlelementrender interface inherits iunknown interface not have additional members.
to point no longer mention drawtodc method exists:
ihtmlelementrender::drawtodc method
deprecated. draws contents of element specified device context.
syntax
hresult drawtodc( hdc hdc );parameters
- hdc
[in] hdc specifying device drawn to, typically printer.return value
returns s_ok if successful, or error value otherwise.
remarks
as of windows internet explorer 9, method deprecated , should not used.
with printers, running ihtmlelementrender::drawtodc may cause problems. can ensure ihtmlelementrender::drawtodc works on printers running ihtmlelementrender::setdocumentprinter method first, , passing modified device context ihtmlelementrender::drawtodc.
note: i'm quoting documentation can still found when microsoft removes msdn altogether. along interface declaration:
ihtmlelementrender = interface(iunknown) ['{3050f669-98b5-11cf-bb82-00aa00bdce0b}'] function drawtodc(hdc: hdc): hresult; stdcall; function setdocumentprinter(const bstrprintername: widestring; hdc: hdc): hresult; stdcall; end; use iviewobject interface
i've tried converting use iviewobject (e.g. how render html element without using web browser?):
procedure renderwebbrowsertometafile(browser: twebbrowser; metafilename: string); var view: iviewobject; m: tmetafile; mc: tmetafilecanvas; w, h: integer; r: trect; dpix: integer; dpiy: integer; dc: hdc; begin w := webbrowserscrollwidth(browser); h := webbrowserscrollheightstrict(browser); //96dpi screen 300dpi metafile (destined printer) dc := getdc(0); try dpix := getdevicecaps(getdc(0), logpixelsx); dpiy := getdevicecaps(getdc(0), logpixelsy); releasedc(0, dc); end; w := muldiv(w, 300, dpix); h := muldiv(h, 300, dpiy); view := browser.document iviewobject; m := tmetafile.create; try m.width := w; m.height := h; r := rect(0, 0, w, h); mc := tmetafilecanvas.create(m, 0); try view.draw( dvaspect_content, //draw aspect 1, //index nil, //paspectinfo nil, //pdvtargetdevice 0, //target device context mc.handle, //hdcdraw @r, //target bounds @w, //window bounds (for metafiles) nil, //continue function 0); //continue value mc.free; end; m.savetofile(metafilename); m.free; end; end; the problem code renders visible area of browser (i.e. without scrolled content):
i need render entire web-page (e.g. printing).
i tried changing draw aspect dvaspect_content (as this example does), dvaspect_docprint:
dvaspect_docprint
provides representation of object on screen though printed printer using print command file menu. described data may represent sequence of pages.
with same result; rather rendering page, visible portion rendered:
how can ask ie9 rendering engine render?
i tried using different indexes along dvaspect_docprint. although iviewobject not documented, maybe that's how print different "pages":
view.draw(dvaspect_docprint, 1, ...); view.draw(dvaspect_docprint, 2, ...); view.draw(dvaspect_docprint, 3, ...); ... view.draw(dvaspect_docprint, n, ...); but webbrowser doesn't care index is; rendering current view (which suppose makes sense when using iviewobject).
how render browser device context?
note: question tagged delphi, there's nothing in question delphi specific.
update: tried other combinations of aspect , index:
view.draw(dvaspect_content, -1, ...); view.draw(dvaspect_docprint, -1, ...); view.draw(dvaspect_content, 0, ...); view.draw(dvaspect_docprint, 0, ...);
using iviewobject, can create screenshot of current viewport. use trick create screenshot of web page (without frames) or frame document.
- calculate full document size using scrollwidth/scrollheight/scrollleft/scrolltop (check ihtmldocument2 , ihtmldocument3 interfaces)
- lock window update , adjust document calculated full size
- use iviewobject create snapshot (your code above seems okay)
- restore document original size , unlock window update
note that should careful of doing when document extremely large.
Comments
Post a Comment