注册 登录
编程论坛 VB6论坛

打印机代码怎么写?

asad 发布于 2023-02-27 22:39, 1184 次点击
打印机有时会暂停打印,能不能用vb6代码控制打印机,取消暂停打印?
只有本站会员才能查看附件,请 登录
6 回复
#2
yuma2023-02-28 19:17
开始打印了,就取消暂停打印了。

你打开的这个是软件程序,也可以通过句柄来操作它。

打开的这个界面只是个工具。用命令也能打印文件。

[此贴子已经被作者于2023-2-28 19:37编辑过]

#3
asad2023-02-28 21:00
回复 2楼 yuma
在窗体添加个两个按钮,如按钮1和按钮2
点击按钮1就暂停打印,暂停打印前面的√打上。点击按钮2就取消暂停打印,暂停打印前面的√去掉,代码怎么写?

[此贴子已经被作者于2023-2-28 21:05编辑过]

#4
yuma2023-03-01 13:27
你可以尝试一下暂停一下打印服务试下。

服务启动、晢停、停止
net start spooler
net pause spooler
net stop spooler
#5
William19492023-03-15 10:49
回复 3楼 asad
Option Explicit
Private Type PRINTER_DEFAULTS
    pDatatype       As String
    pDevMode        As Long
    DesiredAccess   As Long
End Type
Private Const PRINTER_ACCESS_ADMINISTER = &H4

Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal Command As Long) As Long
Private hPrinter As Long

Private Sub Form_Load()
    Dim pDef As PRINTER_DEFAULTS
    pDef.DesiredAccess = PRINTER_ACCESS_ADMINISTER
    Call OpenPrinter(Printer.DeviceName, hPrinter, pDef)
End Sub

Private Sub Form_Terminate()
    Call ClosePrinter(hPrinter)
End Sub

Private Sub Command1_Click() '暂停
    Const PRINTER_CONTROL_PAUSE = 1
    Call SetPrinter(hPrinter, 0&, ByVal 0&, PRINTER_CONTROL_PAUSE)
End Sub

Private Sub Command2_Click() '取消暂停
    Const PRINTER_CONTROL_RESUME = 2
    Call SetPrinter(hPrinter, 0&, ByVal 0&, PRINTER_CONTROL_RESUME)
End Sub
#6
asad2023-03-15 17:39
回复 5楼 William1949
vba代码怎么写,是不是也是这个代码,谢谢
#7
William19492023-03-16 13:22
vba 我不熟,好像vba不能识别 Printer.DeviceName ,

不过可以用 GetDefaultPrinter 取代它, 示例:

Private Declare Function GetDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" (ByVal pszBuffer As String, pcchBuffer As Long) As Long

Private Function GetDeviceName() As String
    Dim sTemp   As String
    Dim sLen    As Long
    sLen = 255
    sTemp = String$(sLen, vbNullChar)
    Call GetDefaultPrinter(sTemp, sLen)
    GetDeviceName = Left$(sTemp, sLen - 1)
End Function
1