关于VB选择打印机,并打印
2015-12-07 21:18
1156 查看
一、 VB选择打印机,与打印机的基本设置 使用PrintDialog
第二、调用PrintDialog,选择打印机,添加打印代码
第三,常用方法,属性
Option Explicit Private Const CCHDEVICENAME = 32 Private Const CCHFORMNAME = 32 Private Const GMEM_FIXED = &H0 Private Const GMEM_MOVEABLE = &H2 Private Const GMEM_ZEROINIT = &H40 Private Const DM_DUPLEX = &H1000& Private Const DM_ORIENTATION = &H1& ' ' --- API TYPES DEFINITION ' Private Type PRINTDLG_TYPE lStructSize As Long hwndOwner As Long hDevMode As Long hDevNames As Long hdc As Long Flags As Long nFromPage As Integer nToPage As Integer nMinPage As Integer nMaxPage As Integer nCopies As Integer hInstance As Long lCustData As Long lpfnPrintHook As Long lpfnSetupHook As Long lpPrintTemplateName As String lpSetupTemplateName As String hPrintTemplate As Long hSetupTemplate As Long End Type Private Type DEVNAMES_TYPE wDriverOffset As Integer wDeviceOffset As Integer wOutputOffset As Integer wDefault As Integer extra As String * 100 End Type Private Type DEVMODE_TYPE dmDeviceName As String * CCHDEVICENAME dmSpecVersion As Integer dmDriverVersion As Integer dmSize As Integer dmDriverExtra As Integer dmFields As Long dmOrientation As Integer dmPaperSize As Integer dmPaperLength As Integer dmPaperWidth As Integer dmScale As Integer dmCopies As Integer dmDefaultSource As Integer dmPrintQuality As Integer dmColor As Integer dmDuplex As Integer dmYResolution As Integer dmTTOption As Integer dmCollate As Integer dmFormName As String * CCHFORMNAME dmUnusedPadding As Integer dmBitsPerPel As Integer dmPelsWidth As Long dmPelsHeight As Long dmDisplayFlags As Long dmDisplayFrequency As Long End Type ' ' --- API DECLARATIONS ' Private Declare Function PrintDialog Lib "comdlg32.dll" Alias "PrintDlgA" _ (pPrintdlg As PRINTDLG_TYPE) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (hpvDest As Any, _ hpvSource As Any, _ ByVal cbCopy As Long) Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long Private Declare Function GlobalUnlock Lib "kernel32" _ (ByVal hMem As Long) As Long Private Declare Function GlobalAlloc Lib "kernel32" _ (ByVal wFlags As Long, _ ByVal dwBytes As Long) As Long Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long ' ' --- PUBLIC ENUM ' Public Enum PrinterConstants cdlPDAllPages = &H0 cdlPDCollate = &H10 cdlPDDisablePrintToFile = &H80000 cdlPDHelpButton = &H800 cdlPDHidePrintToFile = &H100000 cdlPDNoPageNums = &H8 cdlPDNoSelection = &H4 cdlPDNoWarning = &H80 cdlPDPageNums = &H2 cdlPDPrintSetup = &H40 cdlPDPrintToFile = &H20 cdlPDReturnDC = &H100 cdlPDReturnDefault = &H400 cdlPDReturnIC = &H200 cdlPDSelection = &H1 cdlPDUseDevModeCopies = &H40000 End Enum Public Enum ErrorConstants cdlCancel = 32755 End Enum ' ' --- PRIVATE VARIABLES ' Private intMinPage As Integer ' Local copy of Min Private intMaxPage As Integer ' Local copy of Max Private intFromPage As Integer ' Local copy of FromPage Private intToPage As Integer ' Local copy of ToPage Private intCopies As Integer ' Local copy of ToPage ' N.B. 0 >= Min >= FromPage >= ToPage >= Max ' If Max=0 then no limits. ' ' --- PUBLIC VARIABLES ' Public Flags As PrinterConstants Public CancelError As Boolean ' ' -- INITIALIZE ' Private Sub Class_Initialize() intMinPage = 0 intMaxPage = 0 intFromPage = 0 intToPage = 0 intCopies = 1 CancelError = False End Sub ' ' -- PUBLIC MEMBERS ' Property Get Min() As Integer Min = intMinPage End Property Property Let Min(ByVal intNewValue As Integer) intNewValue = IIf(intNewValue > 0, intNewValue, 0) intMinPage = intNewValue If intNewValue > intFromPage Then _ intFromPage = intNewValue If intNewValue > intToPage Then _ intToPage = intNewValue If intNewValue > intMaxPage Then _ intMaxPage = intNewValue End Property Property Get FromPage() As Integer FromPage = intFromPage End Property Property Let FromPage(ByVal intNewValue As Integer) intNewValue = IIf(intNewValue > 0, intNewValue, 0) intFromPage = intNewValue If intNewValue > intToPage Then _ intToPage = intNewValue If intNewValue > intMaxPage Then _ intMaxPage = intNewValue If intNewValue < intMinPage Then _ intMinPage = intNewValue End Property Property Get ToPage() As Integer ToPage = intToPage End Property Property Let ToPage(ByVal intNewValue As Integer) intNewValue = IIf(intNewValue > 0, intNewValue, 0) intToPage = intNewValue If intNewValue > intMaxPage Then _ intMaxPage = intNewValue If intNewValue < intFromPage Then _ intFromPage = intNewValue If intNewValue < intMinPage Then _ intMinPage = intNewValue End Property Property Get Max() As Integer Max = intMaxPage End Property Property Let Max(ByVal intNewValue As Integer) intNewValue = IIf(intNewValue > 0, intNewValue, 0) intMaxPage = intNewValue If intNewValue < intToPage Then _ intToPage = intNewValue If intNewValue < intFromPage Then _ intFromPage = intNewValue If intNewValue < intMinPage Then _ intMinPage = intNewValue End Property Property Get Copies() As Integer Copies = intCopies End Property Property Let Copies(ByVal intNewValue As Integer) intNewValue = IIf(intNewValue > 0, intNewValue, 0) intCopies = intNewValue End Property Public Function ShowPrinter() As Boolean Dim PrintDlg As PRINTDLG_TYPE Dim DevMode As DEVMODE_TYPE Dim DevName As DEVNAMES_TYPE Dim lpDevMode As Long, lpDevName As Long Dim intReturn As Integer Dim objPrinter As Printer Dim strNewPrinterName As String Dim blnCancel As Boolean blnCancel = False ' Use PrintDialog to get the handle to a memory ' block with a DevMode and DevName structures With PrintDlg .lStructSize = Len(PrintDlg) .hwndOwner = 0 .Flags = Flags .nMinPage = intMinPage .nFromPage = intFromPage .nToPage = intToPage .nMaxPage = intMaxPage Debug.Print "1 = " & intCopies .nCopies = intCopies End With 'Set the current orientation and duplex setting DevMode.dmDeviceName = Printer.DeviceName DevMode.dmSize = Len(DevMode) DevMode.dmFields = DM_ORIENTATION Or DM_DUPLEX DevMode.dmOrientation = Printer.Orientation On Error Resume Next DevMode.dmDuplex = Printer.Duplex On Error GoTo 0 'Allocate memory for the initialization hDevMode structure 'and copy the settings gathered above into this memory PrintDlg.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or _ GMEM_ZEROINIT, Len(DevMode)) lpDevMode = GlobalLock(PrintDlg.hDevMode) If lpDevMode > 0 Then CopyMemory ByVal lpDevMode, DevMode, Len(DevMode) intReturn = GlobalUnlock(lpDevMode) End If 'Set the current driver, device, and port name strings With DevName .wDriverOffset = 8 .wDeviceOffset = .wDriverOffset + 1 + Len(Printer.DriverName) .wOutputOffset = .wDeviceOffset + 1 + Len(Printer.Port) .wDefault = 0 End With With Printer DevName.extra = .DriverName & Chr(0) & _ .DeviceName & Chr(0) & .Port & Chr(0) End With 'Allocate memory for the initial hDevName structure 'and copy the settings gathered above into this memory PrintDlg.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or _ GMEM_ZEROINIT, Len(DevName)) lpDevName = GlobalLock(PrintDlg.hDevNames) If lpDevName > 0 Then CopyMemory ByVal lpDevName, DevName, Len(DevName) intReturn = GlobalUnlock(lpDevName) End If 'Call the print dialog up and let the user make changes If PrintDialog(PrintDlg) Then 'First get the DevName structure. lpDevName = GlobalLock(PrintDlg.hDevNames) CopyMemory DevName, ByVal lpDevName, 45 intReturn = GlobalUnlock(lpDevName) With PrintDlg Flags = .Flags intFromPage = .nFromPage intToPage = .nToPage intCopies = .nCopies End With GlobalFree PrintDlg.hDevNames 'Next get the DevMode structure and set the printer 'properties appropriately lpDevMode = GlobalLock(PrintDlg.hDevMode) CopyMemory DevMode, ByVal lpDevMode, Len(DevMode) intReturn = GlobalUnlock(PrintDlg.hDevMode) strNewPrinterName = UCase$(Left(DevMode.dmDeviceName, _ InStr(DevMode.dmDeviceName, Chr$(0)) - 1)) If Printer.DeviceName <> strNewPrinterName Then For Each objPrinter In Printers If UCase$(objPrinter.DeviceName) = strNewPrinterName Then _ Set Printer = objPrinter Next End If On Error Resume Next 'Set printer object properties according to selections made 'by user With Printer .Copies = PrintDlg.nCopies .Duplex = DevMode.dmDuplex .Orientation = DevMode.dmOrientation End With On Error GoTo 0 GlobalFree PrintDlg.hDevMode Else GlobalFree PrintDlg.hDevMode GlobalFree PrintDlg.hDevNames blnCancel = True If CancelError Then _ Err.Raise cdlCancel, "LM PrintDialog", "Cancel." End If ShowPrinter = Not blnCancel End Function
第二、调用PrintDialog,选择打印机,添加打印代码
<pre name="code" class="vb">Private Function Print_Measure_Data() Dim NewTime As Date Dim PrintData(24) As String Dim MyPDialog As New clsPrintDialog Dim Number_Copies As Integer, i, j Dim PrintFromPage As Integer Dim PrintToPage As Integer Dim Measure_Data_Manage_Counts As Integer Measure_Data_Manage_Counts = 3 NewTime = Now Call PrintDataInit(PrintData, NewTime) MyPDialog.Flags = cdlPDAllPages + cdlPDDisablePrintToFile + cdlPDNoSelection MyPDialog.Min = 1 MyPDialog.Copies = 1 MyPDialog.Max = 100 MyPDialog.FromPage = 1 MyPDialog.ToPage = Measure_Data_Manage_Counts If MyPDialog.ShowPrinter = True Then '如果确认打印 Debug.Print "Printer.DeviceName:" & Printer.DeviceName Number_Copies = Printer.Copies If MyPDialog.FromPage > Measure_Data_Manage_Counts Or MyPDialog.FromPage < 1 Then PrintFromPage = 1 Else PrintFromPage = MyPDialog.FromPage End If Debug.Print "PrintFromPage = " & PrintFromPage If MyPDialog.ToPage > Measure_Data_Manage_Counts Or MyPDialog.ToPage < MyPDialog.FromPage Then PrintFromPage = Measure_Data_Manage_Counts Else PrintToPage = MyPDialog.ToPage End If Debug.Print "PrintToPage = " & PrintToPage For i = 1 To Number_Copies Debug.Print "开始打印" Debug.Print "打印" & Number_Copies & "份" j = PrintToPage Do '在此添加打印代码 Call PrintTable(Printer, PrintData, j - PrintFromPage + 1, PrintToPage - PrintFromPage + 1) '打印表格,打印机对象、打印数据数组、第j-PrintFromPage+1页、共PrintToPage-PrintFromPage+1 页 If j = PrintToPage Then Printer.CurrentX = SignatureCoordinate_X_ForA4_F Printer.CurrentY = SignatureCoordinate_Y_ForA4_F Printer.FontSize = SignatureFontSize Printer.Print "签名:" End If If j > PrintFromPage Then Printer.NewPage End If j = j - 1 '循环控制变量 Loop Until j = PrintFromPage - 1 Printer.EndDoc Next End If End Function
第三,常用方法,属性
Printer.CurrentX = '设置位置 Printer.CurrentY = Printer.FontSize = ‘设置字体大小 Printer.Print ‘打印字符串 Printer.Line (起点X,起点Y)-(终点X, 终点Y) '画线函数
相关文章推荐
- VB.NET多线程入门
- 用Visual Basic设计手机短信收发程序
- 用Visual Basic设计手机短信收发程序
- PSI_SI
- vbs版的音乐播放器
- VB6 正则表达式提取内容
- EXCEL VBA 导入图片自适应大小
- VB.NET读取Excel工作表信息
- 用Visual Basic设计手机短信收发程序
- 使用swoole进行消息推送通知,配合vb.net进行客户端开发一样爽[开发篇]
- C#VSVB殊途同归
- 设置透明的NavBar 获取纯色图片
- VB.NET小总
- VB.net三层登录实现
- [VB.NET]调用API获取/设置键盘按键状态
- excel如何调用VBA代码
- 二进制转八进制vb6程序
- 二进制转十六进制vb6程序
- vb6判断动态字节数组是否已经初始化的最简单的办法
- OpenGL ES 学习教程(五) 极速绘制,使用 VBO (顶点缓冲区对象)!