我正在处理旧版应用程序,并尝试使VB6将打印的输出发送到pdf打印机(Nitro)。应用程序的输出应最终打印到特定位置/文件名的pdf文件中。
问题:
当我调用任何Printer
函数时,它会弹出一个对话框,但是我看不到设置对话框的默认路径和文件名的方法。有没有办法做到这一点?
即使我创建了自己的对话框,对Printer
的任何函数调用都会立即弹出一个对话框。
弹出对话框的代码行:
'How do I set the dialogue box it opens here to a default filename/location?
Printer.PaintPicture .Picture, .Left + lngAddToLeft, .Top + lngAddToTop, .Width, .Height
首先,您必须将系统的默认打印机设置为“ Nitro的打印机名称”
下面的代码可以帮助您做到这一点
Private Sub set_Nitro_As_Default()
Dim prn As Printer
If Printers.Count > 0 Then
For Each prn In Printers
If prn.DeviceName = [Nitro Printer Name] Then 'printer that u want to use
Set Printer = prn
Exit For
End If
Next prn
End If
End Sub
要打印一些文本,标签或图形对象,必须将图片框添加到项目中。它包含要打印的所有对象。
准备好所有对象(例如标签或形状)并准备好进行打印的所有东西后,请将该子对象保存在临时bmp文件中的图片框图像
'add this declaration on top of your project
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_PAINT = &HF
Private Const WM_PRINT = &H317
Private Const PRF_CLIENT = &H4& ' Draw the window's client area
Private Const PRF_CHILDREN = &H10& ' Draw all visible child
Private Const PRF_OWNED = &H20& ' Draw all owned windows
'this function save PicBox image in FileName path
'PicBox Contain All thing which you want to print
Public Sub SavePictureBox(PicBox As PictureBox, FileName As String)
Dim rv As Long
Dim ar As Boolean
With PicBox
'Save ReDraw value
ar = .AutoRedraw
.AutoRedraw = True
'Draw controls to picture box
rv = SendMessage(.hwnd, WM_PAINT, .hdc, 0)
rv = SendMessage(.hwnd, WM_PRINT, .hdc, _
PRF_CHILDREN Or PRF_CLIENT Or PRF_OWNED)
'save picture box
SavePicture .Image, FileName
.Cls
'Restore ReDraw
.AutoRedraw = ar
End With
End Sub
您必须在主打印命令中添加类似的内容
private sub iPrint()
'firstly set your intended printer as default
call set_Nitro_As_Default
'set some Generic setting
Printer.PrintQuality = vbPRPQHigh
Printer.PaperSize = vbPRPSA4
for i=1 to [your intended page count]
'do some thing for Arrange your labels and objects to make it ready to print
'after prepare your all thing on picturebox
call SavePictureBox([printed PictureBox], App.path & "\tmp.bmp")
'print this page
'note that you can set margin and your printed page size
Printer.PaintPicture LoadPicture(App.Path & "\tmp.bmp"), 0, 0, _
LoadPicture(App.Path & "\tmp.bmp").Width, _
LoadPicture(App.Path & "\tmp.bmp").Height, _
0, 0, _
Printer.Width, _
Printer.Height
Printer.NewPage
next i
Printer.EndDoc
end sub
完成