我正在开发一个遗留应用程序,并试图让 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
Run Code Online (Sandbox Code Playgroud)
首先,您必须将系统的默认打印机设置为“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
Run Code Online (Sandbox Code Playgroud)
要打印一些文本、标签或图形对象,您必须向项目添加一个图片框。它包含您要打印的所有对象。
在您准备好所有对象(例如标签或形状)以及准备好打印的所有内容后,调用此子程序将您的图片框图像保存在临时 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
Run Code Online (Sandbox Code Playgroud)
你必须在你的主打印命令中添加一些这样的东西
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
Run Code Online (Sandbox Code Playgroud)
完成