添加对打印和的支持在基于对话框的 MFC 应用程序中预览 HTML

2021-12-28 00:00:00 windows printing winapi c++ mfc

我有一个由两部分组成的问题.我需要为现有的基于对话框的 MFC 项目添加对打印的支持.正在打印的文档是使用 HTML 组成的.我知道我可以添加基于 HTML 的对话框,但是如何添加打印功能呢?

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.

推荐答案

灵感来自 优秀的 Marc 的 Durdin 的文章,我做了更多的探索.实际上似乎有一种更简单的方法来提供自定义 DEVMODEDEVNAMES 并在不使用 HTML 对话框或自定义 IE 打印模板.反过来,应该允许设置自定义打印机、纸张尺寸、方向等.

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.

我在 C++ 中有一个游乐场 WebBrowser ActiveX 宿主项目,类似于 this.我在我的 OLE 站点对象 (IOleClientSite) 上实现了 IOleCommandTarget 接口.现在这是有趣的部分,当打印被调用时(通过 Ctrl-P 或通过 IDM_PRINT),浏览器控件将站点对象回调为IOleCommandTarget::Exec(&CGID_DocHostCommandHandler, OLECMDID_PRINT2, &VARIANT(VT_UNKNOWN), NULL).第三个参数包含一个作为 IUnknown 传递的对象,但是当查询 IDispatch 时,它支持所有相同的 __IE_* 属性,可通过 IDispatch::Invoke:

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)

我还没有更进一步,但我认为应该可以改变它们中的任何一个并从 IOleCommandTarget::Exec 返回 S_OK,并期待浏览器控件接受更改.

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.

我希望它以类似的方式用于 IDM_PRINTPREVIEW/OLECMDID_PRINTPREVIEW2,但我还没有验证.如果时间允许,我会多玩一点.同时,欢迎您尝试并分享您的结果.

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.

相关文章