The print template in IE uses this property to determine information about the selected printer after a user closes the Print dialog box. How can I set this property to a DEVMODE structure with code in Visual C++. How can I convert DEVMODE structure to variant. If it possible, I can pass variant to print template and then set __IE_PrinterCmd_DevMode property in jscript.
How to set __IE_PrinterCmd_DevMode Property to a DEVMODE structure in print template in IE with Visual C++
926 Views Asked by A.Danesh At
2
There are 2 best solutions below
2
On
I have just had this same issue and have found that __IE_PrinterCmd_DevMode and __IE_PrinterCmd_DevNames can be set from an IntPtr.
This is on a X86 application, so not sure what would happen on x64 or AnyCPU.
As suggested, I use a class to pass in the DevMode and DevNames through the external object.
Here's the main parts of the code, for this:
Public Class PrintObjCls
Public Printer As String
Public DevMode As IntPtr
Public DevNames As IntPtr
Public Printing As Boolean
Public Failed As Boolean
Public Progress As Integer
End Class
Sub PrintToTemplate(Web as WebBrowser, Settings as PrinterSettings)
Dim Obj As New PrintObjCls
Obj.Printer = Settings.PrinterName
Obj.DevMode = Settings.GetHdevmode
Obj.DevNames = Settings.GetHdevnames
Web.ObjectForScripting = Obj
End Sub
Then in the Print Template
var ext = doc.parentWindow.external;
dialogArguments.__IE_PrinterCMD_Printer = ext.Printer;
dialogArguments.__IE_PrinterCmd_DevMode = ext.DevMode;
dialogArguments.__IE_PrinterCmd_DevNames = ext.DevNames;
Marc Durdin has an excelent blog post with a detailed example in Delphi. It's easily portable to C++ and other languages:
In particular,
__IE_PrinterCmd_DevModeshould be an unlockedHGLOBAL(typically a valid return value fromGlobalAllocorGlobalReAlloc). This is not documented anywhere, I guess Marc discovered the hard way by trial and error, finally finding it working with the values in thePRINTDLG.hDevModeandPRINTDLG.hDevNamesfields, as directly provided by a call toPrintDlg.I've been able to pass
HGLOBALs as integers to a template's script and use them to initialize__IE_PrinterCmd_DevModeand__IE_PrinterCmd_DevNames, before creating aTemplatePrinter. This is handy if you don't want to callShowHTMLDialogExyourself and you already have a hook into your application. I'm using the original window'sexternalscripting object. To access it from the template, I use:If you just want to select a printer other than the system default, you may just as well set the
__IE_PrinterCMD_Printerproperty. You can do it in JScript, it'll affect theTemplatePrinterbehavior objects that you create after setting it. However, with this property alone, you cannot control the initial settings or know which printer the user finally chose.