简体   繁体   中英

Add support to print & preview HTML in a dialog-based MFC app

I have a two-part question. I need to add supporting for printing to an existing dialog-based MFC project. The document being printed is composed using HTML. I know that I can add HTML-based dialog but how do you add a capability for printing to it?

PS. I need this to be able to set the print page size according to a program's needs.

Inspired by the excellent Marc's Durdin's article , I've done some more spelunking. There actually appears to be an easier way to supply custom DEVMODE and DEVNAMES and print without using an HTML dialog or a custom IE print template . That, in turn, should allow to set custom printer, paper size, orientation, etc.

I have a playground WebBrowser ActiveX host project in C++, similar to this . I implement IOleCommandTarget interface on my OLE site object ( IOleClientSite ). Now here's the interesting part, when printing gets invoked (via Ctrl-P or via IDM_PRINT ), the browser control calls back the site object as IOleCommandTarget::Exec(&CGID_DocHostCommandHandler, OLECMDID_PRINT2, &VARIANT(VT_UNKNOWN), NULL) . The 3rd parameter contains an object which is passed as IUnknown , but when queried for IDispatch it supports all the same __IE_* properties , available via IDispatch::Invoke :

__IE_TemplateUrl (VT_EMPTY)
__IE_ParentHWND (VT_UINT)
__IE_HeaderString (VT_BSTR)
__IE_FooterString (VT_BSTR)
__IE_OutlookHeader (VT_UNKNOWN)
__IE_BaseLineScale (VT_INT)
__IE_uPrintFlags (VT_UINT)
__IE_ContentDocumentUrl (VT_BSTR)
__IE_ContentSelectionUrl (VT_BSTR)
__IE_PrinterCMD_Printer (VT_BSTR)
__IE_PrinterCMD_Device (VT_BSTR)
__IE_PrinterCMD_Port (VT_BSTR)
__IE_BrowseDocument (VT_UNKNOWN)
__IE_TemporaryFiles (VT_ARRAY)
__IE_PrinterCMD_DevNames (VT_I4)
__IE_PrinterCMD_DevMode (VT_I4)
__IE_PrintType (VT_BSTR)

I haven't taken this further yet, but I think it should be possible to alter any of the them and return S_OK from IOleCommandTarget::Exec , and expect the browser control to take in the changes.

I expect it to work in a similar way for IDM_PRINTPREVIEW / OLECMDID_PRINTPREVIEW2 , but I haven't verified that yet. I'll play with this a bit more, as time allows. Meanwhile, you're welcome to try it out and share your results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM